X Tutup
The Wayback Machine - https://web.archive.org/web/20220405131734/https://github.com/nodejs/node-addon-api/issues/802
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 to call a javascript object method from C++ code #802

Closed
simoneperelli opened this issue Sep 1, 2020 · 10 comments
Closed

How to call a javascript object method from C++ code #802

simoneperelli opened this issue Sep 1, 2020 · 10 comments

Comments

@simoneperelli
Copy link

@simoneperelli simoneperelli commented Sep 1, 2020

Hi,
I am porting a medium-sized old native addon to NAPI. Inside of this addon there is an event emitter object (built similarly to @NickNaso examples) and native functions that call this event emitter's methods.

The existing implementation uses node::MakeCallback to call the "emit" on JS side.

Looking at the various examples and issues I couldn't figure out how to initiate such a call with NAPI from C++ since I would need to pass a reference to Env. Would it make sense to store Env as a property in the Event Emitter and reuse that once I need to call methods from C++ ?

@fholzer
Copy link
Contributor

@fholzer fholzer commented Sep 2, 2020

When you need to call the javascript function, is that as a result of javascript call to your addon? (ie. is there some other javascript on the stack?)

@simoneperelli
Copy link
Author

@simoneperelli simoneperelli commented Sep 2, 2020

Hi @fholzer thank you for answering. No unfortunately I do not have access to the CallbackInfo.
We are mostly talking about system events like tracking the mouse movement so the call is going to be made by pure C++ code.

@fholzer
Copy link
Contributor

@fholzer fholzer commented Sep 2, 2020

I think you'll probably need to use ThreadSafeFunction

When talking about javascript calls from additional threads, that doesn't necessarily mean threads that you directly start yourself. Could also be threads started by some other library your addon uses.

@gabrielschulhof
Copy link
Contributor

@gabrielschulhof gabrielschulhof commented Sep 2, 2020

If you're calling from a non-N-API callback into JS on the same thread as JS, you can use MakeCallback.

If you're calling from a non-N-API callback into JS from a different thread, you can convert the Napi::Function you wish to call to a ThreadSafeFunction and call that from any thread.

@simoneperelli
Copy link
Author

@simoneperelli simoneperelli commented Sep 3, 2020

Thank you @fholzer and @gabrielschulhof. I believe MakeCallback will do. I see from the docs that I need to pass the this (as Napi Value) representing the object but I am not sure how would I obtain it in this case.

I have a class that inherits from EventEmitter similarly to this example. I can see that in there the This is obtained through the CallbackInfo but again I am not going to have that

@gabrielschulhof
Copy link
Contributor

@gabrielschulhof gabrielschulhof commented Sep 16, 2020

@simoneperelli do you need to have a This? If you have a FunctionReference you can call MakeCallback with Napi::Env::Undefined() as your This.

@gabrielschulhof
Copy link
Contributor

@gabrielschulhof gabrielschulhof commented Sep 16, 2020

... and if you need to have a This you can create a reference to it and pass it along to wherever you call MakeCallback.

@simoneperelli
Copy link
Author

@simoneperelli simoneperelli commented Sep 18, 2020

Thank you @gabrielschulhof will give it a try

@gabrielschulhof
Copy link
Contributor

@gabrielschulhof gabrielschulhof commented Dec 4, 2020

@simoneperelli hopefully my suggestion worked for you. If it did not, and you believe we need to change node-addon-api or add something to it to better support your use case, please re-open this issue!

@simoneperelli
Copy link
Author

@simoneperelli simoneperelli commented Dec 4, 2020

@gabrielschulhof Thank you !

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

3 participants
X Tutup