Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

JS Fest 2019/Autumn. Алексей Орленко. Node.js N-API for Rust

0 views

Published on

There are plenty of use cases for native modules in Node.js. Most importantly, one might want to interface with third-party libraries or operating system services that provide functionality not available in Node.js core out-of-the-box. Another popular option is to use a compiled language for a computationally-heavy algorithm while still using JavaScript for application business logic (although care must be taken and no premature optimization be made as often the cost of crossing the boundary may be higher than performance increase, unless you are doing some kind of image processing or linear algebra, for example). For a long time C++ was the only option to write native addons for Node.js (technically, there were projects that allowed to use other languages, but they were merely second-level bridges, thus twice FFI overhead). In this talk we will look at the history of native modules in Node.js, what N-API is, which problems does it solve and how it enables to use other systems programming languages. We will also look at the future perspectives and briefly discuss in which cases WebAssembly and upcoming WASI can be used instead of native modules.

Published in: Education
  • Be the first to comment

JS Fest 2019/Autumn. Алексей Орленко. Node.js N-API for Rust

  1. 1. Node.js N-API for Rust Alexey Orlenko @aqrln
  2. 2. github.com/aqrln/ jsfest2019-napi-rust
  3. 3. N-API is an API for building native addons that is independent from the underlying JavaScript runtime and is maintained as part of Node.js itself. It also allows modules compiled for one major version of Node.js to run on later ones without recompiling. See https://nodejs.org/dist/latest-v13.x/docs/api/n-api.html
  4. 4. Pure C API allows to write native addons in virtually any compiled programming language, from C++ and Rust to OCaml and Haskell.
  5. 5. * although using languages with managed memory and their own runtimes might be a bit tricky, probably
  6. 6. In fact, there’s even an official C++ wrapper around N-API: https://github.com/ nodejs/node-addon-api
  7. 7. Rust: - fast - modern - integrates seamlessly with C APIs with no overhead - great ecosystem and even greater community
  8. 8. https://neon-bindings.com + stable and mature + good documentation - N-API backend is not ready yet - and maintaining own C wrappers for raw C++ Node.js API has certain drawbacks
  9. 9. N-API + can be used directly from Rust literally the same way one would use it from C - too low-level to write application code directly in terms of its API + but flexible enough to build a nice type-safe Rust API on top of it
  10. 10. If you want to follow the presentation on your own machine or to tweak with the code later, clone the repo (github.com/aqrln/jsfest2019-napi-rust) and then either (a, b or c):
  11. 11. a) If you use VS Code, install the Remote Development plugin (ms-vscode-remote.vscode-remote- extensionpack) and choose to open the folder in container.
  12. 12. b) If you don’t use VS Code, but you do use Docker, run: $ docker build . -t napi-rust $ docker run --it -v $(pwd):/workspaces/napi-rust napi-rust bash
  13. 13. c) Alternatively, install the required software on your host machine: - Node.js - Rust - npm install node-gyp benchmark - cargo install bindgen
  14. 14. Our plan: 1. Get started with using N-API in general, discuss JavaScript values representation and error handling 2. Learn how to generate Rust bindings from C headers
  15. 15. Our plan: 3. Implement type-safe convenience API on top of N-API 4. Utilize heavy metaprogramming in form of procedural macros to eliminate boilerplate code
  16. 16. Our plan: 5. Do some parallel computations and benchmark what we got 6. Release a new version of https://crates.io/crates/napi and pretend it didn’t take almost two years
  17. 17. Now let’s switch to the editor
  18. 18. Recap: - N-API, initially conceived to make Node.js API VM-agnostic and forward-compatible (both API and ABI wise), has a nice side-effect of allowing to use languages other than C++ effortlessly
  19. 19. Recap: - Rust can interoperate with C APIs directly with no overhead. We only used a tiny bit of C to integrate with node-gyp for the sake of simplicity, but that is not strictly necessary: Neon has its own build tool in pure Rust, for example.
  20. 20. Recap: - Procedural macros in Rust 2018 allowed us to to declare N-API callbacks with automatic parameters validation and all necessary error handling extremely concisely
  21. 21. (cont) Rust 2015 only supported procedural macros that automatically derive traits for structs, so we had to pack all parameters of a callback into a structure and create a wrapper function with a separate macro
  22. 22. PSA: Contact me if - you might want to be a maintainer of https://crates.io/crates/napi or - you are learning Rust and you are looking for a task and a mentor
  23. 23. @aqrln GitHub/Twitter

×