-
-
Notifications
You must be signed in to change notification settings - Fork 601
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: one shot ref for class instance
- Loading branch information
Showing
5 changed files
with
122 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod js_value_ref; | ||
pub mod one_shot_instance_ref; | ||
pub mod one_shot_value_ref; | ||
pub mod value_ref; |
106 changes: 106 additions & 0 deletions
106
crates/rspack_napi/src/js_values/one_shot_instance_ref.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
#![allow(clippy::not_unsafe_ptr_arg_deref)] | ||
|
||
use std::cell::{Cell, RefCell}; | ||
use std::marker::PhantomData; | ||
use std::ops::{Deref, DerefMut}; | ||
use std::ptr; | ||
|
||
use napi::bindgen_prelude::{check_status, JavaScriptClassExt, ToNapiValue}; | ||
use napi::sys::{self, napi_env}; | ||
use napi::{CleanupEnvHook, Env, Result}; | ||
|
||
thread_local! { | ||
static CLEANUP_ENV_HOOK: RefCell<Option<CleanupEnvHook<()>>> = Default::default(); | ||
|
||
// cleanup references to be executed when the JS thread exits normally | ||
static GLOBAL_CLEANUP_FLAG: Cell<bool> = const { Cell::new(false) }; | ||
} | ||
|
||
// A RAII (Resource Acquisition Is Initialization) style wrapper around `Ref` that ensures the | ||
// reference is unreferenced when it goes out of scope. This struct maintains a single reference | ||
// count and automatically cleans up when it is dropped. | ||
pub struct OneShotInstanceRef<T: 'static> { | ||
env: napi_env, | ||
napi_ref: sys::napi_ref, | ||
inner: *mut T, | ||
ty: PhantomData<T>, | ||
} | ||
|
||
impl<T: JavaScriptClassExt + 'static> OneShotInstanceRef<T> { | ||
pub fn new(env: napi_env, val: T) -> Result<Self> { | ||
let env_wrapper = Env::from_raw(env); | ||
let mut instance = val.into_instance(&env_wrapper)?; | ||
|
||
let mut napi_ref = ptr::null_mut(); | ||
check_status!(unsafe { sys::napi_create_reference(env, instance.value, 1, &mut napi_ref) })?; | ||
|
||
CLEANUP_ENV_HOOK.with(|ref_cell| { | ||
if ref_cell.borrow().is_none() { | ||
let result = env_wrapper.add_env_cleanup_hook((), move |_| { | ||
CLEANUP_ENV_HOOK.with_borrow_mut(|cleanup_env_hook| *cleanup_env_hook = None); | ||
GLOBAL_CLEANUP_FLAG.set(true); | ||
}); | ||
if let Ok(cleanup_env_hook) = result { | ||
*ref_cell.borrow_mut() = Some(cleanup_env_hook); | ||
} | ||
} | ||
}); | ||
|
||
Ok(Self { | ||
env, | ||
napi_ref, | ||
inner: &mut *instance, | ||
ty: PhantomData, | ||
}) | ||
} | ||
} | ||
|
||
impl<T> Drop for OneShotInstanceRef<T> { | ||
fn drop(&mut self) { | ||
if !GLOBAL_CLEANUP_FLAG.get() { | ||
unsafe { sys::napi_delete_reference(self.env, self.napi_ref) }; | ||
} | ||
} | ||
} | ||
|
||
impl<T: ToNapiValue> ToNapiValue for &OneShotInstanceRef<T> { | ||
unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> Result<sys::napi_value> { | ||
let mut result = ptr::null_mut(); | ||
check_status!( | ||
unsafe { sys::napi_get_reference_value(env, val.napi_ref, &mut result) }, | ||
"Failed to get reference value" | ||
)?; | ||
Ok(result) | ||
} | ||
} | ||
|
||
impl<T: ToNapiValue> ToNapiValue for &mut OneShotInstanceRef<T> { | ||
unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> Result<sys::napi_value> { | ||
let mut result = ptr::null_mut(); | ||
check_status!( | ||
unsafe { sys::napi_get_reference_value(env, val.napi_ref, &mut result) }, | ||
"Failed to get reference value" | ||
)?; | ||
Ok(result) | ||
} | ||
} | ||
|
||
impl<T> Deref for OneShotInstanceRef<T> { | ||
type Target = T; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
unsafe { &*self.inner } | ||
} | ||
} | ||
|
||
impl<T> DerefMut for OneShotInstanceRef<T> { | ||
fn deref_mut(&mut self) -> &mut T { | ||
unsafe { &mut *self.inner } | ||
} | ||
} | ||
|
||
impl<T> AsRef<T> for OneShotInstanceRef<T> { | ||
fn as_ref(&self) -> &T { | ||
unsafe { &*self.inner } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters