Rust in node examples

Experimenting with adding Rust extension into NodeJS/V8 crossplatformingly via C++ native addon with help of C ABI interface.

Entirely the same approach works for Electron framework, with a little nuance that you should put your plugin into node_modules and do electron-rebuild.cmd -w youraddonname -f

Experimented parameter and return types:

void, bool, c_int, c_double (JS float), *c_char (JS string), Struct (JS object), *c_int (JS numeric array)

What's inside:

  • Rust dynamic library in ./src/embed.rs (linked with mylib.cc)
  • Native NodeJS C++ addon ./src/addon.cc using ./src/embed.rs library

Tested on Windows, Ubuntu and OS X

Installing

NON npm dependencies:

  • node-gyp installation recommendations fulfilled properly properly for your platform
  • Rust and Cargo (I did not check older version, but I think it should work with Rust 1.6+). I, personally use multirust-rs and nightly target

NPM dependencies:

> npm install

Building

Build whole addon:

> npm run build

Rebuild Rust only (keep addon.node as it is)

> npm run rebuild-rust

Please refer to package.json for more details.

Running

> node index.js

Links

Disclaimer

These examples are possibly unsafe due to my insufficient competence in subject. However they should be useful for beginners.