Question

I'm using SpiderMonkey JS Engine 1.8.5 API.

JS code looks like:

visitChildren(function() {
                             log("something");
                         });

VisitChildren is something like:

JSBool visit_children(JSContext* cx, uintN argc, jsval *vp) {

    JSFunction* function;
    if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, vp), "f", &function))
        return JS_FALSE;

    jsval retVal;
    JS_CallFunction(cx, JS_GetGlobalObject(cx), function, 0, NULL, &retVal);

    return JS_TRUE;
}

I have intentionally simplified code to make question more clear. Definitely I'm doing something absolutely wrong, could someone suggest me why call to *JS_CallFunction* crashes application ?

UPD1:

Found some vague info here https://developer.mozilla.org/en-US/docs/SpiderMonkey/JSAPI_Reference/JSFunction but still crashes:

jsval functionVal = OBJECT_TO_JSVAL(JS_GetFunctionObject(function));
JS_CallFunctionValue(cx, JS_GetGlobalObject(cx), functionVal, 0, NULL, &retVal);
Was it helpful?

Solution

What you want is JS_CallFunctionValue[1] instead. After testing the type (your JS_ConvertArguments call), ignore the resulting JSFunction of the conversion and use the actual jsval instead. I've not found a use yet for JSFunctions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top