Subj : Re: Spidermonkey: parameters to 'construction' function To : Peter Paulus From : Brendan Eich Date : Fri Apr 16 2004 12:01 pm Peter Paulus wrote: > // Class initialization function > void Document::initialize(::javascript::core::Context& context) > { > _prototype = context.registerClazz(Node::_prototype, &clazz, > &createDocument, 0, (JSPropertySpec*) &objectProperties, > (JSFunctionSpec*) &objectMethods, (JSPropertySpec*) &classProperties, > (JSFunctionSpec*) &classMethods); Why all the casts? Are you casting away const? > As far as I understand this my Document::createDocument() method is > invoked by the following JavaScript statement: > > 1) var document = new w3cDOM·Document(); > > aswell as > > 2) var document = w3cDOM·Document(); > > In case 1 both the JSObject* 'object' parameter and the jsval* 'result' > parameter both point to the same instance of an w3cDOM·Document class. > > I've verified this as follows: > > JSClass* objectClass = JS_GetClass(object); > cout << objectClass->name << "\n"; Right. You can use JS_IsConstructing(cx) to distinguish the two cases, by the way. > In case 2 the JSObject* 'object' parameter and jsval* 'result' differ. > In this case the 'object' parameter points to the 'global' object that > got registered with the runtime for this context. > > The 'result' parameter seems to be a JS_IS_VOID parameter. You mean JSVAL_VOID. > Now JS_SetPrivate(...) fails with the assertion: > Assertion failed: OBJ_GET_CLASS(cx, obj)->flags & JSCLASS_HAS_PRIVATE, > at jsapi.c:1404. This is correct since my 'global' object has no flags > specified in JSClass.flags. > > In both cases JS_IsConstructing(context) is true. (When will this be > false??). Not so. JS_IsConstructing(cx) returns false for case 2. Please show a *minimal* testcase, preferably based on js.c, demonstrating otherwise. But there is no such bug in the engine; many built-in constructors, such as Date and Array, test the same predicate that JS_IsConstructing tests in order to do something different when called as a function. > Would it be a correct approach the define a 'constructor' function as > follows: No. You are writing unnecessary code to work around a bug in your embedding, or in the version of the engine you are using. There is no need to test JSVAL_IS_VOID(*rval) once you know JS_IsConstructing(cx) evaluates to true. /be .