Subj : Re: Property resolution via GetProperty on the global object, and To : netscape.public.mozilla.jseng From : Brendan Eich Date : Tue Aug 03 2004 04:19 pm donteventthinkaboutit@sbcglobal.net wrote: > Now... if x is undefined, and I don't create a property for it in > Resolve, then the GetProperty hook will *not* get called with > an id of 'x'. Right, that's simpler than what I guessed was wrong, I should have seen it. When looking up an unqualified name in the scope chain, getProperty is not called, only resolve. See ECMA-262 (use [[Get]] for getProperty and [[HasProperty]] essentially for resolve). > If I create a property for 'x' via JS_DefineProperty in Resolve, > then GetProperty will be called with an id of 'x'. That's right, once resolve defines a property, it will be found on subsequent lookups, and getProperty will be called if those lookups are read access, setProperty for write accesses (assignments). > If instead: > > JS_EvaluateScript(cx, pGlobalObject,"this.x",6, NULL,0, &result); > > (notice the "this" in the script...) then GetProperty will be called > even if I do not create the property in Resolve. That's right too, because when you write o.p in JS, you are not searching a scope chain, you're instructing JS to get the value of property 'p' in the object denoted by o. > So here's a follow up question, now that I got that squared away... > > If I have a property that needs to stay synced up with a > object on the native side, then it seems I can do something like > the following: > > 1. use Resolve to create the property when first encountered. > > 2. use GetProperty to sync the property with any changes that > may have occurred in the native object (i.e. outside of javascript) Exactly. > Does this sound reasonable? (i.e. using the API as it is intended?) That's exactly how the API is intended to be used (resolve is actually very flexible, so you can do other things with it, such as define a property on a prototype object, or insert an object in the prototype chain -- it pays to study the "2-D" lookup that goes on in JS with unqualified name uses such as 'x', as well as the "1-D" prototype chain lookup that occurs with 'o.p'). /be .