2020-09-30 19:22:48 +09:00
|
|
|
use std::mem;
|
|
|
|
use std::ops::Deref;
|
2020-06-21 20:10:06 +09:00
|
|
|
use std::ptr;
|
|
|
|
|
2020-09-30 19:22:48 +09:00
|
|
|
use super::Value;
|
2020-06-21 20:10:06 +09:00
|
|
|
use crate::error::check_status;
|
2020-09-30 19:22:48 +09:00
|
|
|
use crate::{sys, JsUnknown, Ref, Result};
|
|
|
|
|
|
|
|
#[repr(transparent)]
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
pub struct JsArrayBuffer(pub(crate) Value);
|
2020-06-21 20:10:06 +09:00
|
|
|
|
2020-08-10 13:37:58 +09:00
|
|
|
#[derive(Debug)]
|
2020-09-30 19:22:48 +09:00
|
|
|
pub struct JsArrayBufferValue {
|
|
|
|
pub(crate) value: JsArrayBuffer,
|
|
|
|
data: mem::ManuallyDrop<Vec<u8>>,
|
2020-06-21 20:10:06 +09:00
|
|
|
}
|
|
|
|
|
2020-07-18 03:00:48 +09:00
|
|
|
impl JsArrayBuffer {
|
2020-09-30 19:22:48 +09:00
|
|
|
pub fn into_value(self) -> Result<JsArrayBufferValue> {
|
|
|
|
let mut data = ptr::null_mut();
|
|
|
|
let mut len: u64 = 0;
|
2020-09-03 01:35:47 +09:00
|
|
|
check_status(unsafe {
|
2020-09-30 19:22:48 +09:00
|
|
|
sys::napi_get_arraybuffer_info(self.0.env, self.0.value, &mut data, &mut len)
|
2020-09-03 01:35:47 +09:00
|
|
|
})?;
|
2020-09-30 19:22:48 +09:00
|
|
|
Ok(JsArrayBufferValue {
|
|
|
|
data: mem::ManuallyDrop::new(unsafe {
|
|
|
|
Vec::from_raw_parts(data as *mut _, len as usize, len as usize)
|
|
|
|
}),
|
|
|
|
value: self,
|
|
|
|
})
|
2020-09-03 01:35:47 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2020-09-30 19:22:48 +09:00
|
|
|
pub fn into_ref(self) -> Result<Ref<JsArrayBufferValue>> {
|
|
|
|
Ref::new(self.0, 1, self.into_value()?)
|
2020-09-03 01:35:47 +09:00
|
|
|
}
|
2020-09-30 19:22:48 +09:00
|
|
|
}
|
2020-09-03 01:35:47 +09:00
|
|
|
|
2020-09-30 19:22:48 +09:00
|
|
|
impl JsArrayBufferValue {
|
|
|
|
pub fn new(value: JsArrayBuffer, data: Vec<u8>) -> Self {
|
|
|
|
JsArrayBufferValue {
|
|
|
|
value,
|
|
|
|
data: mem::ManuallyDrop::new(data),
|
|
|
|
}
|
2020-09-03 01:35:47 +09:00
|
|
|
}
|
|
|
|
|
2020-09-30 19:22:48 +09:00
|
|
|
pub fn into_raw(self) -> JsArrayBuffer {
|
|
|
|
self.value
|
2020-09-03 01:35:47 +09:00
|
|
|
}
|
|
|
|
|
2020-09-30 19:22:48 +09:00
|
|
|
pub fn into_unknown(self) -> Result<JsUnknown> {
|
|
|
|
self.value.into_unknown()
|
2020-09-03 01:35:47 +09:00
|
|
|
}
|
2020-07-18 03:00:48 +09:00
|
|
|
}
|
|
|
|
|
2020-09-30 19:22:48 +09:00
|
|
|
impl AsRef<[u8]> for JsArrayBufferValue {
|
|
|
|
fn as_ref(&self) -> &[u8] {
|
|
|
|
self.data.as_slice()
|
2020-09-07 18:26:28 +09:00
|
|
|
}
|
2020-06-21 20:10:06 +09:00
|
|
|
}
|
2020-08-26 01:07:27 +09:00
|
|
|
|
2020-09-30 19:22:48 +09:00
|
|
|
impl Deref for JsArrayBufferValue {
|
|
|
|
type Target = [u8];
|
|
|
|
|
|
|
|
fn deref(&self) -> &[u8] {
|
|
|
|
self.data.as_slice()
|
2020-08-26 01:07:27 +09:00
|
|
|
}
|
|
|
|
}
|