0

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);
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • I don't know C++ much, but what happens if you set jsval to nullptr. – fflorent Dec 30 '12 at 18:10
  • Otherwise, you might be interested in taking a look at this: http://dxr.mozilla.org/search.cgi?tree=mozilla-central&request_time=1356890964124&string=JS_CallFunction – fflorent Dec 30 '12 at 18:11

1 Answers1

1

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.

Jeff Hubbard
  • 9,822
  • 3
  • 30
  • 28