diff --git a/crates/neon/src/handle/mod.rs b/crates/neon/src/handle/mod.rs index e8564abec..f760d7129 100644 --- a/crates/neon/src/handle/mod.rs +++ b/crates/neon/src/handle/mod.rs @@ -48,8 +48,8 @@ pub(crate) mod internal; -pub(crate) mod root; -pub(crate) mod root_global; +pub(crate) mod root_object; +pub(crate) mod root_value; use std::{ error::Error, @@ -59,8 +59,8 @@ use std::{ ops::{Deref, DerefMut}, }; -pub use self::root::Root; -pub use self::root_global::*; +pub use self::root_object::Root; +pub use self::root_value::*; use crate::{ context::Context, diff --git a/crates/neon/src/handle/root.rs b/crates/neon/src/handle/root_object.rs similarity index 100% rename from crates/neon/src/handle/root.rs rename to crates/neon/src/handle/root_object.rs diff --git a/crates/neon/src/handle/root_global.rs b/crates/neon/src/handle/root_value.rs similarity index 72% rename from crates/neon/src/handle/root_global.rs rename to crates/neon/src/handle/root_value.rs index 6c8e25ac6..83aabb533 100644 --- a/crates/neon/src/handle/root_global.rs +++ b/crates/neon/src/handle/root_value.rs @@ -12,13 +12,10 @@ use crate::result::JsResult; use crate::result::NeonResult; use crate::types::JsFunction; use crate::types::JsObject; -use crate::types::JsSymbol; - -static KEY_NEON_CACHE: &str = "__neon_cache"; thread_local! { // Symbol("__neon_cache") - static CACHE_SYMBOL: OnceCell> = OnceCell::default(); + static NEON_CACHE: OnceCell> = OnceCell::default(); } /// Reference counted JavaScript value with a static lifetime for use in async closures @@ -68,47 +65,25 @@ impl RootGlobal { } } -/* - globalThis = { - [key: Symbol("__neon_cache")]: Set - } -*/ fn get_cache<'a>(cx: &mut impl Context<'a>) -> JsResult<'a, JsObject> { - let global_this = cx.global_object(); - - let neon_cache_symbol = CACHE_SYMBOL.with({ + let neon_cache = NEON_CACHE.with({ |raw_value| { raw_value - .get_or_try_init(|| -> NeonResult> { - let set_ctor = global_this.get::(cx, "Map")?; + .get_or_try_init(|| -> NeonResult> { + let set_ctor = cx.global_object().get::(cx, "Map")?; let neon_cache = set_ctor.construct(cx, &[])?; - - let symbol = cx.symbol(KEY_NEON_CACHE); - let symbol = symbol.root(cx); - - { - let symbol = symbol.clone(cx); - let symbol = symbol.into_inner(cx); - global_this.set(cx, symbol, neon_cache)?; - } - - Ok(symbol) + Ok(neon_cache.root(cx)) }) .and_then(|e| Ok(e.clone(cx))) } })?; - let neon_cache_symbol = neon_cache_symbol.into_inner(cx); - - let Some(neon_cache) = global_this.get_opt::(cx, neon_cache_symbol)? else { - return Err(cx.throw_error("Unable to find cache")?); - }; - - Ok(neon_cache) + Ok(neon_cache.into_inner(cx)) } fn set_ref<'a, V: Value>(cx: &mut impl Context<'a>, value: Handle<'a, V>) -> NeonResult { let neon_cache = get_cache(cx)?; + // Is this safe? let key = format!("{:?}", value.to_local()); get_cache(cx)? diff --git a/crates/neon/src/lifecycle.rs b/crates/neon/src/lifecycle.rs index 0a08ef922..6399c2295 100644 --- a/crates/neon/src/lifecycle.rs +++ b/crates/neon/src/lifecycle.rs @@ -20,7 +20,7 @@ use std::{ use crate::{ context::Context, event::Channel, - handle::root::NapiRef, + handle::root_object::NapiRef, sys::{lifecycle, raw::Env, tsfn::ThreadsafeFunction}, types::promise::NodeApiDeferred, };