Subj : Re: Spidermonkey: Need a 2nd set of eyes to find what I did wrong To : netscape.public.mozilla.jseng From : "Izman" Date : Tue Sep 16 2003 11:21 am Thank you for your help Brendan. I am using the win32 debug build of js-1.5-rc5a.tar.gz file downloaded from ftp://ftp.mozilla.org/pub/js/. I tried using the win32 release build but it crashes my app every time I try and use a script, even though the app and js32.dll compile with no errors or warnings. I use VS6 and compile through the IDE. I did make the changes you suggested and will document them at the end of this message. The results I receive this time are identical to before (with the exception that prop_one is returning -1 instead of 0 as per the new enum values). Changes: Added decreasing negative values to the property enum: enum { prop1 = -1, prop2 = -2, }; Removed the JS_DefineProperties statement and changed first NULL in the JS_InitClass statement to define properties from MyObject_properties: JSObject *JSMyObject::JSInit(JSContext *cx, JSObject *obj, JSObject *proto) { JSObject *newObj = JS_InitClass(cx, obj, proto, &MyObjectClass, JSMyObject::JSConstructor, 0, JSMyObject::MyObject_properties, JSMyObject::MyObject_methods, NULL, NULL); //JS_DefineProperties(cx, newObj, JSMyObject::MyObject_properties); return newObj; } Added error checking to the constructor and set rval to the obj: JSBool JSMyObject::JSConstructor(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { JSMyObject *p = new JSMyObject(); if (!p) return JS_FALSE; if(!(JS_SetPrivate(cx, obj, p))) return JS_FALSE; *rval = OBJECT_TO_JSVAL(obj); return JS_TRUE; } Removed the trivial "p = NULL;" statement: void JSChar::JSDestructor(JSContext *cx, JSObject *obj) { JSChar *p = (JSChar *)JS_GetPrivate(cx, obj); delete p; //p = NULL; } "Brendan Eich" wrote in message news:3F6646D4.7030000@meer.net... > Izman wrote: > > >When running a script that accesses the properties: > > > > The value returned from JSGetProperty is 0 when accessing prop1 (as expected). (i.e. "var a = myobj.prop_one;", a ends up with the value 0) > > The value returned from JSGetProperty is "prop_two" when accessing prop2 (should return 1 as per the enum and property definitions). (i.e. "var b = myobj.prop_two;", b ends up with the value "prop_two") > > > > > > What version of the engine are you using? > > >I am missing something obvious I think, I just need some more eyes to look at it. > > > >Here is the code for the class: > > > > > > This seems based directly on the docs at > http://sourceforge.net/docman/display_doc.php?docid=10853&group_id=50913 > as you say. Cc'ing Franky at the last address I have for him, so he can > make corrections per below. > > >class JSMyObject > >{ > >public: > > JSMyObject() { > > m_CMyObject = new CMyObject(); > > } > > > > ~JSMyObject() { > > delete m_CMyObject; > > m_CMyObject = NULL; > > } > > > > static JSClass MyObjectClass; > > > > static JSBool JSConstructor(JSContext *cx, JSObject *obj, uintN argc, > >jsval *argv, jsval *rval); > > static void JSDestructor(JSContext *cx, JSObject *obj); > > > > static JSBool JSGetProperty(JSContext *cx, JSObject *obj, jsval id, jsval > >*vp); > > //static JSBool JSSetProperty(JSContext *cx, JSObject *obj, jsval id, jsval > >*vp); > > > > static JSObject *JSInit(JSContext *cx, JSObject *obj, JSObject *proto); > > > > static JSPropertySpec MyObject_properties[]; > > static JSFunctionSpec MyObject_methods[]; > > > > enum { > > prop1, > > prop2 > > }; > > > > > > Giving tinyids negative values, starting from -1 and decreasing, is > traditional, and allows the object to handle non-negative integer > property ids (AKA array indexes) for different properties than these > tinyid-named ones defined via the JSPropertySpec array. > > >protected: > > CMyObject *getCMyObject() { > > return m_CMyObject; > > } > > > >private: > > CMyObject *m_CMyObject; > >}; > > > >JSObject *MyObject::JSInit(JSContext *cx, JSObject *obj, JSObject *proto) > >{ > > JSObject *newObj = JS_InitClass(cx, obj, proto, &MyObjectClass, > > JSMyObject::JSConstructor, 0, > > NULL, JSMyObject::MyObject_methods, > > NULL, NULL); > > JS_DefineProperties(cx, newObj, JSMyObject::MyObject_properties); > > > > > > This is silly, the JSMyObject::MyObject_properties array should be > passed to JS_InitClass instead of the first NULL actual parameter, for > the |ps| formal parameter. That will define the properties, just as if > you called JS_DefineProperties on the return value (the prototype object > for the class). There is no need for callers to do this work. > > > return newObj; > >} > > > >JSBool JSMyObject::JSConstructor(JSContext *cx, JSObject *obj, uintN argc, > > jsval *argv, jsval *rval) > >{ > > JSMyObject *p = new JSMyObject(); > > JS_SetPrivate(cx, obj, p); > > > > > > This code should check for errors. > > More important, it must set *rval = OBJECT_TO_JSVAL(obj); in order that > 'myobj = new MyObject' to work correctly). > > > return JS_TRUE; > >} > > > >void JSMyObject::JSDestructor(JSContext *cx, JSObject *obj) > >{ > > JSMyObject *p = (JSMyObject *)JS_GetPrivate(cx, obj); > > delete p; > > p = NULL; > > > > > > There's on point in nulling p, as it is about to go out of scope. > > I'm not sure what else is wrong, but please correct the above and let us > know how things go. > > /be > .