1

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!

Community
  • 1
  • 1
Martin
  • 11
  • 3

2 Answers2

1

Okay, just to document the question clarification from your comment I'll repeat it here:

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?

Just to say it up front, this is pretty tricky in general. There are a few options available to you, all of which are somewhat difficult:

  1. Use the debugging API, either via the cool new debugger object API or via jsdbgapi.cpp (the complex and slightly gross interface that firebug uses). You can use debugger functionality to trap on all property accesses, functions calls, and local references in order to dump out interesting properties of objects.
  2. If you're only interested in the way that your (user defined) objects are accessed, you can use Proxies that wrap your objects and dump out the accesses being performed on them. A slightly more powerful and low-level version of proxies are actually used in the browser to implement our security features. (Additionally, most of the proxy functionality is accessible for custom-created objects in the JSAPI via our meta-object-protocol, but proxies are just a much cleaner version of that same functionality.)
  3. Instrument the interpreter loop in jsinterp.cpp. A lot of research-like work turns off the JITs and instruments the interpreter loop, because it's fairly understandable if you've worked on language implementations before. Unfortunately, it's not always easy to translate what you want to do into interpreter loop instrumentation at first, even if you have experience with language implementations.

The debugger object is definitely my favorite option of those I've listed here. If you go with that approach and end up getting stuck feel free to visit #jsapi in irc.mozilla.org and try to get a hold of jorendorff or jimb, they're a) awesome and b) the creators of the debugger object.

cdleary
  • 69,512
  • 53
  • 163
  • 191
0

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
  • 3,104
  • 1
  • 21
  • 32
  • 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 Oct 13 '10 at 19:20
  • 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 Oct 13 '10 at 19:27
  • 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 Oct 13 '10 at 19:32
  • 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 Oct 13 '10 at 19:49
  • 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 Oct 13 '10 at 19:55
  • 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 Oct 13 '10 at 20:29