Skip to content
This repository has been archived by the owner on Nov 12, 2022. It is now read-only.

Commit

Permalink
Added DataViewElement functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
aditj committed Feb 21, 2019
1 parent 7811094 commit 2d74eec
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/typedarray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ use jsapi::UnwrapUint16Array;
use jsapi::UnwrapUint32Array;
use jsapi::UnwrapUint8Array;
use jsapi::UnwrapUint8ClampedArray;
use jsapi::JS_GetDataViewByteLength;
use jsapi::JS_GetDataViewData;
use jsapi::JS_NewDataView;
use rust::{HandleValue, MutableHandleObject, MutableHandleValue};
use rust::CustomTrace;

Expand Down Expand Up @@ -318,6 +321,52 @@ macro_rules! typed_array_element {
);
}

macro_rules! data_view_element {
($t: ident,
$element: ty,
$unwrap: ident,
$length: ident,
$data: ident) => (
pub struct $t;

impl TypedArrayElement for $t {
type Element = $element;
unsafe fn unwrap_array(obj: *mut JSObject) -> *mut JSObject {
$unwrap(obj)
}

unsafe fn length(obj: *mut JSObject) -> u32 {
$length(obj)
}
unsafe fn data(obj: *mut JSObject) -> *mut Self::Element {
$data(obj)
}
}
);

($t: ident,
$element: ty,
$unwrap: ident,
$length: ident,
$data: ident,
$create_new: ident,
$get_data: ident) => (
typed_array_element!($t, $element, $unwrap, $length,$data);
impl TypedArrayElementCreator for $t {
unsafe fn create_new(cx: *mut JSContext,array_buffer: *mut JSObject,byte_offset: u32,byte_length: u32) -> *mut JSObject {
$create_new(cx, array_buffer,byte_offset,byte_length)
}

unsafe fn get_data(obj: *mut JSObject) -> *mut Self::Element {
let mut shared = false;
let data = $get_data(obj, &mut shared, ptr::null_mut());
assert!(!shared);
data
}
}
);
}

typed_array_element!(Uint8,
u8,
UnwrapUint8Array,
Expand Down

0 comments on commit 2d74eec

Please sign in to comment.