c++ - 如何更新 Node.js native 插件以使用新的 API?

标签 c++ node.js node-native-addon

我的工作需要一个旧的 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/

相关文章:

c++ - 有关虚拟文件夹的文档

angularjs - 错误 : CSRF token missing, hackathon-starter 加 AngularJS

javascript - 无法加载资源:net::ERR_CONNECTION_REFUSED 在 NodeJs、AngularJs Web 应用程序上

php - 如何阻止 Webpack 文件名更改?

c++ - 如何在 MacOS 上的 NodeJS native 插件上创建 OpenGL 上下文?

c++ - ((MPI_Datatype)1) 在 C++ 中是什么意思?

c++ - 棘手的 InterlockedDecrement 与 CriticalSection

c++ - 有人可以解释这个 C++ 程序的输出吗?