Search code examples
c++node.jsv8embedded-v8node.js-addon

how to convert node-addon Persistent<function> to Local<function>?


I'm trying to update a node.js addon using the old v8 API.

Here is my wrapper.cpp code:

std::map<int, Persistent<Function> > WrapMdUser::callback_map;

void WrapMdUser::FunCallback(CbRtnField *data) {
std::map<int, Persistent<Function> >::iterator cIt = callback_map.find(data->eFlag);
Local<Number> argv[1] = Nan::New(data->nReason);
cIt->second->Call(Nan::GetCurrentContext()->Global(), 1, argv);
}

If I understand it correctly, the author is using a map of Persistent<function> to store callbacks in it (see callback_map std), but when node-gyp build, the compiler throw this error:

wrapper.cpp:331:19: error: base operand of ‘->’ has non-pointer type ‘v8::Persistent<v8::Function>’
cIt->second->Call(Nan::GetCurrentContext()->Global(), 1, argv);

What is the best way to update this code to new v8 API so I can run it with last node version?

many thanks.


Solution

  • I found the answer in this article, the persistent needs to be converted to a local function first:

    Local<Function>::New(isolate, work->callback)->
    Call(isolate->GetCurrentContext()->Global(), 1, argv);
    

    Thanks to @Scott Frees.