[oden-js] ArrayBuffer values

This commit is contained in:
John Doty 2023-06-30 06:26:53 -07:00
parent b77e7eba3e
commit f7ed78ce3b
2 changed files with 42 additions and 0 deletions

View file

@ -324,6 +324,34 @@ impl ContextRef {
}
}
unsafe extern "C" fn free_array_buffer_bytes(
_rt: *mut sys::JSRuntime,
opaque: *mut std::ffi::c_void,
_ptr: *mut std::ffi::c_void,
) {
drop(Box::from_raw(opaque as *mut Vec<u8>));
}
pub fn new_array_buffer<T>(&self, buffer: T) -> ValueResult
where
T: Into<Vec<u8>>,
{
let mut vec_box = Box::new(buffer.into());
unsafe {
let is_shared = 0;
let byte_ptr = vec_box.as_mut_ptr();
let byte_len = vec_box.len();
self.check_exception(sys::JS_NewArrayBuffer(
self.ctx,
byte_ptr,
byte_len,
Some(Self::free_array_buffer_bytes),
Box::into_raw(vec_box) as *mut _,
is_shared,
))
}
}
/// Fetch the global object for the context.
pub fn global_object(&self) -> ValueResult {
self.check_exception(unsafe { sys::JS_GetGlobalObject(self.ctx) })