Monday, 12 November 2018

Converting V8 objects to pointers

I'm writing an Node application (in TS) that needs to be interfaced with some native library.

I have a library (written in C) - let's consider it's a blackbox - and I'm writing a wrapper using NAN. The library native interface can be simplified into a following functions:

typedef void (*got_message_reply_cb)(context ctx, void * priv, struct X * the_reply);

context lib_connect();
bool lib_send_message(context ctx, message msg, got_message_reply_cb callback, void * priv);

I believe this is pretty straight-forward to understand.

So, I'm trying to wrap that black-box native library into something like:

class TheLibrary : public Nan::ObjectWrap {
    Initialize(v8::Handle<v8::Object> target);
    SendMessage(...)
}

And then to javascript object like:

class TheLibrary {
    SendMessage(message: whatever, callback: (reply) => void); // or return promise, doesn't matter
}

How to do the actual handling of the callback in the NAN C++ module? I need to somehow pass the callback (represented probably by Local<Function> - which have, if I understand it correctly, limited scope) as a pointer to the function and then retrieve it back. How to do that? Thanks for your replies.



from Converting V8 objects to pointers

No comments:

Post a Comment