0

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?

Nay Min
  • 85
  • 10

1 Answers1

0

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({});
}
Nay Min
  • 85
  • 10
  • Apologies, I'm a noob in C/C++, but what if I need access to the callback function before calling it? For example, creating Napi objects using e.g. `Napi::String::New()` or similar requires a `Napi::Env` value, which could be obtained from the callback using `cb.Env()` at the place where I call `callback_add()` (that is, before turning `cb` into a reference). I have this in `OnReadyCallback`, but it segfaults: `Function *cb = (Function *)data; Env env = cb->Env();` – Dan Kadera Oct 15 '21 at 23:07