Add node NAPI Function as callback to native library

2.3k Views Asked by At

Is it possible to add javascript function as callback to a native library accepting callbacks through NAPI?

Here's an example addon code I have so far.


Napi::Function *jsCallback;
void RegisterReadyEvent(const Napi::CallbackInfo &info)
{
    Napi::Function callback = info[0].As<Napi::Function>();
    jsCallback = &jsCallback;

    // native library function for registering callback
    callback_add(OnReadyCallback);
}

void OnReadyCallback(Args *arg)
{
   jsCallback->Call();
}

Napi::Object InitAll(Napi::Env env, Napi::Object exports)
{
    exports.Set(String::New(env, "onReady"), Function::New(env, RegisterReadyEvent));
    return exports;
}

The problem is the order of declaration. It seems OnReadyCallback has to be declared before RegisterReadyEvent function or it won't compile. But if I declare it before RegisterReadyEvent, jsCallback became an empty pointer and it still won't compile.

Or is there a better and proper way to achieve the same thing?

1

There are 1 best solutions below

1
On

It seems the C library have void *data as a state placeholder. Apparently it's a norm in C libraries exposing callbacks.

Pass the Napi::Function as reference.

callback_add(OnReadyCallback, &callback);

And cast it back.

void OnReadyCallback(Args *arg, void *data)
{
   ((Napi::Function *)data)->Call({});
}