【发布时间】:2026-02-21 07:25:01
【问题描述】:
使用 Spidermonkey v27:
“保留”然后从 C++ 异步调用临时 JS 函数的正确方法是什么?
JS代码:
myFunction(function(){
console.log("The function works");
});
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
}
我已经阅读了 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
并检查了这个 * 帖子Spidermonkey and Garbage Collection
甚至尝试让 JS 回调函数成为一个永远不应该被垃圾回收的全局函数。 (但这不是我想要的。)
【问题讨论】:
-
你有没有考虑加入 mozilla irc? wiki.mozilla.org/IRC我怀疑那里的人会立即为您提供帮助。
标签: javascript c++ spidermonkey