diff --git a/crates/napi/Cargo.toml b/crates/napi/Cargo.toml index a3b50f99..19c23857 100644 --- a/crates/napi/Cargo.toml +++ b/crates/napi/Cargo.toml @@ -83,6 +83,7 @@ version = "1" [dependencies.napi-sys] path = "../sys" version = "3.1.1" +default-features = false [dependencies.encoding_rs] optional = true diff --git a/crates/napi/src/bindgen_runtime/js_values/map.rs b/crates/napi/src/bindgen_runtime/js_values/map.rs index 0296f821..4b5a7b8c 100644 --- a/crates/napi/src/bindgen_runtime/js_values/map.rs +++ b/crates/napi/src/bindgen_runtime/js_values/map.rs @@ -23,6 +23,7 @@ where K: AsRef, V: ToNapiValue, { + #[cfg(not(feature = "noop"))] unsafe fn to_napi_value(raw_env: sys::napi_env, val: Self) -> Result { let env = Env::from(raw_env); #[cfg_attr(feature = "napi10", allow(unused_mut))] @@ -30,14 +31,16 @@ where #[cfg(all( feature = "napi10", feature = "node_version_detect", - feature = "dyn-symbols" + feature = "dyn-symbols", + not(feature = "noop"), ))] let node_version = NODE_VERSION.get().unwrap(); for (k, v) in val.into_iter() { #[cfg(all( feature = "napi10", feature = "node_version_detect", - feature = "dyn-symbols" + feature = "dyn-symbols", + not(feature = "noop"), ))] { if node_version.major >= 20 && node_version.minor >= 18 { @@ -56,6 +59,11 @@ where unsafe { Object::to_napi_value(raw_env, obj) } } + + #[cfg(feature = "noop")] + unsafe fn to_napi_value(_env: sys::napi_env, _val: Self) -> Result { + unimplemented!("HashMap is not supported in noop mode"); + } } impl FromNapiValue for HashMap @@ -95,6 +103,7 @@ where K: AsRef, V: ToNapiValue, { + #[cfg(not(feature = "noop"))] unsafe fn to_napi_value(raw_env: sys::napi_env, val: Self) -> Result { let env = Env::from(raw_env); #[cfg_attr(feature = "napi10", allow(unused_mut))] @@ -102,14 +111,16 @@ where #[cfg(all( feature = "napi10", feature = "node_version_detect", - feature = "dyn-symbols" + feature = "dyn-symbols", + not(feature = "noop"), ))] let node_version = NODE_VERSION.get().unwrap(); for (k, v) in val.into_iter() { #[cfg(all( feature = "napi10", feature = "node_version_detect", - feature = "dyn-symbols" + feature = "dyn-symbols", + not(feature = "noop"), ))] { if node_version.major >= 20 && node_version.minor >= 18 { @@ -128,6 +139,11 @@ where unsafe { Object::to_napi_value(raw_env, obj) } } + + #[cfg(feature = "noop")] + unsafe fn to_napi_value(_env: sys::napi_env, _val: Self) -> Result { + unimplemented!("BTreeMap is not supported in noop mode"); + } } impl FromNapiValue for BTreeMap @@ -169,6 +185,7 @@ where V: ToNapiValue, S: Default + BuildHasher, { + #[cfg(not(feature = "noop"))] unsafe fn to_napi_value(raw_env: sys::napi_env, val: Self) -> Result { let env = Env::from(raw_env); #[cfg_attr(feature = "napi10", allow(unused_mut))] @@ -176,14 +193,16 @@ where #[cfg(all( feature = "napi10", feature = "node_version_detect", - feature = "dyn-symbols" + feature = "dyn-symbols", + not(feature = "noop"), ))] let node_version = NODE_VERSION.get().unwrap(); for (k, v) in val.into_iter() { #[cfg(all( feature = "napi10", feature = "node_version_detect", - feature = "dyn-symbols" + feature = "dyn-symbols", + not(feature = "noop"), ))] { if node_version.major >= 20 && node_version.minor >= 18 { @@ -202,6 +221,11 @@ where unsafe { Object::to_napi_value(raw_env, obj) } } + + #[cfg(feature = "noop")] + unsafe fn to_napi_value(_env: sys::napi_env, _val: Self) -> Result { + unimplemented!("BTreeMap is not supported in noop mode"); + } } #[cfg(feature = "object_indexmap")] @@ -227,7 +251,8 @@ where #[cfg(all( feature = "napi10", feature = "node_version_detect", - feature = "dyn-symbols" + feature = "dyn-symbols", + not(feature = "noop"), ))] fn fast_set_property, V: ToNapiValue>( raw_env: sys::napi_env, diff --git a/crates/napi/src/bindgen_runtime/module_register.rs b/crates/napi/src/bindgen_runtime/module_register.rs index 7cca7e78..f38a4a66 100644 --- a/crates/napi/src/bindgen_runtime/module_register.rs +++ b/crates/napi/src/bindgen_runtime/module_register.rs @@ -251,7 +251,10 @@ pub unsafe extern "C" fn napi_register_module_v1( env: sys::napi_env, exports: sys::napi_value, ) -> sys::napi_value { - #[cfg(not(target_family = "wasm"))] + #[cfg(any( + target_env = "msvc", + all(not(target_family = "wasm"), feature = "dyn-symbols") + ))] unsafe { sys::setup(); } diff --git a/crates/sys/src/functions.rs b/crates/sys/src/functions.rs index 7a01a799..e141815f 100644 --- a/crates/sys/src/functions.rs +++ b/crates/sys/src/functions.rs @@ -838,7 +838,7 @@ pub use napi8::*; #[cfg(feature = "napi9")] pub use napi9::*; -#[cfg(all(windows, not(target_env = "msvc")))] +#[cfg(all(windows, not(target_env = "msvc"), feature = "dyn-symbols"))] fn test_library( lib_result: Result, ) -> Result { @@ -859,7 +859,7 @@ fn test_library( } } -#[cfg(all(windows, not(target_env = "msvc")))] +#[cfg(all(windows, not(target_env = "msvc"), feature = "dyn-symbols"))] fn find_node_library() -> Result { return unsafe { test_library(libloading::os::windows::Library::this()) @@ -876,7 +876,10 @@ fn find_node_library() -> Result { }; } -#[cfg(not(target_family = "wasm"))] +#[cfg(any( + target_env = "msvc", + all(not(target_family = "wasm"), feature = "dyn-symbols") +))] pub(super) unsafe fn load_all() -> Result { #[cfg(all(windows, target_env = "msvc"))] let host = libloading::os::windows::Library::this()?.into(); diff --git a/crates/sys/src/lib.rs b/crates/sys/src/lib.rs index f9ea57c1..e5cd4428 100644 --- a/crates/sys/src/lib.rs +++ b/crates/sys/src/lib.rs @@ -2,7 +2,10 @@ #![allow(ambiguous_glob_reexports)] -#[cfg(not(target_family = "wasm"))] +#[cfg(any( + target_env = "msvc", + all(not(target_family = "wasm"), feature = "dyn-symbols") +))] macro_rules! generate { (@stub_fn $name:ident($($param:ident: $ptype:ty,)*) -> napi_status) => { unsafe extern "C" fn $name($(_: $ptype,)*) -> napi_status { @@ -76,7 +79,10 @@ macro_rules! generate { }; } -#[cfg(target_family = "wasm")] +#[cfg(any( + target_family = "wasm", + all(not(target_env = "msvc"), not(feature = "dyn-symbols")) +))] macro_rules! generate { (extern "C" { $(fn $name:ident($($param:ident: $ptype:ty$(,)?)*)$( -> $rtype:ty)?;)+ @@ -95,7 +101,10 @@ mod types; pub use functions::*; pub use types::*; -#[cfg(not(target_family = "wasm"))] +#[cfg(any( + target_env = "msvc", + all(not(target_family = "wasm"), feature = "dyn-symbols") +))] /// Loads N-API symbols from host process. /// Must be called at least once before using any functions in bindings or /// they will panic diff --git a/examples/napi/Cargo.toml b/examples/napi/Cargo.toml index b1f8ec67..d3281bb5 100644 --- a/examples/napi/Cargo.toml +++ b/examples/napi/Cargo.toml @@ -48,6 +48,7 @@ napi = { path = "../../crates/napi", default-features = false, features = [ "node_version_detect", "web_stream", "error_anyhow", + "dyn-symbols", ] } [target.'cfg(target_family = "wasm")'.dependencies]