Subj : Re: about defining object... To : netscape.public.mozilla.jseng From : celleris@naver.com (Youngsun Jeong) Date : Wed Oct 01 2003 03:02 am >>JSObject* InitMyobjectClass(JSContext *cx, JSObject *obj) >>{ >> JSObject *proto; >> >> proto = JS_InitClass(cx, obj, NULL, &my_class, NULL, 1, NULL, >>my_method, NULL,NULL); >> >There's your problem: you don't pass a non-null constructor parameter >(after &my_class). Yes, so I modified that. See below: JSObject* InitMyobjectClass(...) { ... proto = JS_InitClass(cx, obj, NULL, &my_class, MyObj, 1, NULL, my_method, NULL, NULL); ... } And I write a native constructor: //native constructor static JSBool MyObj(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { if(argc == 0) { obj = NULL; } else { if(!js_ValueToObject(cx, argv[0], &obj)) return JS_FALSE; } if(!obj) { JS_ASSERT(!argc || JSVAL_IS_NULL(argv[0]) || JSVAL_IS_VOID(argv[0])); if (cx->fp->flags & JSFRAME_CONSTRUCTING) return JS_TRUE; obj = js_NewObject(cx, &my_class, NULL, NULL); if (!obj) return JS_FALSE; } *rval = OBJECT_TO_JSVAL(obj); return JS_TRUE; } Is that right? What value of "obj", "argv" and "argc"? >>"my_class" has method "my_plusAge()", doesn't have any properties. >>And I add "InitMyobjectClass" to jsapi.c. See below: >> >>file name : jsapi.c >> ... >>#include "myobject.h" //added >> ... >>JS_PUBLIC_API(JSBool) >>JS_InitStandardClasses(JSContext *cx, JSObject *obj) >>{ >> ... >> return js_InitArrayClass(cx, obj) && >> js_InitBooleanClass(cx, obj) && >> js_InitMathClass(cx, obj) && >> js_InitNumberClass(cx, obj) && >> js_InitStringClass(cx, obj) && >> InitMyobjectClass(cx, obj) && //added >> >> >Please don't do that. There's absolutely no reason. In *your* code, >that calls JS_InitStandardClasses, just call your class init function >after JS_InitStandardClasses returns successfully. Ok, I deleted that code. >> ... >>} >>What is wrong? >> >> >See above, and more below. >>And I want to know how do make the relation between "age" and >>"plusAge()". >> >Where do you call JS_SetPrivate on new objects of your class, to give >them an "age" private datum? Since you pass NULL for the class >constructor, there's no chance for your code to associate an age with >each object via JS_SetPrivate. I don't know where should I call JS_SetPrivate because I don't know where "age" private datum is passed. Please tell me how do that. >What documentation are you studying, btw? I'm studying "mozilla/js/src/README.html" and "JS Embedder's Guide " on mozilla's js/spidermonkey page. .