node-api: deprecate napi_module_register based registration
Summary
Change NAPI_MODULE
and NAPI_MODULE_INIT
macros to use symbol-based module registration.
Symbol-based registration was added in PR #20161 and used by Node-API modules written in non-C++ languages.
It is much simpler and requires no work on module library load.
Details
Currently ABI-safe Node-API modules have two ways to register modules:
- Create
napi_module
struct and callnapi_module_register
on module library (.so or .dll) load. - Define
napi_register_module_v1
function which is called on-demand to initialize module exports.
Node.JS module binding loads module library and then
- if it finds registered
napi_module
struct, then it uses itsnm_register_func
to initialize module exports, - otherwise, it tries to find the
napi_register_module_v1
and calls it to initialize module exports.
The first registration method is the original Node-API (NAPI) module registration. The second method (aka symbol-based registration) was added in PR #20161 on Apr 23, 2018, and available in Node.JS from version 10.
It is interesting to see that all Node-API bindings written in other languages (Rust, Go, etc.) they all use the symbol-based registration. Even the C/C++ WASM bindings use the symbol-based registration. It is easier to implement, and it does not need calling code on module library load.
In this PR we align C/C++ Node-API registration with registration in other languages to use the symbol-based registration. The NAPI_MODULE
and NAPI_MODULE_INIT
macros are changed to define the napi_register_module_v1
function instead of calling napi_module_register
. It effectively deprecates the napi_module_register
function, but since no changes are made to the Node.JS binding mechanism, all previously compiled modules must continue to work.
The test_null_init
is removed because new macros fail to compile if the NULL
parameter is given to the NAPI_MODULE
macro.
This PR is a "spin off" from the PR #45715 to keep it focused on the new versioning approach.