nodejs / node-addon-api Public
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
Comments
|
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?) |
|
Hi @fholzer thank you for answering. No unfortunately I do not have access to the CallbackInfo. |
|
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. |
|
If you're calling from a non-N-API callback into JS on the same thread as JS, you can use If you're calling from a non-N-API callback into JS from a different thread, you can convert the |
|
Thank you @fholzer and @gabrielschulhof. I believe MakeCallback will do. I see from the docs that I need to pass the I have a class that inherits from EventEmitter similarly to this example. I can see that in there the |
|
@simoneperelli do you need to have a |
|
... and if you need to have a |
|
Thank you @gabrielschulhof will give it a try |
|
@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! |
|
@gabrielschulhof Thank you ! |

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.

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++ ?
The text was updated successfully, but these errors were encountered: