2020-09-28 01:27:37 +09:00
|
|
|
use std::mem;
|
2020-08-10 13:37:58 +09:00
|
|
|
use std::ops::Deref;
|
2020-06-21 20:10:06 +09:00
|
|
|
use std::ptr;
|
|
|
|
|
2020-09-28 01:27:37 +09:00
|
|
|
use super::Value;
|
|
|
|
#[cfg(feature = "serde-json")]
|
|
|
|
use super::ValueType;
|
2020-06-21 20:10:06 +09:00
|
|
|
use crate::error::check_status;
|
2020-10-03 00:23:21 +09:00
|
|
|
use crate::{sys, JsUnknown, NapiValue, Ref, Result};
|
2020-09-28 01:27:37 +09:00
|
|
|
|
|
|
|
#[repr(transparent)]
|
2020-10-11 23:15:58 +09:00
|
|
|
#[derive(Debug)]
|
2020-09-28 01:27:37 +09:00
|
|
|
pub struct JsBuffer(pub(crate) Value);
|
2020-06-21 20:10:06 +09:00
|
|
|
|
2020-08-10 13:37:58 +09:00
|
|
|
#[derive(Debug)]
|
2020-09-28 01:27:37 +09:00
|
|
|
pub struct JsBufferValue {
|
|
|
|
pub(crate) value: JsBuffer,
|
|
|
|
data: mem::ManuallyDrop<Vec<u8>>,
|
2020-06-21 20:10:06 +09:00
|
|
|
}
|
|
|
|
|
2020-09-28 01:27:37 +09:00
|
|
|
impl JsBuffer {
|
|
|
|
pub fn into_value(self) -> Result<JsBufferValue> {
|
|
|
|
let mut data = ptr::null_mut();
|
2020-11-04 23:52:13 +09:00
|
|
|
let mut len: usize = 0;
|
2020-09-03 01:35:47 +09:00
|
|
|
check_status(unsafe {
|
2020-11-04 23:52:13 +09:00
|
|
|
sys::napi_get_buffer_info(
|
|
|
|
self.0.env,
|
|
|
|
self.0.value,
|
|
|
|
&mut data,
|
|
|
|
&mut len as *mut usize as *mut _,
|
|
|
|
)
|
2020-09-03 01:35:47 +09:00
|
|
|
})?;
|
2020-09-28 01:27:37 +09:00
|
|
|
Ok(JsBufferValue {
|
2020-11-04 23:52:13 +09:00
|
|
|
data: mem::ManuallyDrop::new(unsafe { Vec::from_raw_parts(data as *mut _, len, len) }),
|
2020-09-28 01:27:37 +09:00
|
|
|
value: self,
|
|
|
|
})
|
2020-09-03 01:35:47 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2020-09-28 01:27:37 +09:00
|
|
|
pub fn into_ref(self) -> Result<Ref<JsBufferValue>> {
|
|
|
|
Ref::new(self.0, 1, self.into_value()?)
|
2020-07-03 01:36:45 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-28 01:27:37 +09:00
|
|
|
impl JsBufferValue {
|
|
|
|
#[cfg(feature = "serde-json")]
|
|
|
|
pub(crate) fn from_raw(env: sys::napi_env, value: sys::napi_value) -> Result<Self> {
|
2020-06-21 20:10:06 +09:00
|
|
|
let mut data = ptr::null_mut();
|
2020-11-04 23:52:13 +09:00
|
|
|
let mut len = 0usize;
|
|
|
|
check_status(unsafe {
|
|
|
|
sys::napi_get_buffer_info(env, value, &mut data, &mut len as *mut usize as *mut _)
|
|
|
|
})?;
|
2020-09-28 01:27:37 +09:00
|
|
|
Ok(Self {
|
|
|
|
value: JsBuffer(Value {
|
2020-06-21 20:10:06 +09:00
|
|
|
env,
|
|
|
|
value,
|
|
|
|
value_type: ValueType::Object,
|
|
|
|
}),
|
2020-11-04 23:52:13 +09:00
|
|
|
data: mem::ManuallyDrop::new(unsafe { Vec::from_raw_parts(data as *mut _, len, len) }),
|
2020-06-21 20:10:06 +09:00
|
|
|
})
|
|
|
|
}
|
2020-09-07 18:26:28 +09:00
|
|
|
|
2020-09-28 01:27:37 +09:00
|
|
|
pub fn new(value: JsBuffer, data: Vec<u8>) -> Self {
|
|
|
|
JsBufferValue {
|
|
|
|
value,
|
|
|
|
data: mem::ManuallyDrop::new(data),
|
2020-09-07 18:26:28 +09:00
|
|
|
}
|
|
|
|
}
|
2020-09-28 01:27:37 +09:00
|
|
|
|
|
|
|
pub fn into_raw(self) -> JsBuffer {
|
|
|
|
self.value
|
|
|
|
}
|
|
|
|
|
2020-10-03 00:23:21 +09:00
|
|
|
pub fn into_unknown(self) -> JsUnknown {
|
2020-11-20 01:07:13 +09:00
|
|
|
unsafe { JsUnknown::from_raw_unchecked(self.value.0.env, self.value.0.value) }
|
2020-09-28 01:27:37 +09:00
|
|
|
}
|
2020-06-21 20:10:06 +09:00
|
|
|
}
|
|
|
|
|
2020-09-28 01:27:37 +09:00
|
|
|
impl AsRef<[u8]> for JsBufferValue {
|
2020-06-21 20:10:06 +09:00
|
|
|
fn as_ref(&self) -> &[u8] {
|
2020-09-28 01:27:37 +09:00
|
|
|
self.data.as_slice()
|
2020-06-21 20:10:06 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-28 01:27:37 +09:00
|
|
|
impl Deref for JsBufferValue {
|
2020-06-21 20:10:06 +09:00
|
|
|
type Target = [u8];
|
|
|
|
|
|
|
|
fn deref(&self) -> &[u8] {
|
2020-09-28 01:27:37 +09:00
|
|
|
self.data.as_slice()
|
2020-08-26 01:07:27 +09:00
|
|
|
}
|
|
|
|
}
|