chore(napi): remove thread_local from dependenies (#1506)

This commit is contained in:
LongYinan 2023-03-05 19:49:52 +08:00 committed by GitHub
parent 3bd3c9cc08
commit 96959e6425
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 11 additions and 18 deletions

View file

@ -126,8 +126,7 @@ impl TryToTokens for NapiFn {
quote! { quote! {
// constructor function is called from class `factory` // constructor function is called from class `factory`
// so we should skip the original `constructor` logic // so we should skip the original `constructor` logic
let inner = napi::__private::___CALL_FROM_FACTORY.get_or_default(); if napi::__private::___CALL_FROM_FACTORY.with(|inner| inner.load(std::sync::atomic::Ordering::Relaxed)) {
if inner.load(std::sync::atomic::Ordering::Relaxed) {
return std::ptr::null_mut(); return std::ptr::null_mut();
} }
#function_call_inner #function_call_inner

View file

@ -352,14 +352,13 @@ impl NapiStruct {
)?; )?;
let mut result = std::ptr::null_mut(); let mut result = std::ptr::null_mut();
let inner = napi::__private::___CALL_FROM_FACTORY.get_or_default(); napi::__private::___CALL_FROM_FACTORY.with(|inner| inner.store(true, std::sync::atomic::Ordering::Relaxed));
inner.store(true, std::sync::atomic::Ordering::Relaxed);
napi::check_status!( napi::check_status!(
napi::sys::napi_new_instance(env, ctor, 0, std::ptr::null_mut(), &mut result), napi::sys::napi_new_instance(env, ctor, 0, std::ptr::null_mut(), &mut result),
"Failed to construct class `{}`", "Failed to construct class `{}`",
#js_name_raw #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 mut object_ref = std::ptr::null_mut();
let initial_finalize: Box<dyn FnOnce()> = Box::new(|| {}); let initial_finalize: Box<dyn FnOnce()> = Box::new(|| {});
let finalize_callbacks_ptr = std::rc::Rc::into_raw(std::rc::Rc::new(std::cell::Cell::new(Box::into_raw(initial_finalize)))); let finalize_callbacks_ptr = std::rc::Rc::into_raw(std::rc::Rc::new(std::cell::Cell::new(Box::into_raw(initial_finalize))));

View file

@ -51,7 +51,6 @@ tokio_time = ["tokio/time"]
[dependencies] [dependencies]
ctor = "0.1" ctor = "0.1"
once_cell = "1.16" once_cell = "1.16"
thread_local = "1"
bitflags = "1" bitflags = "1"
[dependencies.anyhow] [dependencies.anyhow]

View file

@ -4,14 +4,13 @@ use std::ptr;
use std::rc::Rc; use std::rc::Rc;
use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::atomic::{AtomicBool, Ordering};
use once_cell::sync::Lazy;
use thread_local::ThreadLocal;
use crate::{bindgen_prelude::*, check_status, sys, Result}; use crate::{bindgen_prelude::*, check_status, sys, Result};
#[doc(hidden)] thread_local! {
/// Determined is `constructor` called from Class `factory` #[doc(hidden)]
pub static ___CALL_FROM_FACTORY: Lazy<ThreadLocal<AtomicBool>> = Lazy::new(ThreadLocal::new); /// Determined is `constructor` called from Class `factory`
pub static ___CALL_FROM_FACTORY: AtomicBool = AtomicBool::new(false);
}
pub struct CallbackInfo<const N: usize> { pub struct CallbackInfo<const N: usize> {
env: sys::napi_env, env: sys::napi_env,
@ -144,11 +143,10 @@ impl<const N: usize> CallbackInfo<N> {
) -> Result<(sys::napi_value, *mut T)> { ) -> Result<(sys::napi_value, *mut T)> {
let this = self.this(); let this = self.this();
let mut instance = ptr::null_mut(); let mut instance = ptr::null_mut();
let inner = ___CALL_FROM_FACTORY.get_or_default(); ___CALL_FROM_FACTORY.with(|s| s.store(true, Ordering::Relaxed));
inner.store(true, Ordering::Relaxed);
let status = let status =
unsafe { sys::napi_new_instance(self.env, this, 0, ptr::null_mut(), &mut instance) }; 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` // Error thrown in `constructor`
if status == sys::Status::napi_pending_exception { if status == sys::Status::napi_pending_exception {
let mut exception = ptr::null_mut(); let mut exception = ptr::null_mut();

View file

@ -298,7 +298,6 @@ unsafe extern "C" fn napi_register_module_v1(
} else { } else {
wait_first_thread_registered(); wait_first_thread_registered();
} }
crate::__private::___CALL_FROM_FACTORY.get_or_default();
let mut exports_objects: HashSet<String> = HashSet::default(); let mut exports_objects: HashSet<String> = HashSet::default();
MODULE_REGISTER_CALLBACK.borrow_mut(|inner| { MODULE_REGISTER_CALLBACK.borrow_mut(|inner| {
inner inner
@ -499,8 +498,7 @@ pub(crate) unsafe extern "C" fn noop(
env: sys::napi_env, env: sys::napi_env,
_info: sys::napi_callback_info, _info: sys::napi_callback_info,
) -> sys::napi_value { ) -> sys::napi_value {
let inner = crate::bindgen_runtime::___CALL_FROM_FACTORY.get_or_default(); if !crate::bindgen_runtime::___CALL_FROM_FACTORY.with(|s| s.load(Ordering::Relaxed)) {
if !inner.load(Ordering::Relaxed) {
unsafe { unsafe {
sys::napi_throw_error( sys::napi_throw_error(
env, env,