简体   繁体   中英

How do I update a Node.js native addon to use the new API?

There's an old Node.js native addon that I need for my work, but no longer works for Node.js 12 and above because of deprecation of many native APIs. Out of dozens of errors, I have fixed all but one, which has to do with initializing and calling a callback function. The new API requires 4 arguments, while the old one had three. This is the broken code:

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);
  }
}

Specifically, note the new API , which uses 4 arguments, and contrast it with the old one , which needs only three. I can't figure out what parameters to put in, since essentially no tutorials exist on the internet for the new API, and the internet is filled with examples of the old broken one.

Can anyone point me in the right direction? The exact error message I am getting is error C2660: 'v8::Function::Call': function does not take 3 arguments in the line I marked with the comment above.

After going through the nan changelog reading the nan sources , I found a new way of calling callbacks. Specifically, the line:

Nan::New(r->callback)->Call(Nan::GetCurrentContext()->Global(), 1, argv);

becomes

Nan::Call(Nan::New(r->callback), Nan::GetCurrentContext()->Global(), 1, argv);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM