node.jsnode-addon-api

nodejs napi How to manually create a CallbackInfo type data


I want to call some existing functions, but the function requires the CallbackInfo parameter, how do I initialize a CallbackInfo

Value b(const CallbackInfo& info)
{
  ...
}

Value a(const CallbackInfo& info)
{
  CallbackInfo newInfo = CallbackInfo::New();
  return b(newInfo);
}

Solution

  • void b(const CallbackInfo& info)
    {
      printf("function b\n");
    }
    
    void a(const CallbackInfo& info)
    {
       auto js_this = info.This().As<Napi::Object>();
    
      if (js_this.Has("b") && js_this.Get("b").IsFunction())
      {
        js_this.Get("b").As<Napi::Function>().Call(info.This(), {});
      }
    }