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 22, 2019
1 parent 7811094 commit bb7ff56
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
51 changes: 51 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,53 @@ macro_rules! typed_array_element {
);
}

macro_rules! data_view_element {
($t: ident,
$element: ty,
$unwrap: ident,
$length: 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_and_data(obj: *mut JSObject) -> (u32,) {
($length(obj),)
}

}
);

($t: ident,
$element: ty,
$unwrap: ident,
$length: ident,
$create_new: ident,
$get_data: ident) => (
data_view_element!($t, $element, $unwrap, $length);
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
}
}
);
}
data_view_element!(MaxTypedArrayViewType,
u8,
UnwrapArrayBufferView,
JS_GetDataViewByteLength,
JS_NewDataView,
JS_GetDataViewData);
typed_array_element!(Uint8,
u8,
UnwrapUint8Array,
Expand Down Expand Up @@ -392,6 +442,7 @@ macro_rules! array_alias {
}
}

array_alias!(MaxTypedArrayViewType, HeapMaxTypedArrayViewType, ArrayBufferViewU8);
array_alias!(Uint8ClampedArray, HeapUint8ClampedArray, ClampedU8);
array_alias!(Uint8Array, HeapUint8Array, Uint8);
array_alias!(Int8Array, HeapInt8Array, Int8);
Expand Down
5 changes: 5 additions & 0 deletions tests/typedarray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ fn typedarray() {

let _ac = JSAutoCompartment::new(cx, global.get());

rooted!(in(cx) let mut rval0 = UndefinedValue());
assert!(rt.evaluate_script(global.handle(), "new DataView(buffer,12,4)",
"test", 1, rval0.handle_mut()).is_ok());
assert!(rval0.is_object());

rooted!(in(cx) let mut rval = UndefinedValue());
assert!(rt.evaluate_script(global.handle(), "new Uint8Array([0, 2, 4])",
"test", 1, rval.handle_mut()).is_ok());
Expand Down

0 comments on commit bb7ff56

Please sign in to comment.