Subj : Spidermonkey: Need a 2nd set of eyes to find what I did wrong To : netscape.public.mozilla.jseng From : "Izman" Date : Mon Sep 15 2003 01:44 pm Thank you in advance to anyone who sees my mistake and points it out to me. I followed the docs at this link: http://sourceforge.net/docman/display_doc.php?docid=10853&group_id=50913. I have the js engine embedded in a C++ app and almost everything works ok. The only issue I have is with an object I have created. I can create the object in a script (i.e. "var myobj = new MyObject();"), I can access the first property, but I can not access the second property. Can someone give me a little help, I did something wrong and just don't see what it is. The code below from my app and is almost taken directly from the link above, with different names for variables obviously. The object has no methods and can get but not set property values. 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") I am missing something obvious I think, I just need some more eyes to look at it. Here is the code for the class: 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 }; 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); return newObj; } JSBool JSMyObject::JSConstructor(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { JSMyObject *p = new JSMyObject(); JS_SetPrivate(cx, obj, p); return JS_TRUE; } void JSMyObject::JSDestructor(JSContext *cx, JSObject *obj) { JSMyObject *p = (JSMyObject *)JS_GetPrivate(cx, obj); delete p; p = NULL; } JSPropertySpec JSMyObject::MyObject_properties[] = { { "prop_one", prop1, JSPROP_ENUMERATE }, { "prop_two", prop2, JSPROP_ENUMERATE }, { 0 } }; JSBool JSMyObject::JSGetProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp) { /* if (JSVAL_IS_INT(id)) { JSMyObject *priv = (JSMyObject *)JS_GetPrivate(cx, obj); switch(JSVAL_TO_INT(id)) { case prop1: break; case prop2: break; } }*/ *vp = id; return JS_TRUE; } JSFunctionSpec JSMyObject::MyObject_methods[] = { //{ "computeReduction", computeReduction, 1, 0, 0 }, { 0 } }; JSClass JSMyObject::MyObjectClass = { "MyObject", JSCLASS_HAS_PRIVATE, JS_PropertyStub, JS_PropertyStub, JSMyObject::JSGetProperty, JS_PropertyStub, JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JSMyObject::JSDestructor }; .