[英]How to statically store and call a node.js callback function
I have a node.js addon that I developped for node 0.10 and I need to update it for the current node.我有一个为节点 0.10 开发的 node.js 插件,我需要为当前节点更新它。 I fixed most of the api changes but I'm stuck with the issue of storing a callback pointer statically for later call.
我修复了大部分 api 更改,但我遇到了静态存储回调指针以供以后调用的问题。 I modified 3_callback example addon to show what I did so far and the error I get during compile.
我修改了 3_callback 示例插件以显示我到目前为止所做的事情以及我在编译过程中遇到的错误。 This is what I have so far (the compile error is added as commented lines):
这是我到目前为止所拥有的(编译错误添加为注释行):
#include <node.h>
using namespace v8;
typedef Persistent<Function> CallBack_t;
static CallBack_t MyCallBack;
void CallBack( char* str ) {
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope( isolate );
const unsigned argc = 1;
Local<Value> argv[ argc ] = { String::NewFromUtf8( isolate, str ) };
Local<Function> cb = Local<Function>::New( isolate, MyCallBack );
cb->Call( isolate->GetCurrentContext()->Global(), argc, argv );
}
void RunCallback( const FunctionCallbackInfo<Value>& args ) {
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope( isolate );
Local<Function> cb = Local<Function>::Cast( args[ 0 ] );
MyCallBack = CallBack_t::New( isolate, cb );
//..\addon.cc( 24 ) : error C2664 : 'v8::Function *v8::PersistentBase<T>::New(v8::Isolate *,T *)' : cannot convert argument 2 from 'v8::Local<v8::Function>' to 'v8::Function *' [E:\SoftwareProjects\node-addon-examples\trunk\3_global_callback\node_0.12\build\addon.vcxproj]
// with
// [
// T = v8::Function
// ]
//..\addon.cc( 24 ) : note : No user - defined - conversion operator available that can perform this conversion, or the operator cannot be called CallBack( "JavaScriptCallBack calling" );
}
void Init( Handle<Object> exports, Handle<Object> module ) {
NODE_SET_METHOD( module, "exports", RunCallback );
}
NODE_MODULE( addon, Init )
I tried different alternative but none works so far.我尝试了不同的替代方案,但到目前为止都没有奏效。 Where is the error?
错误在哪里?
Addressing the error in your code:解决代码中的错误:
cannot convert argument 2 from 'v8::Local<v8::Function>' to 'v8::Function *'
This should fix it:这应该解决它:
v8::Local<v8::Function> cb = std::Local<v8::Function>::Cast(args[0]);
v8::Function * f_ptr = *cb;
. .
For Statically Storing and Calling a JS-Callback Function in Node V8 Version 4.XX:在 Node V8 版本 4.XX 中静态存储和调用 JS-Callback 函数:
callbacks.cxx:回调.cxx:
v8::Persistent<v8::Function> r_call;
/** Save the JS-Callback for later use.
* If args[0] is not a function (such as null), remove callback
*/
void RunCallback(const v8::FunctionCallbackInfo<v8::Value>& args) {
v8::Isolate* isolate = args.GetIsolate();
if (args[0]->IsFunction()) {
v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(args[0]);
v8::Function * ptr = *func;
r_call.Reset(isolate, func);
}
else {
r_call.Reset();
}
}
/* Run the saved callback if there is one. */
void run() {
if(!r_call.IsEmpty()) {
v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::Local<v8::Function> func = v8::Local<v8::Function>::New(isolate, r_call);
if (!func.IsEmpty()) {
const unsigned argc = 1;
v8::Local<v8::Value> argv[argc] =
{ v8::String::NewFromUtf8(isolate, "hello world") };
func->Call(v8::Null(isolate), argc, argv);
}
}
}
test.js:测试.js:
const mod = require("...");
mod.RunCallback((msg) => { console.log(msg); });
mod.run();
-- tested with: node-gyp v4.4.4, MSVS2013, SWIGv3.0.10 -- -- 测试:node-gyp v4.4.4、MSVS2013、SWIGv3.0.10 --
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.