Nat*_*iss 5 javascript c++ spidermonkey
使用Spidermonkey v27:
什么是"保留"然后从C++异步调用临时JS函数的正确方法?
JS代码:
myFunction(function(){
console.log("The function works");
});
Run Code Online (Sandbox Code Playgroud)
C++代码:
bool js_myFunction(JSContext* cx, uint32_t argc, jsval* vp)
{
if (argc == 1)
{
implement_async_function(cx, vp);
JS_SET_RVAL(cx, vp, JSVAL_NULL);
return true;
}
return false;
}
static JSContext* savedContext;
static jsval* savedVal;
void implement_async_function(JSContext* cx, jsval* vp)
{
// if the function is called immediately, everything is okay!
jsval retVal;
JS_CallFunctionValue(cx, nullptr, *vp, 0, nullptr, &retVal);
// "The function works"
// if some work has to be done before calling the callback...
savedContext = cx;
savedVal = vp;
start_async_process();
}
void async_process_complete()
{
jsval retVal;
JS_CallFunctionValue(savedContext, nullptr, *savedVal, 0, nullptr, &retVal);
// "<no filename="filename">:0:true is not a function"
// or else it crashes...
}
void alternate_implement_async_function(JSContext* cx, jsval* vp)
{
// also tried this:
savedContext = cx;
savedVal = vp;
JS_AddValueRoot(savedContext, savedVal);
start_async_process();
// and this:
savedContext = cx;
savedVal = new jsval(*vp);
JS_AddValueRoot(savedContext, savedVal);
start_async_process();
// and using JS::RootedValue
// and using JS::Heap<JS::Value>
// and using the global context instead of the saved context
}
Run Code Online (Sandbox Code Playgroud)
我已经阅读了SpiderMonkey的文档:
https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/JSAPI_Reference/JS::Value
https://developer.mozilla.org/en-US/docs/SpiderMonkey/GC_Rooting_Guide
https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/JSAPI_reference/JS_AddRoot
https://developer.mozilla.org/en-US/docs/SpiderMonkey/JSAPI_Reference/JS_CallFunctionValue
并检查了这个StackOverflow后Spidermonkey和垃圾收集
并尝试甚至使JS回调函数成为一个永远不会被垃圾收集的全局函数.(这不是我想要的.)