views:

24

answers:

1

Hello, someone asked how to get the value of a JSObject property from c. That helped me a bit.

But, does anyone know how to get the current JavaScript name of an object from c?

example:

var foo={prop:'bar'};

then somewhere for example in jsapi.cpp:

JS_somemethod(JSContext *cx, JSObject *obj){

//how do i get the name 'foo' (or the current alias) here if *obj points to the foo (or whatever alias name) object?

}

Thx for hints and answers!

A: 

Without looking too closely at the API, I'm going to say that while it might be possible to do this in some very basic cases, you don't want to. This is why:

In Javascript, as in most languages, variables point to values. Variables are not the values themselves. The object is not in any way inherently related to the name foo.

For instance, you can do

var foo = {prop: 'bar'};
var fooo = foo;
var foooo = foo;
var quux = {q: foo, r: foo, s: [foo]};

All of those foos are now the exact same foo; which one do you want back? There's no API call for that because it's too ambiguous and not terribly useful.

However, if you really want to find one of the global variables holding a value, you can try iterating over the keys on the global object and testing them for the value.

for(var key in this){
  if(this[key] == myval){
    var myname = key;
  }
}

You'd have to translate this into your API calls or else put it in a function and call that through the API.

The more simple and straightforward solution would be to figure out what you want to do with foo later on, and pass in a callback function that will do that, e.g. with JS_CallFunction.

tl;dr: Previous paragraph.

Jesse Millikan
Hello Jesse, thank you so much for your answer. Your explanation is very clear and understandable. From the user-script point of view you are absolutely right. But does it apply to the current executed context (JSContext) as well? We really do not have the power to distinguish between foo, fooo and foooo? Would be perfect if it was possible at runtime to do that even if all the foo*'s pointing to the same object.Thank you for the idea with iterating the object.Cheers.
Martin
Good, glad that helps. Again, no, if you're only given the value that foo, fooo and foooo are holding, there's nothing that distinguishes them.
Jesse Millikan
Ok, you convinced me for now ;)What i will do now to get the "name-chain" is: start with a given object, go to its parent (with JS_GetParent()) then iterate over all properties and compare the objects until it matches. hope it works.
Martin
It looks like JS_GetParent() will return the global object for objects from object literals. Are you sure that's what you want? Like I said, I still recommend a callback function for whatever you're trying to do.
Jesse Millikan
I am quite new to SpiderMonkey development. Maybe i tell you in short my purpose: For the integration of new security system in webbrowsers i need to find out what is accessed during a common session on a website. my aim is to get something like a log which objects are accessed and how (read,write,execute). for example: window.alert (x) window.foo.bar (w) ... you now why i need the names of the variables? maybe you've got an other idea?
Martin
Maybe use JS_DefineProperty on all of the properties you need to track, where the callback will log the read or write and then set or get the property as normal. (And do the same with JS_DefineFunction on function calls.)
Jesse Millikan