diff --git a/crates/backend/src/codegen/fn.rs b/crates/backend/src/codegen/fn.rs index edce9bf7..dd78ea07 100644 --- a/crates/backend/src/codegen/fn.rs +++ b/crates/backend/src/codegen/fn.rs @@ -126,8 +126,7 @@ impl TryToTokens for NapiFn { quote! { // constructor function is called from class `factory` // so we should skip the original `constructor` logic - let inner = napi::__private::___CALL_FROM_FACTORY.get_or_default(); - if inner.load(std::sync::atomic::Ordering::Relaxed) { + if napi::__private::___CALL_FROM_FACTORY.with(|inner| inner.load(std::sync::atomic::Ordering::Relaxed)) { return std::ptr::null_mut(); } #function_call_inner diff --git a/crates/backend/src/codegen/struct.rs b/crates/backend/src/codegen/struct.rs index fa9a150a..f8aa2221 100644 --- a/crates/backend/src/codegen/struct.rs +++ b/crates/backend/src/codegen/struct.rs @@ -352,14 +352,13 @@ impl NapiStruct { )?; let mut result = std::ptr::null_mut(); - let inner = napi::__private::___CALL_FROM_FACTORY.get_or_default(); - inner.store(true, std::sync::atomic::Ordering::Relaxed); + napi::__private::___CALL_FROM_FACTORY.with(|inner| inner.store(true, std::sync::atomic::Ordering::Relaxed)); napi::check_status!( napi::sys::napi_new_instance(env, ctor, 0, std::ptr::null_mut(), &mut result), "Failed to construct class `{}`", #js_name_raw )?; - inner.store(false, std::sync::atomic::Ordering::Relaxed); + napi::__private::___CALL_FROM_FACTORY.with(|inner| inner.store(false, std::sync::atomic::Ordering::Relaxed)); let mut object_ref = std::ptr::null_mut(); let initial_finalize: Box = Box::new(|| {}); let finalize_callbacks_ptr = std::rc::Rc::into_raw(std::rc::Rc::new(std::cell::Cell::new(Box::into_raw(initial_finalize)))); diff --git a/crates/napi/Cargo.toml b/crates/napi/Cargo.toml index 5b71bfa7..69668cf3 100644 --- a/crates/napi/Cargo.toml +++ b/crates/napi/Cargo.toml @@ -51,7 +51,6 @@ tokio_time = ["tokio/time"] [dependencies] ctor = "0.1" once_cell = "1.16" -thread_local = "1" bitflags = "1" [dependencies.anyhow] diff --git a/crates/napi/src/bindgen_runtime/callback_info.rs b/crates/napi/src/bindgen_runtime/callback_info.rs index f7c238a4..fd3a34ad 100644 --- a/crates/napi/src/bindgen_runtime/callback_info.rs +++ b/crates/napi/src/bindgen_runtime/callback_info.rs @@ -4,14 +4,13 @@ use std::ptr; use std::rc::Rc; use std::sync::atomic::{AtomicBool, Ordering}; -use once_cell::sync::Lazy; -use thread_local::ThreadLocal; - use crate::{bindgen_prelude::*, check_status, sys, Result}; -#[doc(hidden)] -/// Determined is `constructor` called from Class `factory` -pub static ___CALL_FROM_FACTORY: Lazy> = Lazy::new(ThreadLocal::new); +thread_local! { + #[doc(hidden)] + /// Determined is `constructor` called from Class `factory` + pub static ___CALL_FROM_FACTORY: AtomicBool = AtomicBool::new(false); +} pub struct CallbackInfo { env: sys::napi_env, @@ -144,11 +143,10 @@ impl CallbackInfo { ) -> Result<(sys::napi_value, *mut T)> { let this = self.this(); let mut instance = ptr::null_mut(); - let inner = ___CALL_FROM_FACTORY.get_or_default(); - inner.store(true, Ordering::Relaxed); + ___CALL_FROM_FACTORY.with(|s| s.store(true, Ordering::Relaxed)); let status = unsafe { sys::napi_new_instance(self.env, this, 0, ptr::null_mut(), &mut instance) }; - inner.store(false, Ordering::Relaxed); + ___CALL_FROM_FACTORY.with(|s| s.store(false, Ordering::Relaxed)); // Error thrown in `constructor` if status == sys::Status::napi_pending_exception { let mut exception = ptr::null_mut(); diff --git a/crates/napi/src/bindgen_runtime/module_register.rs b/crates/napi/src/bindgen_runtime/module_register.rs index f66ae1cc..ff4b8e1c 100644 --- a/crates/napi/src/bindgen_runtime/module_register.rs +++ b/crates/napi/src/bindgen_runtime/module_register.rs @@ -298,7 +298,6 @@ unsafe extern "C" fn napi_register_module_v1( } else { wait_first_thread_registered(); } - crate::__private::___CALL_FROM_FACTORY.get_or_default(); let mut exports_objects: HashSet = HashSet::default(); MODULE_REGISTER_CALLBACK.borrow_mut(|inner| { inner @@ -499,8 +498,7 @@ pub(crate) unsafe extern "C" fn noop( env: sys::napi_env, _info: sys::napi_callback_info, ) -> sys::napi_value { - let inner = crate::bindgen_runtime::___CALL_FROM_FACTORY.get_or_default(); - if !inner.load(Ordering::Relaxed) { + if !crate::bindgen_runtime::___CALL_FROM_FACTORY.with(|s| s.load(Ordering::Relaxed)) { unsafe { sys::napi_throw_error( env,