Subj : Re: updated jsgen (maybe rc1?) - beta 7 To : Marcello Bastea-Forte From : Brendan Eich Date : Sun Jan 04 2004 10:46 am Marcello Bastea-Forte wrote: >> Also, you're not worrying about thread safety -- do you plan to >> handle multiple threads some day? Threads are the main motivation >> for contexts; a single-threaded embedding might use more than one >> context (in fact, Mozilla's DOM does), but strictly speaking it need >> not, because there are no thread-safety hazards requiring more than >> one context. > > > I'm not sure how I should be worrying about that. Avoid mutable statics -- looks like you've done that (sorry I haven't had time to read your source). > >> But to get back to your question: there's no need to call >> JS_InitClass(cx, obj, ...) more than once per obj. > > > I know that there's no need, but it's more of a matter that my code > *might* call initclass multiple times. In beta 7, I'm now using the > following code: > > JSObject *JSTest::JSInit(JSContext *cx, JSObject *obj) { > if (obj==NULL) > obj = JS_GetGlobalObject(cx); > jsval oldobj; > if (JS_TRUE == JS_GetProperty(cx, obj, "JSTest", &oldobj) && > !JSVAL_IS_VOID(oldobj)) > return JSVAL_TO_OBJECT(oldobj); > return JS_InitClass(cx, obj, NULL, &JSTest::_jsClass, > JSTest::JSConstructor, 0, > JSTest::_JSPropertySpec, > JSTest::_JSFunctionSpec, > > JSTest::_JSPropertySpec_static, JSTest::_JSFunctionSpec_static); This is quite like what XBL does. One small improvement: use JS_LookupProperty instead of JS_GetProperty if your global object class's getter does any computation -- Lookup retrieves the slot-cached value of the property without calling the getter. Another nit: use JSTest::_jsClass.name instead of repeating "JSTest". Comments on JS_GetGlobalObject below. > >> Explicitly parameterize? The JS API has many entry points whose >> parameter lists begin (JSContext *cx, JSObject *obj...) and obj is >> the parameter independent of cx telling in which object to define a >> class or property, e.g. > > > I'm also using JS_GetGlobalObject to get around not having a JSObject > * all the time. I should probably add JSObject *obj as an optional > parameter on getJSObject(JSContext *cx). That's neither fish nor foul: better would be to avoid any dependence on JS_GetGlobalObject, or use only it, and insist that there be a 1:1 relationship between JSContext and global object. Mozilla's DOM does the latter, but it is heavier-weight than the former. /be .