Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how can i use js callback #1

Open
liuxingpluck opened this issue Oct 10, 2022 · 1 comment
Open

how can i use js callback #1

liuxingpluck opened this issue Oct 10, 2022 · 1 comment

Comments

@liuxingpluck
Copy link

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

@akshayganeshen
Copy link
Owner

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants