Replace complex imperative registration logic with the well-defined `napi_register_module_v1` symbol, as discussed here:
@@ -27,11 +27,25 @@ compile_error!("Cannot enable both `legacy-runtime` and `napi-runtime` features.
use cfg_if::cfg_if;
-// FIXME: move doc comments above the cfg_if! and see if that works
-
cfg_if! {
if #[cfg(feature = "napi-runtime")] {
-
+ /// Register the current crate as a Node module, providing startup
+ /// logic for initializing the module object at runtime.
+ ///
+ /// The first argument is a pattern bound to a `neon::context::ModuleContext`. This
+ /// is usually bound to a mutable variable `mut cx`, which can then be used to
+ /// pass to Neon APIs that require mutable access to an execution context.
+ ///
+ /// Example:
+ ///
+ /// ```rust,ignore
+ /// register_module!(mut cx, {
+ /// cx.export_function("foo", foo)?;
+ /// cx.export_function("bar", bar)?;
+ /// cx.export_function("baz", baz)?;
+ /// Ok(())
+ /// });
+ /// ```
#[macro_export]
macro_rules! register_module {
($module:pat, $init:block) => {
@@ -39,44 +53,21 @@ cfg_if! {
};
($name:expr, $module:pat, $init:block) => {
- #[cfg_attr(target_os = "linux", link_section = ".ctors")]
- #[cfg_attr(target_os = "macos", link_section = "__DATA,__mod_init_func")]
- #[cfg_attr(target_os = "windows", link_section = ".CRT$XCU")]
- pub static __register_neon_module: extern "C" fn() = {
- unsafe extern "C" fn __init_neon_module(
+ #[no_mangle]
+ pub unsafe extern "C" fn napi_register_module_v1(
_env: $crate::macro_internal::runtime::nodejs_sys::napi_env,
exports: $crate::macro_internal::runtime::nodejs_sys::napi_value
) -> $crate::macro_internal::runtime::nodejs_sys::napi_value
{
- $init
- exports
- }
-
- static mut __neon_module: $crate::macro_internal::runtime::nodejs_sys::napi_module
- = $crate::macro_internal::runtime::nodejs_sys::napi_module {
- nm_version: 1,
- nm_flags: 0,
- nm_filename: concat!(file!(), "\0").as_ptr() as *const _,
- nm_register_func: Some(__init_neon_module),
- nm_modname: concat!($name, "\0").as_ptr() as *const _,
- nm_priv: ::std::ptr::null_mut(),
- reserved: [0 as *mut _; 4],
- };
-
- extern "C" fn register_module() {
// Suppress the default Rust panic hook, which prints diagnostics to stderr.
::std::panic::set_hook(::std::boxed::Box::new(|_| { }));
- unsafe {
- $crate::macro_internal::runtime::nodejs_sys::napi_module_register(&mut __neon_module as *mut _);
- }
- }
+ $init
- register_module
- };
+ exports
+ }
}
}
-
} else {
/// Register the current crate as a Node module, providing startup
/// logic for initializing the module object at runtime.
