Subj : Re: finilize & construct: call disbalance To : netscape.public.mozilla.jseng From : anton muhin Date : Fri Jul 09 2004 07:28 pm First of all, thank you for your explanation. Brendan Eich wrote: > When you call JS_InitClass, it creates a prototype object of your class, > so that object will be finalized too. It won't be constructed, though > -- you have to call the constructor yourself, if you need to. > Most class prototypes don't need to be constructed, but that means if > JSCLASS_HAS_PRIVATE, JS_GetPrivate will return null for such objects, > so your finalize implementation has to null-check. Does this follow the stanard? 13.2 Creating Function Objects 9. Create a new object as would be constructed by the expression new Object(). 10. Set the constructor property of Result(9) to F. This property is given attributes { DontEnum }. 11. Set the prototype property of F to Result(9). This property is given attributes as specified in section 15.3.5.2. So my reading is that by default prototype should be set to new Object. Am I wrong? Eric Lippert from Microsoft seems to think so too (url: http://blogs.msdn.com/ericlippert/archive/2003/11/06/53352.aspx ) Well, let’s go back to our simple first example. I said above that since Car has no prototype assigned to it, we create a default prototype. During the creation of the default prototype, the interpreter assigns Car to Car.prototype.constructor. That might be a little confusing, so let's look at some pseudocode. This: function Car(){} logically does the same thing as var Car = new Function(); Car.prototype = new Object(); Car.prototype.constructor = Car; > You'll have to debug a bit more to say why you see two finalizes other > than the one for the instance you create explicitly in JS, via new. > Perhaps you JS_InitClass in two different global objects? JS_InitClass is called once. The problem was with my constructor. It looked like: JSBool ctor(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { JSObject * o = JS_NewObject(cx, &kls, NULL, NULL); *rval = OBJECT_TO_JSVAL(obj); return JS_TRUE; } and thus there are two objects: one created by the engine (passed as obj) and another created by me with JS_NewObject. But it raises another questions: is JS_InstanceOf(cx, obj, &kls, argv) guaranteed when calling constructor? with the best regards, anton. .