Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass object to JavaScript callback in V8

I'm working on a Node module, and am trying to pass an instance of a class that subclasses ObjectWrap as an argument to a JavaScript callback.

In other places I've been able to successfully unwrap JavaScript objects to the same class, using:

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

How might I do the reverse? I want to pass an instance of GitCommit to a JavaScript callback, like:

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 this possible? If so would someone please give me an example, or a link to the relevant documentation?

like image 444
Michael Robinson Avatar asked Oct 22 '22 16:10

Michael Robinson


1 Answers

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);
like image 199
Chen Avatar answered Oct 27 '22 10:10

Chen