Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

JS_InitClass issue

62 views
Skip to first unread message

Eliot

unread,
May 5, 2009, 3:13:28 PM5/5/09
to

I'm having issues with JS_InitClass
Basically - the class doesnt' seem to have a prototype.

This sample program shows the issue:
http://gist.github.com/107056

Any help would be appreciated.

-Eliot

Blake Kaplan

unread,
May 5, 2009, 6:42:08 PM5/5/09
to
Eliot <elioth...@gmail.com> wrote:
> Basically - the class doesnt' seem to have a prototype.

Eliot came on IRC to ask about this. The problem is confusion between the
class' constructor (passed into JS_InitClass) and the JSClass hook
'construct'.

For reference, the constructor function passed into JS_InitClass tells
SpiderMonkey to expose a function object named the same as the passed-in
class' name member and whose .prototype property is the class prototype (the
same one returned by JS_InitClass). If constructor is null, then the property
that is defined is the prototype object itself.

In contrast the class hook is called when someone attempts to construct an
instantiation of the given object. That is, given:

JSBool
construct(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
printf("In construct\n");
return JS_TRUE;
}

static JSClass clazz = {
"mine", 0,
JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
NULL, NULL, NULL, construct, NULL, NULL, NULL, NULL
};

JSBool
class_construct(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
jsval *rval)
{
printf("In class_construct\n");
return JS_TRUE;
}

JS_InitClass(cx, global, NULL, &clazz, class_construct,
0, NULL, NULL, NULL, NULL);

Then evaluating the JS:

m = new mine();
new m();

should output:

In class_construct
In construct
--
Blake Kaplan

0 new messages