0

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.

ZachB
  • 13,051
  • 4
  • 61
  • 89
Sulliwane
  • 410
  • 6
  • 16
  • 1
    Pretty sure this issue is covered here: https://stackoverflow.com/questions/13826803/calling-javascript-function-from-a-c-callback-in-v8/28554065#28554065 – Scott Frees Jun 13 '16 at 19:27

1 Answers1

0

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.

Sulliwane
  • 410
  • 6
  • 16