简体   繁体   中英

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. I fixed most of the api changes but I'm stuck with the issue of storing a callback pointer statically for later call. I modified 3_callback example addon to show what I did so far and the error I get during compile. 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:
callbacks.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:

const mod = require("...");
mod.RunCallback((msg) => { console.log(msg); });
mod.run();

-- tested with: node-gyp v4.4.4, MSVS2013, SWIGv3.0.10 --

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM