From 070230079d75fd72b661208a45eaecaa0ce0a43f Mon Sep 17 00:00:00 2001 From: LongYinan Date: Tue, 11 Apr 2023 11:44:10 +0800 Subject: [PATCH] fix(napi): noop feature --- crates/napi/Cargo.toml | 2 +- .../napi/src/bindgen_runtime/module_register.rs | 17 +++++++++++------ crates/napi/src/lib.rs | 2 +- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/crates/napi/Cargo.toml b/crates/napi/Cargo.toml index 8b9251a7..ab39abf1 100644 --- a/crates/napi/Cargo.toml +++ b/crates/napi/Cargo.toml @@ -53,7 +53,7 @@ tokio_test_util = ["tokio/test-util"] tokio_time = ["tokio/time"] [dependencies] -ctor = "0.1" +ctor = "0.2" once_cell = "1.16" bitflags = "2" diff --git a/crates/napi/src/bindgen_runtime/module_register.rs b/crates/napi/src/bindgen_runtime/module_register.rs index 2813c2af..5ef58a8e 100644 --- a/crates/napi/src/bindgen_runtime/module_register.rs +++ b/crates/napi/src/bindgen_runtime/module_register.rs @@ -1,6 +1,6 @@ use std::collections::{HashMap, HashSet}; use std::ffi::CStr; -#[cfg(all(feature = "napi4", not(target_arch = "wasm32")))] +#[cfg(all(feature = "napi4", not(target_arch = "wasm32"), not(feature = "noop")))] use std::hash::Hash; #[cfg(all(feature = "napi4", not(target_arch = "wasm32")))] use std::ops::Deref; @@ -10,10 +10,9 @@ use std::thread::ThreadId; use once_cell::sync::Lazy; -use crate::{ - check_status, check_status_or_throw, sys, Env, JsError, JsFunction, Property, Result, Value, - ValueType, -}; +use crate::{check_status, sys, Env, JsFunction, Property, Result, Value, ValueType}; +#[cfg(not(feature = "noop"))] +use crate::{check_status_or_throw, JsError}; pub type ExportRegisterCallback = unsafe fn(sys::napi_env) -> Result; pub type ModuleExportsCallback = @@ -37,6 +36,7 @@ impl Default for PersistedPerInstanceVec { } impl PersistedPerInstanceVec { + #[cfg(not(feature = "noop"))] #[allow(clippy::mut_from_ref)] fn borrow_mut(&self, f: F) where @@ -81,6 +81,7 @@ pub(crate) struct PersistedPerInstanceHashSet { inner: *mut HashSet, } +#[cfg(not(feature = "noop"))] #[cfg(all(feature = "napi4", not(target_arch = "wasm32")))] impl PersistedPerInstanceHashSet { pub(crate) fn insert(&self, item: T) { @@ -118,6 +119,7 @@ unsafe impl Sync for PersistedPerInstanceHashSet {} pub(crate) struct PersistedPerInstanceHashMap(*mut HashMap); impl PersistedPerInstanceHashMap { + #[cfg(not(feature = "noop"))] pub(crate) fn from_hashmap(hashmap: HashMap) -> Self { Self(Box::into_raw(Box::new(hashmap))) } @@ -155,7 +157,9 @@ type RegisteredClassesMap = PersistedPerInstanceHashMap = Lazy::new(Default::default); static MODULE_CLASS_PROPERTIES: Lazy = Lazy::new(Default::default); +#[cfg(not(feature = "noop"))] static IS_FIRST_MODULE: AtomicBool = AtomicBool::new(true); +#[cfg(not(feature = "noop"))] static FIRST_MODULE_REGISTERED: AtomicBool = AtomicBool::new(false); static REGISTERED_CLASSES: Lazy = Lazy::new(Default::default); static FN_REGISTER_MAP: Lazy = Lazy::new(Default::default); @@ -177,11 +181,12 @@ pub(crate) static THREADS_CAN_ACCESS_ENV: once_cell::sync::OnceCell< type RegisteredClasses = PersistedPerInstanceHashMap; -#[cfg(feature = "compat-mode")] +#[cfg(all(feature = "compat-mode", not(feature = "noop")))] // compatibility for #[module_exports] static MODULE_EXPORTS: Lazy> = Lazy::new(Default::default); +#[cfg(not(feature = "noop"))] #[inline] fn wait_first_thread_registered() { while !FIRST_MODULE_REGISTERED.load(Ordering::SeqCst) { diff --git a/crates/napi/src/lib.rs b/crates/napi/src/lib.rs index 09323b75..c1ea8be8 100644 --- a/crates/napi/src/lib.rs +++ b/crates/napi/src/lib.rs @@ -151,7 +151,7 @@ macro_rules! assert_type_of { pub use crate::bindgen_runtime::ctor as module_init; pub mod bindgen_prelude { - #[cfg(feature = "compat-mode")] + #[cfg(all(feature = "compat-mode", not(feature = "noop")))] pub use crate::bindgen_runtime::register_module_exports; #[cfg(feature = "tokio_rt")] pub use crate::tokio_runtime::*;