Spidermonkey api: object function returning an object

317 views Asked by At

my native function should return an object but it returns the called function object. I am using the spidermonkey jsapi to extend my application. I define several classes which are proxies to their c++ counterparts. The usage is like a main object, called 'mx' used to create objects of type 'widget' using a function named 'widget'

Each application object has some kind of ID which can be passed to the js object constructor as an argument. In spidermonkey 1.8.5 i could create a js widget object using the mx widget method

function on_trigger( triggervalue )
{
w = mx.widget("somename");
print_console("mx is " + mx );
print_console("w is " + w );
}

But mx.widget("somename") does not return an object of type widget, it returns the function object:

mx is [object mx]
w is function widget() {
    [native code]
}

This is how the jsapi classes and functions are implemented:

JSBool mx_wrapper_getwidget(JSContext* cx, unsigned argc, jsval* argv)
{
    JSObject *global = JS_GetGlobalForScopeChain(cx);

    jsval v;
    JS_GetProperty(cx, global, widget_wrapper_class.name, &v);

    JS::CallArgs args = JS::CallArgsFromVp(argc, argv);
    JSObject *w = JS_New(cx, JSVAL_TO_OBJECT(v), argc, args.array());
    args.rval().setObject( *w );

    return JS_TRUE;
}

static JSFunctionSpec mx_wrapper_methods[] =
{
  JS_FS("widget", mx_wrapper_getwidget, 1, 0),
  JS_FS_END
};

JS_InitClass(cx, global, NULL, &mx_wrapper_class, mx_wrapper_cons, 0, mx_wrapper_properties, mx_wrapper_methods, NULL, NULL)
JS_DefineObject(cx, global, "mx", &mx_wrapper_class, mx_wrapper_proto, 0);

Why does JSBool mx_wrapper_getwidget() return the function object and not the newly created object?

All the best Heiner

1

There are 1 answers

0
Heiner On BEST ANSWER

answering myself: among various other minor issues i did not define my class properly

wrong (widget_wrapper_finalize, JSCLASS_NO_OPTIONAL_MEMBERS missing):

static JSClass widget_wrapper_class =
{
    MX_JS_WIDGET_CLASSNAME, JSCLASS_HAS_PRIVATE,
    JS_PropertyStub, JS_DeletePropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
    JS_EnumerateStub, JS_ResolveStub, NULL
};

right:

static JSClass widget_wrapper_class =
{
    MX_JS_WIDGET_CLASSNAME, JSCLASS_HAS_PRIVATE,
    JS_PropertyStub, JS_DeletePropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
    JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, widget_wrapper_finalize, JSCLASS_NO_OPTIONAL_MEMBERS
};