I would like to be able to instantiate an instance of a C++ class and pass it as an argument to a JavaScript callback. The class inherits from ObjectWrap, and is available to and used in Node.js.
I am able to pass instances of the same class to the callback if said instance came from JavaScript.
This is a basic example of what I need to do:
MyClass *instance = new MyClass();
// If was passed as the first param to this method, commit is set as below, this code works.
// MyClass *instance = ObjectWrap::Unwrap<MyClass>(args[0]->ToObject());
// Assign some property values to commit
Handle<Value> argv[] = { instance->handle_ };
ar->callback->Call(Context::GetCurrent()->Global(), 1, argv);
This doesn't work, instead it segfaults. gdb tells me:
Program received signal EXC_BAD_ACCESS, Could not access memory.
How might I modify my code to allow me to pass my instance to the callback?
Update: the class in question may be viewed on Github: commit.cc.
The answer is that I was doing it wrong.
The correct code:
Local<Value> instance = MyClass::constructor_template->NewInstance();
// Assign some property values to commit
Handle<Value> argv[] = { instance };
ar->callback->Call(Context::GetCurrent()->Global(), 1, argv);
To get the MyClass instance:
MyClass *instanceOfMyClass = ObjectWrap::Unwrap(instance);