Skip to content

nodejs addon, how to invoke the registered callback function #454

Description

@dreamyzhang
  • Node.js Version:6.9
  • OS:linux
  • Scope (install, code, runtime, meta, other?):addon
  • Module (and version) (if relevant):addon

Like this problem here: [http://stackoverflow.com/questions/40856548/no-matching-function-for-call-to-persistentfunctionnewisolate-localfunct][1] , but when I using the callback function, it alaways coredump.

    class test : public node::ObjectWrap
    {
	    public:
		    void start();
		    static void on(const v8::FunctionCallbackInfo<v8::Value>& args);
		    std::map<string, v8::Persistent<v8::Function> > callback;   
    }

    //Register callback function
    void test::On(const FunctionCallbackInfo<Value>& args)
    {
	    Isolate* isolate = args.GetIsolate();
	    Local<String> eventName = args[0]->ToString();         
	    Local<Function> cb = Local<Function>::Cast(args[1]);   
	    String::Utf8Value eNameUtf8(eventName); 
	    callback[*eNameUtf8].Reset(isolate, cb);
    }                                                        

    void test::start()
    {
	    Isolate* isolate = Isolate::GetCurrent();
	    for(typeof(callback.begin()) it=callback.begin(); it!=callback.end(); it++)
	    {                                                                     
		    Local<Value> argv[1] = { Local<Value>::New(isolate, String::New(isolate, itr->first)) };                                                
		    //How to call the registered callback function just now?
		    //will coredump on here
		    it->second.Call(Null(isolate), 1, argv);                                                          
	    }
    }


    //test.js
    var t = require('bindings')('test')
    t.on("test1", function(){})
    t.on("test2", function(){})
    t.start()

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions