napi-rs/napi/src/js_values/arraybuffer.rs

57 lines
1.3 KiB
Rust
Raw Normal View History

2020-08-26 01:07:27 +09:00
use std::convert::TryFrom;
use std::ptr;
2020-08-26 01:07:27 +09:00
use super::{JsObject, JsUnknown, NapiValue, Value, ValueType};
use crate::error::check_status;
2020-08-26 01:07:27 +09:00
use crate::{sys, Error, Result};
#[derive(Debug)]
pub struct JsArrayBuffer {
pub value: JsObject,
pub data: *const u8,
pub len: u64,
}
2020-07-18 03:00:48 +09:00
impl JsArrayBuffer {
pub(crate) fn from_raw_unchecked(env: sys::napi_env, value: sys::napi_value) -> Self {
Self {
value: JsObject(Value {
env,
value,
value_type: ValueType::Object,
}),
data: ptr::null(),
len: 0,
}
}
}
impl NapiValue for JsArrayBuffer {
fn raw_value(&self) -> sys::napi_value {
self.value.0.value
}
fn from_raw(env: sys::napi_env, value: sys::napi_value) -> Result<Self> {
let mut data = ptr::null_mut();
let mut len: u64 = 0;
let status = unsafe { sys::napi_get_arraybuffer_info(env, value, &mut data, &mut len) };
check_status(status)?;
Ok(JsArrayBuffer {
value: JsObject(Value {
env,
value,
value_type: ValueType::Object,
}),
data: data as *const u8,
len,
})
}
}
2020-08-26 01:07:27 +09:00
impl TryFrom<JsUnknown> for JsArrayBuffer {
type Error = Error;
fn try_from(value: JsUnknown) -> Result<JsArrayBuffer> {
JsArrayBuffer::from_raw(value.0.env, value.0.value)
}
}