refactor(napi): change data to &'static [u8] in JsBuffer

This commit is contained in:
LongYinan 2020-08-10 12:37:58 +08:00
parent 088d5fb1f3
commit 64df7205e9
No known key found for this signature in database
GPG key ID: A3FFE134A3E20881
14 changed files with 45 additions and 45 deletions

View file

@ -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<u8>) -> Result<JsBuffer> {
pub fn create_buffer_with_data(&self, mut data: Vec<u8>) -> Result<JsBuffer> {
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]

View file

@ -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,

View file

@ -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 {

View file

@ -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
}
}

View file

@ -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,

View file

@ -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).

View file

@ -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]

View file

@ -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<JsNumber> for usize {

View file

@ -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 {

View file

@ -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 {

View file

@ -1,4 +1,4 @@
use super::Value;
#[derive(Clone, Copy, Debug)]
#[derive(Debug)]
pub struct JsUndefined(pub(crate) Value);

View file

@ -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,

View file

@ -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(

View file

@ -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<JsNull> {
@ -13,10 +13,10 @@ pub fn call_function(ctx: CallContext) -> Result<JsNull> {
#[js_function(1)]
pub fn call_function_with_this(ctx: CallContext<JsObject>) -> Result<JsNull> {
let js_this = ctx.this;
let js_this = &ctx.this;
let js_func = ctx.get::<JsFunction>(0)?;
js_func.call(Some(&js_this), &[])?;
js_func.call(Some(js_this), &[])?;
ctx.env.get_null()
}