DEV Community

loading...

Help using Node addons to call c++

gwsounddsg profile image GW ・1 min read

I'm currently working with trying to call c++ in js. I've been successful using nan for passing and returning primitives.

But when I tried passing/returning std::string it got harder. I read in the addon docs, "Unless there is a need for direct access to functionality which is not exposed by N-API, use N-API."

Any advise on how to proceed? Should I stick with nan or switch to N-API? I'm eventually going to work with c++ classes and structs.

Discussion

pic
Editor guide
Collapse
adam_cyclones profile image
Adam Crockett

Hello, I'm not really so clued up on the node C++ extensions but what are you trying to achieve overall, I'd suggest web assembly depending on this.

Collapse
gwsounddsg profile image
GW Author

Thanks for the reply Adam,

I'm building a cross-platform desktop audio app using Electron and React. The audio part needs to be in c++. I haven't used web assembly before, would it be a better solution for me? I can give you more details if you need.

Collapse
adam_cyclones profile image
Adam Crockett

Yes absolutely, Wasm compiles from a variety of languages including c++. The compiler you need is called emacripten, go download it to get started. Emacripten looks scary but it's not to bad I swear and I can help you. WASM typically runs in browsers but works in node too.

Thread Thread
gwsounddsg profile image
GW Author

Amazing! I just might take you up on your offer. Much appricated.

Thread Thread
adam_cyclones profile image
Adam Crockett

If you get a repo up and running I'm sure I can help.

Thread Thread
gwsounddsg profile image
GW Author

After a bunch of reading up on wasm and emscripten, I've been hitting a wall with just installing emscripten. Or more specifically, including emscripten.h. Any ideas? I'm on macOS.

Also, is there a better platform I can bug you with questions rather than this dev.to post?

Thread Thread
adam_cyclones profile image
Adam Crockett

I think il have to clone what you have, I'm macos as well. Honestly github repo private or public will be the best way to colab

Thread Thread
gwsounddsg profile image
GW Author

Great! Just created a repo and sent you a request.

Thread Thread
adam_cyclones profile image
Adam Crockett

Thanks, I might not be able to help tonight, I'm moving house soon so agreed to sit down and make some lists with my partner il be able to clone tomorrow.