chore(sys): add back non dyn-symbols behavior (#3045)

This commit is contained in:
LongYinan 2025-12-02 16:46:42 +08:00 committed by GitHub
parent 295c6b3c60
commit cf0465f390
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 56 additions and 14 deletions

View File

@ -83,6 +83,7 @@ version = "1"
[dependencies.napi-sys] [dependencies.napi-sys]
path = "../sys" path = "../sys"
version = "3.1.1" version = "3.1.1"
default-features = false
[dependencies.encoding_rs] [dependencies.encoding_rs]
optional = true optional = true

View File

@ -23,6 +23,7 @@ where
K: AsRef<str>, K: AsRef<str>,
V: ToNapiValue, V: ToNapiValue,
{ {
#[cfg(not(feature = "noop"))]
unsafe fn to_napi_value(raw_env: sys::napi_env, val: Self) -> Result<sys::napi_value> { unsafe fn to_napi_value(raw_env: sys::napi_env, val: Self) -> Result<sys::napi_value> {
let env = Env::from(raw_env); let env = Env::from(raw_env);
#[cfg_attr(feature = "napi10", allow(unused_mut))] #[cfg_attr(feature = "napi10", allow(unused_mut))]
@ -30,14 +31,16 @@ where
#[cfg(all( #[cfg(all(
feature = "napi10", feature = "napi10",
feature = "node_version_detect", feature = "node_version_detect",
feature = "dyn-symbols" feature = "dyn-symbols",
not(feature = "noop"),
))] ))]
let node_version = NODE_VERSION.get().unwrap(); let node_version = NODE_VERSION.get().unwrap();
for (k, v) in val.into_iter() { for (k, v) in val.into_iter() {
#[cfg(all( #[cfg(all(
feature = "napi10", feature = "napi10",
feature = "node_version_detect", feature = "node_version_detect",
feature = "dyn-symbols" feature = "dyn-symbols",
not(feature = "noop"),
))] ))]
{ {
if node_version.major >= 20 && node_version.minor >= 18 { if node_version.major >= 20 && node_version.minor >= 18 {
@ -56,6 +59,11 @@ where
unsafe { Object::to_napi_value(raw_env, obj) } unsafe { Object::to_napi_value(raw_env, obj) }
} }
#[cfg(feature = "noop")]
unsafe fn to_napi_value(_env: sys::napi_env, _val: Self) -> Result<sys::napi_value> {
unimplemented!("HashMap is not supported in noop mode");
}
} }
impl<K, V, S> FromNapiValue for HashMap<K, V, S> impl<K, V, S> FromNapiValue for HashMap<K, V, S>
@ -95,6 +103,7 @@ where
K: AsRef<str>, K: AsRef<str>,
V: ToNapiValue, V: ToNapiValue,
{ {
#[cfg(not(feature = "noop"))]
unsafe fn to_napi_value(raw_env: sys::napi_env, val: Self) -> Result<sys::napi_value> { unsafe fn to_napi_value(raw_env: sys::napi_env, val: Self) -> Result<sys::napi_value> {
let env = Env::from(raw_env); let env = Env::from(raw_env);
#[cfg_attr(feature = "napi10", allow(unused_mut))] #[cfg_attr(feature = "napi10", allow(unused_mut))]
@ -102,14 +111,16 @@ where
#[cfg(all( #[cfg(all(
feature = "napi10", feature = "napi10",
feature = "node_version_detect", feature = "node_version_detect",
feature = "dyn-symbols" feature = "dyn-symbols",
not(feature = "noop"),
))] ))]
let node_version = NODE_VERSION.get().unwrap(); let node_version = NODE_VERSION.get().unwrap();
for (k, v) in val.into_iter() { for (k, v) in val.into_iter() {
#[cfg(all( #[cfg(all(
feature = "napi10", feature = "napi10",
feature = "node_version_detect", feature = "node_version_detect",
feature = "dyn-symbols" feature = "dyn-symbols",
not(feature = "noop"),
))] ))]
{ {
if node_version.major >= 20 && node_version.minor >= 18 { if node_version.major >= 20 && node_version.minor >= 18 {
@ -128,6 +139,11 @@ where
unsafe { Object::to_napi_value(raw_env, obj) } unsafe { Object::to_napi_value(raw_env, obj) }
} }
#[cfg(feature = "noop")]
unsafe fn to_napi_value(_env: sys::napi_env, _val: Self) -> Result<sys::napi_value> {
unimplemented!("BTreeMap is not supported in noop mode");
}
} }
impl<K, V> FromNapiValue for BTreeMap<K, V> impl<K, V> FromNapiValue for BTreeMap<K, V>
@ -169,6 +185,7 @@ where
V: ToNapiValue, V: ToNapiValue,
S: Default + BuildHasher, S: Default + BuildHasher,
{ {
#[cfg(not(feature = "noop"))]
unsafe fn to_napi_value(raw_env: sys::napi_env, val: Self) -> Result<sys::napi_value> { unsafe fn to_napi_value(raw_env: sys::napi_env, val: Self) -> Result<sys::napi_value> {
let env = Env::from(raw_env); let env = Env::from(raw_env);
#[cfg_attr(feature = "napi10", allow(unused_mut))] #[cfg_attr(feature = "napi10", allow(unused_mut))]
@ -176,14 +193,16 @@ where
#[cfg(all( #[cfg(all(
feature = "napi10", feature = "napi10",
feature = "node_version_detect", feature = "node_version_detect",
feature = "dyn-symbols" feature = "dyn-symbols",
not(feature = "noop"),
))] ))]
let node_version = NODE_VERSION.get().unwrap(); let node_version = NODE_VERSION.get().unwrap();
for (k, v) in val.into_iter() { for (k, v) in val.into_iter() {
#[cfg(all( #[cfg(all(
feature = "napi10", feature = "napi10",
feature = "node_version_detect", feature = "node_version_detect",
feature = "dyn-symbols" feature = "dyn-symbols",
not(feature = "noop"),
))] ))]
{ {
if node_version.major >= 20 && node_version.minor >= 18 { if node_version.major >= 20 && node_version.minor >= 18 {
@ -202,6 +221,11 @@ where
unsafe { Object::to_napi_value(raw_env, obj) } unsafe { Object::to_napi_value(raw_env, obj) }
} }
#[cfg(feature = "noop")]
unsafe fn to_napi_value(_env: sys::napi_env, _val: Self) -> Result<sys::napi_value> {
unimplemented!("BTreeMap is not supported in noop mode");
}
} }
#[cfg(feature = "object_indexmap")] #[cfg(feature = "object_indexmap")]
@ -227,7 +251,8 @@ where
#[cfg(all( #[cfg(all(
feature = "napi10", feature = "napi10",
feature = "node_version_detect", feature = "node_version_detect",
feature = "dyn-symbols" feature = "dyn-symbols",
not(feature = "noop"),
))] ))]
fn fast_set_property<K: AsRef<str>, V: ToNapiValue>( fn fast_set_property<K: AsRef<str>, V: ToNapiValue>(
raw_env: sys::napi_env, raw_env: sys::napi_env,

View File

@ -251,7 +251,10 @@ pub unsafe extern "C" fn napi_register_module_v1(
env: sys::napi_env, env: sys::napi_env,
exports: sys::napi_value, exports: sys::napi_value,
) -> 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 { unsafe {
sys::setup(); sys::setup();
} }

View File

@ -838,7 +838,7 @@ pub use napi8::*;
#[cfg(feature = "napi9")] #[cfg(feature = "napi9")]
pub use napi9::*; pub use napi9::*;
#[cfg(all(windows, not(target_env = "msvc")))] #[cfg(all(windows, not(target_env = "msvc"), feature = "dyn-symbols"))]
fn test_library( fn test_library(
lib_result: Result<libloading::os::windows::Library, libloading::Error>, lib_result: Result<libloading::os::windows::Library, libloading::Error>,
) -> Result<libloading::Library, libloading::Error> { ) -> Result<libloading::Library, libloading::Error> {
@ -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<libloading::Library, libloading::Error> { fn find_node_library() -> Result<libloading::Library, libloading::Error> {
return unsafe { return unsafe {
test_library(libloading::os::windows::Library::this()) test_library(libloading::os::windows::Library::this())
@ -876,7 +876,10 @@ fn find_node_library() -> Result<libloading::Library, libloading::Error> {
}; };
} }
#[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<libloading::Library, libloading::Error> { pub(super) unsafe fn load_all() -> Result<libloading::Library, libloading::Error> {
#[cfg(all(windows, target_env = "msvc"))] #[cfg(all(windows, target_env = "msvc"))]
let host = libloading::os::windows::Library::this()?.into(); let host = libloading::os::windows::Library::this()?.into();

View File

@ -2,7 +2,10 @@
#![allow(ambiguous_glob_reexports)] #![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 { macro_rules! generate {
(@stub_fn $name:ident($($param:ident: $ptype:ty,)*) -> napi_status) => { (@stub_fn $name:ident($($param:ident: $ptype:ty,)*) -> napi_status) => {
unsafe extern "C" fn $name($(_: $ptype,)*) -> 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 { macro_rules! generate {
(extern "C" { (extern "C" {
$(fn $name:ident($($param:ident: $ptype:ty$(,)?)*)$( -> $rtype:ty)?;)+ $(fn $name:ident($($param:ident: $ptype:ty$(,)?)*)$( -> $rtype:ty)?;)+
@ -95,7 +101,10 @@ mod types;
pub use functions::*; pub use functions::*;
pub use types::*; 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. /// Loads N-API symbols from host process.
/// Must be called at least once before using any functions in bindings or /// Must be called at least once before using any functions in bindings or
/// they will panic /// they will panic

View File

@ -48,6 +48,7 @@ napi = { path = "../../crates/napi", default-features = false, features = [
"node_version_detect", "node_version_detect",
"web_stream", "web_stream",
"error_anyhow", "error_anyhow",
"dyn-symbols",
] } ] }
[target.'cfg(target_family = "wasm")'.dependencies] [target.'cfg(target_family = "wasm")'.dependencies]