如何将节点插件 Persistent<function> 转换为 Local<function>?
how to convert node-addon Persistent<function> to Local<function>?
我正在尝试使用旧的 v8 API 更新 node.js 插件。
这是我的 wrapper.cpp
代码:
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);
}
如果我没理解错的话,作者是在使用Persistent<function>
的映射来存储回调(参见callback_map
std),但是当node-gyp build
时,编译器会抛出这个错误:
wrapper.cpp:331:19: error: base operand of ‘->’ has non-pointer type ‘v8::Persistent<v8::Function>’
cIt->second->Call(Nan::GetCurrentContext()->Global(), 1, argv);
将此代码更新到新的 v8 API 以便我可以 运行 使用上一个节点版本的最佳方法是什么?
非常感谢。
我在这个article中找到了答案,持久化需要先转换为本地函数:
Local<Function>::New(isolate, work->callback)->
Call(isolate->GetCurrentContext()->Global(), 1, argv);
感谢@Scott Frees。
我正在尝试使用旧的 v8 API 更新 node.js 插件。
这是我的 wrapper.cpp
代码:
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);
}
如果我没理解错的话,作者是在使用Persistent<function>
的映射来存储回调(参见callback_map
std),但是当node-gyp build
时,编译器会抛出这个错误:
wrapper.cpp:331:19: error: base operand of ‘->’ has non-pointer type ‘v8::Persistent<v8::Function>’
cIt->second->Call(Nan::GetCurrentContext()->Global(), 1, argv);
将此代码更新到新的 v8 API 以便我可以 运行 使用上一个节点版本的最佳方法是什么?
非常感谢。
我在这个article中找到了答案,持久化需要先转换为本地函数:
Local<Function>::New(isolate, work->callback)->
Call(isolate->GetCurrentContext()->Global(), 1, argv);
感谢@Scott Frees。