Sunday, 9 April 2017

What is the simplest way to return return opaque data from C++ to js?

I'm trying to navigate the perilous waters of inconsistent and poorly documented C++ resources for writing native nodejs modules and struggling with the simplest proof-of-concept attempts. The only code which even compiles comes not from actual documentation or tutorials, but from some few working real modules that do way too much and still break the moment I try to adjust or trim away anything. Additionally, I've yet to see the same pattern of usage twice, so I'm at a loss to infer anything reliable from code that does work. (I am not a C++ developer and have barely touched the language, but the problem seems to revolve around all the infrastructure of Node/V8.)

Thus, I am in desperate need of code samples that are actually relevant and focused, and not laden with scads of boilerplate that don't even get explained or justified.

So, what is the simplest way I can put something (such as a struct) in an object, give the object to javascript, and ensure the object stays alive (only) until javascript drops it? I feel like this couldn't be a simpler request. I don't need methods, API exposure, ability to instantiate on js side, of even ability to see the data. All I need is to be able to pass it back in later native function calls.

Presumably I need to define a class extending Nan::ObjectWrap class, but in that case can someone provide a working code sample that contains only what is absolutely necessary to achieve this reference holding?

#include <nan.h>
#include <windows.h>

NAN_METHOD(myStructGetter) {
    THEIR_STRUCT data = someLibraryCall();
    info.GetReturnValue().Set( ??? );
}

NAN_METHOD(myStructUser) {
    THEIR_STRUCT data = (??? info[0] ???);
    someOtherLibraryCall(data);
}

NAN_MODULE_INIT(Init) {
    NAN_EXPORT(target, myStructGetter);
    NAN_EXPORT(target, myStructUser);
}

NODE_MODULE(myModule, Init)



via HonoredMule

No comments:

Post a Comment