Subj : Re: Properties and Objects To : netscape.public.mozilla.jseng From : eboli@hotdak.net (Joseph Smith) Date : Wed Dec 03 2003 04:13 am Hey, I give up :) I am willing to do this any way possible i am just running out of time to get it done, i have read documentation and am still not getting what i am missing, so I created the following two classes *mostly* as per an online tutorial {parent, child}; Parent has an array of JSChilderen, when its created it creates a bunch of new JSChilderen, and i am trying to use getProperty() to simply tell the parent object to call the child objects output function (ie from a script, var p = new Parent(); p.child.output;) I have included the classes to show you what i am trying to do, if you can explain how to pass in this fashion would be great help :) Also if you can exain how this happens can you also show me how to go from the static m_Childeren[0] that i am using for testing purposes, into an index generated from the calling function (ie var p = new Parent; p.child[40000].output) for example??? class JSChild { public: JSChild() { } virtual ~JSChild() { } static JSBool JSGetProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp); static JSBool JSSetProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp); static JSBool JSConstructor(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval); static void JSDestructor(JSContext *cx, JSObject *obj); static JSObject* JSInit(JSContext *cx, JSObject *obj, JSObject *proto = NULL); static JSClass Child_class; static JSBool OutputDebugString(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval); private: static JSPropertySpec Child_properties[]; static JSFunctionSpec Child_methods[]; enum { }; }; JSPropertySpec JSChild::Child_properties[] = { { 0 } }; JSFunctionSpec JSChild::Child_methods[] = { { "output", OutputDebugString, 0, 0, 0 }, { 0, 0, 0, 0, 0 } }; JSClass JSChild::Child_class = { "Child", JSCLASS_HAS_PRIVATE, JS_PropertyStub, JS_PropertyStub, JSChild::JSGetProperty, JSChild::JSSetProperty, JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JSChild::JSDestructor }; JSBool JSChild::JSConstructor(JSContext *cx, JSObject *obj, uintN, jsval *, jsval *) { JS_SetPrivate(cx, obj, (void*)new JSChild); return JS_TRUE; } void JSChild::JSDestructor(JSContext *cx, JSObject *obj) { delete (JSChild*)JS_GetPrivate(cx, obj); } JSObject *JSChild::JSInit(JSContext *cx, JSObject *obj, JSObject *proto) { JSObject *newProtoObj = JS_InitClass(cx, obj, proto, &Child_class, JSChild::JSConstructor, 0, NULL, JSChild::Child_methods, NULL, NULL); JS_DefineProperties(cx, newProtoObj, JSChild::Child_properties); return newProtoObj; } JSBool JSChild::JSGetProperty(JSContext *, JSObject *, jsval, jsval *) { return JS_TRUE; } JSBool JSChild::JSSetProperty(JSContext *, JSObject *, jsval , jsval *) { return JS_TRUE; } JSBool JSChild::OutputDebugString(JSContext *, JSObject *, uintN, jsval *, jsval *) { #ifdef __WXMSW__ ::OutputDebugString(wxT("Testing\n")); #else fputs("Testing", stderr); #endif return JS_TRUE; } class JSParent { public: JSParent() { for (int i = 0; i < 10; i++) { m_Childeren[i] = new JSChild(); } } virtual ~JSParent() { } JSChild* getJSChild(int index) { return m_Childeren[index]; } static JSBool JSGetProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp); static JSBool JSSetProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp); static JSBool JSConstructor(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval); static void JSDestructor(JSContext *cx, JSObject *obj); static JSObject* JSInit(JSContext *cx, JSObject *obj, JSObject *proto = NULL); static JSClass Parent_class; private: JSChild* m_Childeren[10]; static JSPropertySpec Parent_properties[]; static JSFunctionSpec Parent_methods[]; enum { child_prop }; }; JSPropertySpec JSParent::Parent_properties[] = { { "child", child_prop, JSPROP_ENUMERATE }, { 0 } }; JSFunctionSpec JSParent::Parent_methods[] = { { 0, 0, 0, 0, 0 } }; JSClass JSParent::Parent_class = { "Parent", JSCLASS_HAS_PRIVATE, JS_PropertyStub, JS_PropertyStub, JSParent::JSGetProperty, JSParent::JSSetProperty, JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JSParent::JSDestructor }; JSBool JSParent::JSConstructor(JSContext *cx, JSObject *obj, uintN, jsval *, jsval *) { JS_SetPrivate(cx, obj, (void*)new JSParent); return JS_TRUE; } void JSParent::JSDestructor(JSContext *cx, JSObject *obj) { delete (JSParent*)JS_GetPrivate(cx, obj); } JSObject *JSParent::JSInit(JSContext *cx, JSObject *obj, JSObject *proto) { JSObject *newProtoObj = JS_InitClass(cx, obj, proto, &Parent_class, JSParent::JSConstructor, 0, NULL, JSParent::Parent_methods, NULL, NULL); JS_DefineProperties(cx, newProtoObj, JSParent::Parent_properties); return newProtoObj; } JSBool JSParent::JSGetProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp) { if (JSVAL_IS_INT(id)) { JSParent *p = (JSParent *) JS_GetPrivate(cx, obj); p=p; // Keep compiler happy switch (JSVAL_TO_INT(id)) { case child_prop: { // How do i return our m_childeren[index] object??? // What i tried before to no avail using static [index] will eventually use a wxHashArray or something to that extent *vp = OBJECT_TO_JSVAL(p->getJSChild(0)); } } } else { return JS_FALSE; } return JS_TRUE; } JSBool JSParent::JSSetProperty(JSContext *, JSObject *, jsval , jsval *) { return JS_TRUE; } Thanks again for at least reading and any help you may offer, and I also finally picked up on the point you were making reguarding the use of [] for an array of objects vs () :) (WHACKS SELF IN THE HEAD) :) Joseph .