has native modules – “native addons”. e.g. the sqlite3 npm package (TryGhost/node-sqlite3 on GitHub) And just as React Native native modules are written with JSI, Node.js native addons are written with Node-API, which is similarly engine-agnostic. sqlite3.c JSI or Node-API V8 JSC Hermes … and more!
node-addon-api). # Community-driven; de facto industry standard. $ Available for various languages (C++, Swift, Rust, C#, Nim, Zig, Go…). % Can express pretty much anything ECMAScript can. & First-class interop between native and JS (e.g. lifetimes, async functions). ' ABI-stable (long story).
safe and stable MyApp.app libHermes.0.12.0.dylib ABI safety MyApp.app libHermes.0.12.0.dylib - libHermes.0.12.0.dylib + libHermes.0.13.0.dylib ABI stability Lets a binary be used in your app without recompilation, regardless of the build mode, compiler, or language. Lets your app adopt new versions of a binary without recompilation, due to backwards compatibility. (debug) (release)
Native Windows In order to realise these benefits, React Native Windows uses a fork of Hermes, hermes-windows, that implements an ABI-stable API based on Node-API. Since its development, Hermes has actually begun developing an official ABI‑stable API – but for now, let’s study how hermes-windows works!
• We can use the latest Hermes regardless of RNW version. • We can use prebuilt Hermes in release mode (for faster execution) whilst building the rest of our app in debug mode (for faster builds). … All thanks to Node-API!
native addons node_api.h Node-API js_native_api.h Although NativeScript is available as a Node.js native addon, Hermes Windows only implements the underlying API, Node-API. This means we lack module-loading support: require("./addon.node") + … Can we still use the library somehow?
Call objc_bridge_init() from the native side. Typical approach (Node.js) Modified approach (React Native) How to initialise the addon Using Node.js native addons with only Node-API
file to set up Node-API: react-native/ReactCommon/hermes/executor/HermesExecutorFactory.cpp Using Node.js native addons with only Node-API Call objc_bridge_init() during this initialisation! Initialising from the native side
native addons • Completion of the official ABI-stable Hermes API • ABI stability for React Native native modules • More cross-ecosystem collaboration Hopes for the future
native modules standard and are based on an ABI-stable API, Node-API. • ABI stability is important for flexibility in using prebuilt libraries. • Cross-ecosystem collaboration is currently limited without full Node.js native addons support, but not impossible with workarounds. • Work is underway to upstream Node-API into Hermes! Summary