From ae8de8a63124e8c6a331c9832bc349b40c58b63a Mon Sep 17 00:00:00 2001 From: LongYinan Date: Sun, 11 Oct 2020 22:15:58 +0800 Subject: [PATCH] feat(napi): implement dataview related apis --- README.md | 4 +- napi/src/js_values/arraybuffer.rs | 81 ++++++++++++++++++++++++++----- napi/src/js_values/buffer.rs | 2 +- napi/src/js_values/mod.rs | 2 + 4 files changed, 73 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 51070c76..ef1fe192 100644 --- a/README.md +++ b/README.md @@ -164,7 +164,7 @@ yarn test | [napi_create_object](https://nodejs.org/api/n-api.html#n_api_napi_create_object) | 1 | v8.0.0 | ✅ | | [napi_create_symbol](https://nodejs.org/api/n-api.html#n_api_napi_create_symbol) | 1 | v8.0.0 | ✅ | | [napi_create_typedarray](https://nodejs.org/api/n-api.html#n_api_napi_create_typedarray) | 1 | v8.0.0 | ✅ | -| [napi_create_dataview](https://nodejs.org/api/n-api.html#n_api_napi_create_dataview) | 1 | v8.3.0 | ⛔️ | +| [napi_create_dataview](https://nodejs.org/api/n-api.html#n_api_napi_create_dataview) | 1 | v8.3.0 | ✅ | | [napi_create_int32](https://nodejs.org/api/n-api.html#n_api_napi_create_int32) | 1 | v8.4.0 | ✅ | | [napi_create_uint32](https://nodejs.org/api/n-api.html#n_api_napi_create_uint32) | 1 | v8.4.0 | ✅ | | [napi_create_int64](https://nodejs.org/api/n-api.html#n_api_napi_create_int64) | 1 | v8.4.0 | ✅ | @@ -185,7 +185,7 @@ yarn test | [napi_get_buffer_info](https://nodejs.org/api/n-api.html#n_api_napi_get_buffer_info) | 1 | v8.0.0 | ✅ | | [napi_get_prototype](https://nodejs.org/api/n-api.html#n_api_napi_get_prototype) | 1 | v8.0.0 | ✅ | | [napi_get_typedarray_info](https://nodejs.org/api/n-api.html#n_api_napi_get_typedarray_info) | 1 | v8.0.0 | ✅ | -| [napi_get_dataview_info](https://nodejs.org/api/n-api.html#n_api_napi_get_dataview_info) | 1 | v8.3.0 | ⛔️ | +| [napi_get_dataview_info](https://nodejs.org/api/n-api.html#n_api_napi_get_dataview_info) | 1 | v8.3.0 | ✅ | | [napi_get_date_value](https://nodejs.org/api/n-api.html#n_api_napi_get_date_value) | 5 | v11.11.0 | ✅ | | [napi_get_value_bool](https://nodejs.org/api/n-api.html#n_api_napi_get_value_bool) | 1 | v8.0.0 | ✅ | | [napi_get_value_double](https://nodejs.org/api/n-api.html#n_api_napi_get_value_double) | 1 | v8.0.0 | ✅ | diff --git a/napi/src/js_values/arraybuffer.rs b/napi/src/js_values/arraybuffer.rs index 3aac6a65..f59faa27 100644 --- a/napi/src/js_values/arraybuffer.rs +++ b/napi/src/js_values/arraybuffer.rs @@ -1,5 +1,6 @@ use std::mem; use std::ops::Deref; +use std::os::raw::c_void; use std::ptr; use super::{Value, ValueType}; @@ -7,7 +8,7 @@ use crate::error::check_status; use crate::{sys, JsUnknown, NapiValue, Ref, Result}; #[repr(transparent)] -#[derive(Debug, Clone, Copy)] +#[derive(Debug)] pub struct JsArrayBuffer(pub(crate) Value); #[derive(Debug)] @@ -17,18 +18,30 @@ pub struct JsArrayBufferValue { } #[repr(transparent)] -#[derive(Debug, Clone, Copy)] +#[derive(Debug)] pub struct JsTypedArray(pub(crate) Value); #[derive(Debug)] -pub struct JsTypedArrayValue { +pub struct JsTypedArrayValue { pub arraybuffer: JsArrayBuffer, - pub data: mem::ManuallyDrop>, + data: *mut c_void, pub byte_offset: u64, pub length: u64, pub typedarray_type: TypedArrayType, } +#[repr(transparent)] +#[derive(Debug)] +pub struct JsDataView(pub(crate) Value); + +#[derive(Debug)] +pub struct JsDataViewValue { + pub arraybuffer: JsArrayBuffer, + data: *mut c_void, + pub byte_offset: u64, + pub length: u64, +} + #[derive(Debug)] pub enum TypedArrayType { Int8, @@ -101,8 +114,8 @@ impl JsArrayBuffer { }) } - pub fn create_typed_array( - &self, + pub fn into_typedarray( + self, typedarray_type: TypedArrayType, length: u64, byte_offset: u64, @@ -125,6 +138,24 @@ impl JsArrayBuffer { })) } + pub fn into_dataview(self, length: u64, byte_offset: u64) -> Result { + let mut dataview_value = ptr::null_mut(); + check_status(unsafe { + sys::napi_create_dataview( + self.0.env, + length, + self.0.value, + byte_offset, + &mut dataview_value, + ) + })?; + Ok(JsDataView(Value { + env: self.0.env, + value: dataview_value, + value_type: ValueType::Object, + })) + } + #[inline] pub fn into_ref(self) -> Result> { Ref::new(self.0, 1, self.into_value()?) @@ -167,11 +198,11 @@ impl JsTypedArray { /// https://nodejs.org/api/n-api.html#n_api_napi_get_typedarray_info /// /// ***Warning***: Use caution while using this API since the underlying data buffer is managed by the VM. - pub fn into_value(self) -> Result> { + pub fn into_value(self) -> Result { let mut typedarray_type = sys::napi_typedarray_type::napi_int8_array; let mut len = 0u64; let mut data = ptr::null_mut(); - let mut array_buffer = ptr::null_mut(); + let mut arraybuffer_value = ptr::null_mut(); let mut byte_offset = 0u64; check_status(unsafe { sys::napi_get_typedarray_info( @@ -180,19 +211,43 @@ impl JsTypedArray { &mut typedarray_type, &mut len, &mut data, - &mut array_buffer, + &mut arraybuffer_value, &mut byte_offset, ) })?; Ok(JsTypedArrayValue { - data: mem::ManuallyDrop::new(unsafe { - Vec::from_raw_parts(data as *mut T, len as usize, len as usize) - }), + data, length: len, byte_offset, typedarray_type: typedarray_type.into(), - arraybuffer: JsArrayBuffer::from_raw_unchecked(self.0.env, array_buffer), + arraybuffer: JsArrayBuffer::from_raw_unchecked(self.0.env, arraybuffer_value), + }) + } +} + +impl JsDataView { + pub fn into_value(self) -> Result { + let mut length = 0u64; + let mut byte_offset = 0u64; + let mut arraybuffer_value = ptr::null_mut(); + let mut data = ptr::null_mut(); + + check_status(unsafe { + sys::napi_get_dataview_info( + self.0.env, + self.0.value, + &mut length, + &mut data, + &mut arraybuffer_value, + &mut byte_offset, + ) + })?; + Ok(JsDataViewValue { + arraybuffer: JsArrayBuffer::from_raw_unchecked(self.0.env, arraybuffer_value), + byte_offset, + length, + data, }) } } diff --git a/napi/src/js_values/buffer.rs b/napi/src/js_values/buffer.rs index b0eac81d..14dd3218 100644 --- a/napi/src/js_values/buffer.rs +++ b/napi/src/js_values/buffer.rs @@ -9,7 +9,7 @@ use crate::error::check_status; use crate::{sys, JsUnknown, NapiValue, Ref, Result}; #[repr(transparent)] -#[derive(Debug, Clone, Copy)] +#[derive(Debug)] pub struct JsBuffer(pub(crate) Value); #[derive(Debug)] diff --git a/napi/src/js_values/mod.rs b/napi/src/js_values/mod.rs index 4ab0376c..d64adbca 100644 --- a/napi/src/js_values/mod.rs +++ b/napi/src/js_values/mod.rs @@ -446,6 +446,7 @@ impl_js_value_methods!(JsBoolean); impl_js_value_methods!(JsBuffer); impl_js_value_methods!(JsArrayBuffer); impl_js_value_methods!(JsTypedArray); +impl_js_value_methods!(JsDataView); impl_js_value_methods!(JsNumber); impl_js_value_methods!(JsString); impl_js_value_methods!(JsObject); @@ -470,6 +471,7 @@ impl_napi_value_trait!(JsBoolean, Boolean); impl_napi_value_trait!(JsBuffer, Object); impl_napi_value_trait!(JsArrayBuffer, Object); impl_napi_value_trait!(JsTypedArray, Object); +impl_napi_value_trait!(JsDataView, Object); impl_napi_value_trait!(JsNumber, Number); impl_napi_value_trait!(JsString, String); impl_napi_value_trait!(JsObject, Object);