diff --git a/napi/src/env.rs b/napi/src/env.rs index 236e4d83..9c37a071 100644 --- a/napi/src/env.rs +++ b/napi/src/env.rs @@ -179,17 +179,19 @@ impl Env { check_status(status)?; mem::forget(data); - let mut buffer = JsBuffer::from_raw_unchecked(self.0, raw_value); - buffer.data = data_ptr as *const u8; - buffer.len = length; - Ok(buffer) + Ok(JsBuffer::from_raw_unchecked( + self.0, + raw_value, + data_ptr as *mut u8, + length as usize, + )) } #[inline] - pub fn create_buffer_with_data(&self, data: Vec) -> Result { + pub fn create_buffer_with_data(&self, mut data: Vec) -> Result { let length = data.len() as u64; let mut raw_value = ptr::null_mut(); - let data_ptr = data.as_ptr(); + let data_ptr = data.as_mut_ptr(); let status = unsafe { sys::napi_create_external_buffer( self.0, @@ -206,10 +208,12 @@ impl Env { unsafe { sys::napi_adjust_external_memory(self.0, length as i64, &mut changed) }; check_status(adjust_external_memory_status)?; mem::forget(data); - let mut buffer = JsBuffer::from_raw_unchecked(self.0, raw_value); - buffer.data = data_ptr as *const u8; - buffer.len = length; - Ok(buffer) + Ok(JsBuffer::from_raw_unchecked( + self.0, + raw_value, + data_ptr, + length as usize, + )) } #[inline] diff --git a/napi/src/js_values/arraybuffer.rs b/napi/src/js_values/arraybuffer.rs index c5b40617..a058c824 100644 --- a/napi/src/js_values/arraybuffer.rs +++ b/napi/src/js_values/arraybuffer.rs @@ -4,7 +4,7 @@ use super::{JsObject, NapiValue, Value, ValueType}; use crate::error::check_status; use crate::{sys, Result}; -#[derive(Clone, Copy, Debug)] +#[derive(Debug)] pub struct JsArrayBuffer { pub value: JsObject, pub data: *const u8, diff --git a/napi/src/js_values/boolean.rs b/napi/src/js_values/boolean.rs index b9ab7142..d4f43cc2 100644 --- a/napi/src/js_values/boolean.rs +++ b/napi/src/js_values/boolean.rs @@ -4,7 +4,7 @@ use super::Value; use crate::error::check_status; use crate::{sys, Error, Result}; -#[derive(Clone, Copy, Debug)] +#[derive(Debug)] pub struct JsBoolean(pub(crate) Value); impl JsBoolean { diff --git a/napi/src/js_values/buffer.rs b/napi/src/js_values/buffer.rs index 1c70f80f..8091ae5a 100644 --- a/napi/src/js_values/buffer.rs +++ b/napi/src/js_values/buffer.rs @@ -1,4 +1,4 @@ -use std::ops::{Deref, DerefMut}; +use std::ops::Deref; use std::ptr; use std::slice; @@ -6,23 +6,26 @@ use super::{JsObject, JsUnknown, NapiValue, Value, ValueType}; use crate::error::check_status; use crate::{sys, Result}; -#[derive(Clone, Copy, Debug)] +#[derive(Debug)] pub struct JsBuffer { pub value: JsObject, - pub data: *const u8, - pub len: u64, + pub data: &'static [u8], } impl JsBuffer { - pub(crate) fn from_raw_unchecked(env: sys::napi_env, value: sys::napi_value) -> Self { + pub(crate) fn from_raw_unchecked( + env: sys::napi_env, + value: sys::napi_value, + data: *mut u8, + len: usize, + ) -> Self { Self { value: JsObject(Value { env, value, value_type: ValueType::Object, }), - data: ptr::null(), - len: 0, + data: unsafe { slice::from_raw_parts_mut(data, len) }, } } @@ -47,15 +50,14 @@ impl NapiValue for JsBuffer { value, value_type: ValueType::Object, }), - data: data as *const u8, - len, + data: unsafe { slice::from_raw_parts_mut(data as *mut _, len as usize) }, }) } } impl AsRef<[u8]> for JsBuffer { fn as_ref(&self) -> &[u8] { - self.deref() + self.data } } @@ -63,12 +65,6 @@ impl Deref for JsBuffer { type Target = [u8]; fn deref(&self) -> &[u8] { - unsafe { slice::from_raw_parts(self.data, self.len as usize) } - } -} - -impl DerefMut for JsBuffer { - fn deref_mut(&mut self) -> &mut [u8] { - unsafe { slice::from_raw_parts_mut(self.data as *mut _, self.len as usize) } + self.data } } diff --git a/napi/src/js_values/class_property.rs b/napi/src/js_values/class_property.rs index 73cbfeda..2f57eea6 100644 --- a/napi/src/js_values/class_property.rs +++ b/napi/src/js_values/class_property.rs @@ -2,7 +2,7 @@ use std::ptr; use crate::{sys, Callback, Env, NapiValue, Result}; -#[derive(Clone, Debug)] +#[derive(Debug)] pub struct Property { name: String, raw_descriptor: sys::napi_property_descriptor, diff --git a/napi/src/js_values/function.rs b/napi/src/js_values/function.rs index 7f3a6c8e..c3497efa 100644 --- a/napi/src/js_values/function.rs +++ b/napi/src/js_values/function.rs @@ -5,7 +5,7 @@ use super::Value; use crate::error::check_status; use crate::{sys, Env, Error, JsObject, JsUnknown, NapiValue, Result, Status}; -#[derive(Clone, Copy, Debug)] +#[derive(Debug)] pub struct JsFunction(pub(crate) Value); /// See [Working with JavaScript Functions](https://nodejs.org/api/n-api.html#n_api_working_with_javascript_functions). diff --git a/napi/src/js_values/mod.rs b/napi/src/js_values/mod.rs index 63fc7cf1..f0963d25 100644 --- a/napi/src/js_values/mod.rs +++ b/napi/src/js_values/mod.rs @@ -35,20 +35,20 @@ pub(crate) use value_ref::Ref; pub use value_type::ValueType; // Value types -#[derive(Clone, Copy, Debug)] +#[derive(Debug)] pub struct JsUnknown(pub(crate) Value); -#[derive(Clone, Copy, Debug)] +#[derive(Debug)] pub struct JsNull(pub(crate) Value); #[cfg(napi6)] -#[derive(Clone, Copy, Debug)] +#[derive(Debug)] pub struct JsBigint(pub(crate) Value); -#[derive(Clone, Copy, Debug)] +#[derive(Debug)] pub struct JsSymbol(pub(crate) Value); -#[derive(Clone, Copy, Debug)] +#[derive(Debug)] pub struct JsExternal(pub(crate) Value); #[inline] diff --git a/napi/src/js_values/number.rs b/napi/src/js_values/number.rs index c0486aab..81ae1abe 100644 --- a/napi/src/js_values/number.rs +++ b/napi/src/js_values/number.rs @@ -4,7 +4,7 @@ use super::Value; use crate::error::check_status; use crate::{sys, Error, Result}; -#[derive(Clone, Copy, Debug)] +#[derive(Debug)] pub struct JsNumber(pub(crate) Value); impl TryFrom for usize { diff --git a/napi/src/js_values/object.rs b/napi/src/js_values/object.rs index c81b4ad6..0a1144d4 100644 --- a/napi/src/js_values/object.rs +++ b/napi/src/js_values/object.rs @@ -5,7 +5,7 @@ use super::Value; use crate::error::check_status; use crate::{sys, Env, Error, JsBuffer, JsNumber, JsString, NapiValue, Result, Status}; -#[derive(Clone, Copy, Debug)] +#[derive(Debug)] pub struct JsObject(pub(crate) Value); impl JsObject { diff --git a/napi/src/js_values/string.rs b/napi/src/js_values/string.rs index 1587582b..da8f599f 100644 --- a/napi/src/js_values/string.rs +++ b/napi/src/js_values/string.rs @@ -8,7 +8,7 @@ use super::Value; use crate::error::check_status; use crate::{sys, Error, Result, Status}; -#[derive(Clone, Copy, Debug)] +#[derive(Debug)] pub struct JsString(pub(crate) Value); impl JsString { diff --git a/napi/src/js_values/undefined.rs b/napi/src/js_values/undefined.rs index adafb5d0..4f32b746 100644 --- a/napi/src/js_values/undefined.rs +++ b/napi/src/js_values/undefined.rs @@ -1,4 +1,4 @@ use super::Value; -#[derive(Clone, Copy, Debug)] +#[derive(Debug)] pub struct JsUndefined(pub(crate) Value); diff --git a/napi/src/js_values/value.rs b/napi/src/js_values/value.rs index d4d3645c..fb9f8137 100644 --- a/napi/src/js_values/value.rs +++ b/napi/src/js_values/value.rs @@ -2,7 +2,7 @@ use crate::sys; use super::ValueType; -#[derive(Clone, Copy, Debug)] +#[derive(Debug)] pub struct Value { pub env: sys::napi_env, pub value: sys::napi_value, diff --git a/napi/src/lib.rs b/napi/src/lib.rs index b34fc603..686e0485 100644 --- a/napi/src/lib.rs +++ b/napi/src/lib.rs @@ -159,7 +159,7 @@ macro_rules! register_module { let hook_result = Ok(()); match hook_result.and_then(move |_| result) { - Ok(_) => exports.raw_value(), + Ok(_) => cjs_module.exports.raw_value(), Err(e) => { unsafe { sys::napi_throw_error( diff --git a/test_module/src/function.rs b/test_module/src/function.rs index 733f7f8e..ef9a9596 100644 --- a/test_module/src/function.rs +++ b/test_module/src/function.rs @@ -1,4 +1,4 @@ -use napi::{JsFunction, CallContext, JsNull, Result, JsObject}; +use napi::{CallContext, JsFunction, JsNull, JsObject, Result}; #[js_function(1)] pub fn call_function(ctx: CallContext) -> Result { @@ -13,10 +13,10 @@ pub fn call_function(ctx: CallContext) -> Result { #[js_function(1)] pub fn call_function_with_this(ctx: CallContext) -> Result { - let js_this = ctx.this; + let js_this = &ctx.this; let js_func = ctx.get::(0)?; - js_func.call(Some(&js_this), &[])?; + js_func.call(Some(js_this), &[])?; ctx.env.get_null() }