How to pass an object to a JavaScript callback in V8

I am working on a Node module and trying to pass an instance of a class that subclasses ObjectWrapas an argument to a JavaScript callback.

In other places, I was able to successfully decompress JavaScript objects into one class using:

 GitCommit *commit = ObjectWrap::Unwrap<GitCommit>(args[0]->ToObject());

How can I do the opposite? I want to pass an instance GitCommitto a JavaScript callback, for example:

Local<Value> argv[] = {
  // Error code
  Local<Value>::New(Integer::New(0)),
  // The commit
  commit // Instance of GitCommit : ObjectWrap
};

// Both error code and the commit are passed, JS equiv: callback(error, commit)    
ar->callback->Call(Context::GetCurrent()->Global(), 1, argv);

Is it possible? If so, please give me an example or a link to the relevant documentation?

+5
source share
1 answer

So you are writing a node addon. Try:

Handle<Value> argv[] = {
    // Error code
    Integer::New(0),
    // The commit
    commit->handle_ // Instance of GitCommit : ObjectWrap
};

// Both error code and the commit are passed, JS equiv: callback(error, commit)    
ar->callback->Call(Context::GetCurrent()->Global(), 1, argv);
+3
source

All Articles