I've defined the following wrapped class in my C++ Node extension.
class JSStatus : public Nan::ObjectWrap {
public:
long statusCode;
std::string statusMessage;
std::string statusDetails;
static NAN_MODULE_INIT(Init);
static NAN_METHOD(New);
static NAN_GETTER(HandleGetters);
static NAN_SETTER(HandleSetters);
static Nan::Persistent<v8::FunctionTemplate> constructor;
};
The normal calling sequence goes like this (all on the Javascript thread):
I'm stuck on #5, as there doesn't seem to be a way to "new" a Nan::ObjectWrap in C++, then pass that object to Javascript.
This seems like something that would be common enough to be covered by NAN, but I've been unable to ascertain how to do it. Any ideas?
It would appear there's no way to "new" an object in C++ and pass it to Javascript. My workaround was to create a generic JS object and add fields to it one at a time. Klunky, but not too difficult with NAN.
// Send status to Javascript
void sendStatus(JSStatus* status) {
// Create callback parameters
const int argc = 1;
v8::Local<v8::Value> args[argc];
// Create generic JS object for status
v8::Local<v8::Object> jsObject = Nan::New<v8::Object>();
// Create property names & values for each field
v8::Local<v8::String> propName = Nan::New("statusCode").ToLocalChecked();
v8::Local<v8::Value> propValue = Nan::New(status->statusCode);
// Add field to JS object
Nan::Set(jsObject, propName, propValue);
// And again...
propName = Nan::New("statusMessage").ToLocalChecked();
propValue = Nan::New(status->statusMessage).ToLocalChecked();
Nan::Set(jsObject, propName, propValue);
// .... Etc., etc. for all the other fields ....
// Set parameter to JS object
args[0] = jsObject;
// Pass status to Javascript
v8::Local<v8::Value> jsReturnValue = jsStatusDelegate.Call(argc, args);
}