我的工作需要一个旧的 Node.js native 插件,但由于许多 native API 被弃用,它不再适用于 Node.js 12 及更高版本。在数十个错误中,我已经修复了除一个之外的所有错误,该错误与初始化和调用回调函数有关。新 API 需要 4 个参数,而旧 API 有 3 个。这是损坏的代码:
void node_mpg123_feed_after (uv_work_t *req) {
Nan::HandleScope scope;
feed_req *r = (feed_req *)req->data;
Local<Value> argv[1];
argv[0] = Nan::New<Integer>(r->rtn);
Nan::TryCatch try_catch;
Nan::New(r->callback)->Call(Nan::GetCurrentContext()->Global(), 1, argv); //Compilation error in this line
// cleanup
r->callback.Reset();
delete r;
if (try_catch.HasCaught()) {
FatalException(try_catch);
}
}
具体来说,note the new API ,它使用 4 个参数,并且 contrast it with the old one ,只需要三个。我不知道要输入哪些参数,因为互联网上基本上不存在新 API 的教程,而且互联网上充满了旧的损坏的示例。
有人能指出我正确的方向吗?我收到的确切错误消息是我用上面的注释标记的行中的 error C2660: 'v8::Function::Call': function does not take 3 argument
。
最佳答案
浏览完 nan 变更日志后,阅读 nan sources ,我发现了一种新的调用回调的方式。具体来说,该行:
Nan::New(r->callback)->Call(Nan::GetCurrentContext()->Global(), 1, argv);
变成了
Nan::Call(Nan::New(r->callback), Nan::GetCurrentContext()->Global(), 1, argv);
关于c++ - 如何更新 Node.js native 插件以使用新的 API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59463310/