Subj : Re: Property resolution via GetProperty on the global object, and "this" To : netscape.public.mozilla.jseng From : donteventthinkaboutit Date : Tue Aug 03 2004 09:00 pm On Tue, 03 Aug 2004 11:00:24 -0700, Brendan Eich wrote: Thanks! (sorry this is a little long.) I got it working, but I would like to be sure that I understand. The exact code is hard to extract from its context so I will try to Illustrate with something much simpler. This was my original issue, (fixed by using Resolve) --- Class object: static JSClass sGlobalClass = { "Global", JSCLASS_HAS_PRIVATE, JS_PropertyStub, JS_PropertyStub, sGlobalGetProperty, JS_PropertyStub, JS_EnumerateStub, s:GlobalResolve, JS_ConvertStub, JS_FinalizeStub }; Global object creation: . . . JSObject *pGlobalObject= JS_NewObject(cx,&sGlobalClass,NULL,NULL); JS_InitStandardClasses(cx,pGlobalObject); . . . Evaluate Call: jsval result; JS_EvaluateScript(cx, pGlobalObject,"x",1, NULL,0, &result); 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'. If I create a property for 'x' via JS_DefineProperty in Resolve, then GetProperty will be called with an id of 'x'. 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. I was not using Resolve before, only GetProperty, so I was never seeing the property in GetProperty unless I qualified it with "this" in the script. --- 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) Does this sound reasonable? (i.e. using the API as it is intended?) Dennis >donteventthinkaboutit@sbcglobal.net wrote: > >> Here is my problem... >> >> I am calling JS_EvaluateScript on my global object. >> >> GetProperty only seems to be invoked if the property is explicitly >> preceeded by "this" >> >> so inside the script... >> >> this.foo // tries to resolve foo on the global object via >> // GetProperty as expected. >> >> foo // GetPoperty not called on global object. >> >> >> Please... what am I missing? > > >You have to show your code, at least the way you set up the global >object, and of course the exact call to JS_EvaluateScript, and the test >input in full -- otherwise I'm just guessing, which probably wastes both >our time. > >The JSClass.getProperty hook will be called for every property value >read from an object of the class, so my first thought is that |this| and >the scope object are in fact not the same object. > >BTW, if you need to make a connection between a proxy-object stored in a >property value and its peer native object only on first access, you >should use resolve, not getProperty. > >/be .