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

Commit bb7ff56

Browse files
committed
Added DataViewElement functionality
1 parent 7811094 commit bb7ff56

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

src/typedarray.rs

+51
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ use jsapi::UnwrapUint16Array;
5858
use jsapi::UnwrapUint32Array;
5959
use jsapi::UnwrapUint8Array;
6060
use jsapi::UnwrapUint8ClampedArray;
61+
use jsapi::JS_GetDataViewByteLength;
62+
use jsapi::JS_GetDataViewData;
63+
use jsapi::JS_NewDataView;
6164
use rust::{HandleValue, MutableHandleObject, MutableHandleValue};
6265
use rust::CustomTrace;
6366

@@ -318,6 +321,53 @@ macro_rules! typed_array_element {
318321
);
319322
}
320323

324+
macro_rules! data_view_element {
325+
($t: ident,
326+
$element: ty,
327+
$unwrap: ident,
328+
$length: ident) => (
329+
pub struct $t;
330+
331+
impl TypedArrayElement for $t {
332+
type Element = $element;
333+
unsafe fn unwrap_array(obj: *mut JSObject) -> *mut JSObject {
334+
$unwrap(obj)
335+
}
336+
337+
unsafe fn length_and_data(obj: *mut JSObject) -> (u32,) {
338+
($length(obj),)
339+
}
340+
341+
}
342+
);
343+
344+
($t: ident,
345+
$element: ty,
346+
$unwrap: ident,
347+
$length: ident,
348+
$create_new: ident,
349+
$get_data: ident) => (
350+
data_view_element!($t, $element, $unwrap, $length);
351+
impl TypedArrayElementCreator for $t {
352+
unsafe fn create_new(cx: *mut JSContext,array_buffer: *mut JSObject,byte_offset: u32,byte_length: u32) -> *mut JSObject {
353+
$create_new(cx, array_buffer,byte_offset,byte_length)
354+
}
355+
356+
unsafe fn get_data(obj: *mut JSObject) -> *mut Self::Element {
357+
let mut shared = false;
358+
let data = $get_data(obj, &mut shared, ptr::null_mut());
359+
assert!(!shared);
360+
data
361+
}
362+
}
363+
);
364+
}
365+
data_view_element!(MaxTypedArrayViewType,
366+
u8,
367+
UnwrapArrayBufferView,
368+
JS_GetDataViewByteLength,
369+
JS_NewDataView,
370+
JS_GetDataViewData);
321371
typed_array_element!(Uint8,
322372
u8,
323373
UnwrapUint8Array,
@@ -392,6 +442,7 @@ macro_rules! array_alias {
392442
}
393443
}
394444

445+
array_alias!(MaxTypedArrayViewType, HeapMaxTypedArrayViewType, ArrayBufferViewU8);
395446
array_alias!(Uint8ClampedArray, HeapUint8ClampedArray, ClampedU8);
396447
array_alias!(Uint8Array, HeapUint8Array, Uint8);
397448
array_alias!(Int8Array, HeapInt8Array, Int8);

tests/typedarray.rs

+5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ fn typedarray() {
3333

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

36+
rooted!(in(cx) let mut rval0 = UndefinedValue());
37+
assert!(rt.evaluate_script(global.handle(), "new DataView(buffer,12,4)",
38+
"test", 1, rval0.handle_mut()).is_ok());
39+
assert!(rval0.is_object());
40+
3641
rooted!(in(cx) let mut rval = UndefinedValue());
3742
assert!(rt.evaluate_script(global.handle(), "new Uint8Array([0, 2, 4])",
3843
"test", 1, rval.handle_mut()).is_ok());

0 commit comments

Comments
 (0)