Subj : Re: Spidermonkey: parameters to 'construction' function To : Peter Paulus From : Brendan Eich Date : Mon Apr 19 2004 10:57 am Peter Paulus wrote: > This means the 'constructor' function boilerplate may look like this. > > JSBool Document::createDocument(JSContext* context, JSObject* object, > uintN argc, jsval* argv, jsval* result) > { > JSBool ok = JS_FALSE; > > JSObject* jsDocument = 0L; > if (JS_IsConstructing(context)) > { > jsDocument = JSVAL_TO_OBJECT(*result); // or jsDocument = object; You could use |obj| instead of |*result| here -- it's faster and it is better documented, as well as being to my mind nicer looking. Either way works and will continue to work, though, so it's your choice. > } > else > { > jsDocument = JS_NewObject(context, &Document::clazz, NULL, NULL); Check for null return, if so, return JS_FALSE immediately to propagate the out-of-memory error. > *result = OBJECT_TO_JSVAL(jsDocument); > } > > if (jsDocument) No need for this test if you return early on null. > { > void* hostDocument = new ...; > ok = JS_SetPrivate(context, jsDocument, hostDocument); And that allows you to return JS_SetPrivate(...); here, eliminating the useless |ok| local variable. /be .