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):
- Javascript calls MyExtension::UpdateStatus(callbackFunction)
- UpdateStatus() saves 'callbackFunction' for later use by SetStatus()
- UpdateStatus() calls a native library which returns status to a known, named method called SetStatus(NativeStatus)
- SetStatus() creates a 'JSStatus', copying values from nativeStatus
- SetStatus() passes the 'JSStatus' object to a known javascript function called StatusUpdated(JSStatus)
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.