Sulliwane Sulliwane - 1 year ago 131
Node.js Question

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


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
to store callbacks in it (see
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.

Answer Source

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.