Subj : Re: [SpiderMonkey] Confused about class property To : netscape.public.mozilla.jseng From : Peter Paulus Date : Mon Oct 10 2005 06:02 pm Brendan Eich wrote: > Peter Paulus wrote: > >> Brendan Eich wrote: >> >>> Peter Paulus wrote: >>> >>>> >>>> >>>> Not just to start a monologue, I've disassembled the javascript: >>>> >>>> var x = ColorSpace.lab; >>>> >>>> into: >>>> >>>> 00000: 0 defvar "x" >>>> main: >>>> 00003: 0 bindname "x" >>>> 00006: 0 name "ColorSpace" >>>> 00009: 0 getprop "lab" >>>> 00012: 0 setname "x" >>>> 00015: 0 pop >>>> >>>> So I guess it must be the interpreter that turns ColorSpace into a >>>> Function object, rather than an 'ColorSpace' object. >>>> >>>> Are there flags on my JSClass struct or otherwise to manipulate this >>>> behavior? >>> >>> >>> >>> >>> If you don't want your call to JS_InitClass to bind a constructor to >>> the class name in the global object, pass null for the |constructor| >>> formal parameter. Then the class name will denote a prototype object >>> of your class, containing the properties and methods (not the static >>> properties and methods) you specify. >>> >>> /be >> >> >> I'm not sure I want to bind a constructor. I've looked at the >> constructor as a convenient place to bind private data to an object >> (JS_SetPrivate()). > > > > Sure. > > >> Up till now I've been passing the constructor function to >> JS_InitClass() and was still able to handle JSBool >> getClassProperty(JSContext*, JSObject*, jsid, jsval*) for any class. >> This has worked fine, since every JSClass pointed to it's own static >> JSBool getClassProperty(JSContext*, JSObject*, jsid, jsval*) function. > > > > That signature looks like JSObjectOps.getProperty, not > JSClass.getProperty (note the jsid third argument). Are you talking > about JSClass here, or JSObjectOps? > I'm talking about JSClass. The correct signature is static JSBool getClassProperty(JSContext*, JSObject*, jsval, jsval*);. I did it off the top of my head and made a mistake in the third parameter. I'm sorry about causing this noise. > >> Implicitely, when this function is called, you know for which class >> this is. There was never a need to examine the JSObject* in this case. >> Based on the id I could return an appropriate result. > > > > This is true of instance properties, not of "class static" properties of > the constructor. > > >> In my current project I'm trying to build classes dynamically in >> SpiderMonkey from Adobe InDesign's ExtendScript, in order to use their >> native objects from Spidermonkey. In this setup I have exactly one >> static method: JSBool getClassProperty(JSContext*, JSObject*, jsid, >> jsval*) to serve all classes. Now I was suprised to find that >> JSObject* pointed to a "Function" object. > > > > That's what a constructor object is, though. > > >> If I omit the constructor function with JS_InitClass() indeed the >> getClassProperty() contains the right kind of object. But then I have >> lost my place to augment a JSObject with my private data. >> >> Can you advise me of a suitable place to add private data to a JSObject? > > > > Use constructors for that. > > You need to show actual code. It's not clear how you were using > getClassProperty for all classes, or whether it was a JSObjectOps hook > or a JSClass hook, or why you couldn't use the static property specifier > argument to JS_InitClass. > > /be I'm preparing a zip archive which I can hopefully send you tommorrow. By 'using getClassProperties() for all classes' I mean: On all JSPropertySpecs passed to JS_InitClass() the getter functions points to a single C++ function. I do not have an getter function specific for a certain class on every JSPropertySpec, as would be the usual case. It is just a single method across all classes. With kind regards, Peter Paulus .