Skip to content
Advertisement

Calling JavaScript from C++ with node.js

Is there a way to call JS functions from C++ through node.js (as callbacks or something like that)? If yes, how? I’m searching for it on the web, but haven’t found any helpful resource.

Thanks in advance

Advertisement

Answer

One way to do it form a native addon can be using the provided function as a callback, for example let’s gonna assume that you have a function named setPrintFunction() declared in your native environment (A native addon):

(Call this for example main.cc)

JavaScript

Then, just importing your addon and using it like:

(Call this for example index.js)

JavaScript

So what you’re basically doing is capturing the reference to the v8::Function (Which is the javascript function, but in the native environment) and then invoking it and passing "Hello World!" as the first (and unique) parameter.

More on the subject: https://nodejs.org/api/addons.html

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement