From eba0b52c59fc5d585d492ce9e7ebf1d8d7e2f3f7 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Mon, 7 Oct 2024 19:20:21 +0200 Subject: [PATCH] chore(napi): env doesn't need to be mut (#2292) --- crates/backend/src/codegen/struct.rs | 8 +++----- crates/napi/src/bindgen_runtime/js_values/class.rs | 2 +- crates/napi/src/env.rs | 8 ++++---- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/crates/backend/src/codegen/struct.rs b/crates/backend/src/codegen/struct.rs index 26aadc08..b1b7ff11 100644 --- a/crates/backend/src/codegen/struct.rs +++ b/crates/backend/src/codegen/struct.rs @@ -442,12 +442,10 @@ impl NapiStruct { let finalize_trait = if class.use_custom_finalize { quote! {} + } else if self.has_lifetime { + quote! { impl <'_javascript_function_scope> napi::bindgen_prelude::ObjectFinalize for #name<'_javascript_function_scope> {} } } else { - if self.has_lifetime { - quote! { impl <'_javascript_function_scope> napi::bindgen_prelude::ObjectFinalize for #name<'_javascript_function_scope> {} } - } else { - quote! { impl napi::bindgen_prelude::ObjectFinalize for #name {} } - } + quote! { impl napi::bindgen_prelude::ObjectFinalize for #name {} } }; let to_napi_value_impl = if self.has_lifetime { diff --git a/crates/napi/src/bindgen_runtime/js_values/class.rs b/crates/napi/src/bindgen_runtime/js_values/class.rs index eafd9f09..c1583ee4 100644 --- a/crates/napi/src/bindgen_runtime/js_values/class.rs +++ b/crates/napi/src/bindgen_runtime/js_values/class.rs @@ -24,7 +24,7 @@ impl<'env, T: 'env> ClassInstance<'env, T> { Self { value, inner } } - pub fn as_object(&self, env: Env) -> Object { + pub fn as_object(&self, env: &Env) -> Object { unsafe { Object::from_raw_unchecked(env.raw(), self.value) } } } diff --git a/crates/napi/src/env.rs b/crates/napi/src/env.rs index e8e4ae23..f319c8c4 100644 --- a/crates/napi/src/env.rs +++ b/crates/napi/src/env.rs @@ -402,7 +402,7 @@ impl Env { /// Registering externally allocated memory will trigger global garbage collections more often than it would otherwise. /// /// ***ATTENTION ⚠️***, do not use this with `create_buffer_with_data/create_arraybuffer_with_data`, since these two functions already called the `adjust_external_memory` internal. - pub fn adjust_external_memory(&mut self, size: i64) -> Result { + pub fn adjust_external_memory(&self, size: i64) -> Result { let mut changed = 0i64; check_status!(unsafe { sys::napi_adjust_external_memory(self.0, size, &mut changed) })?; Ok(changed) @@ -410,7 +410,7 @@ impl Env { #[cfg(target_family = "wasm")] #[allow(unused_variables)] - pub fn adjust_external_memory(&mut self, size: i64) -> Result { + pub fn adjust_external_memory(&self, size: i64) -> Result { Ok(0) } @@ -1025,7 +1025,7 @@ impl Env { #[cfg(feature = "napi3")] pub fn add_env_cleanup_hook( - &mut self, + &self, cleanup_data: T, cleanup_fn: F, ) -> Result> @@ -1049,7 +1049,7 @@ impl Env { } #[cfg(feature = "napi3")] - pub fn remove_env_cleanup_hook(&mut self, hook: CleanupEnvHook) -> Result<()> + pub fn remove_env_cleanup_hook(&self, hook: CleanupEnvHook) -> Result<()> where T: 'static, {