napi-go icon indicating copy to clipboard operation
napi-go copied to clipboard

how can i use js callback

Open liuxingpluck opened this issue 3 years ago • 1 comments

i want to use like this: js code: let gomodule=require("./gomod.node"); gomodule.func( function( fromGoObj){ console.info(fromGoObj) } );

then how can i write go code?

Your project has been very helpful to me. Thank you very much

liuxingpluck avatar Oct 10 '22 11:10 liuxingpluck

Hey! Sorry for the super slow response on this.

I'm still developing the js_native_api.go to allow for this use-case, but it's not there just yet. Ultimately you'd need to make use of the napi_call_function / napi.CallFunction API to do this.

This isn't finalized, but the Go portion would look a little something like this once that's added:

func FuncCallback(env napi.Env, info napi.CallbackInfo) napi.Value {
  callbackInfo, _ := napi.GetCbInfo(env, info)
  jsCallback := callbackInfo.Args[0]

  fromGoObj, _ := napi.CreateObject(env)
  globalValue, _ := napi.GetGlobal(env)
  // this napi.CallFunction API does not exist yet:
  napi.CallFunction(env, globalValue, jsCallback, []napi.Value{fromGoObj})
}

// func CallFunction(env napi.Env, recv napi.Value, args []napi.Value) (napi.Value, napi.Status)

akshayganeshen avatar Nov 07 '22 22:11 akshayganeshen