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

Usable DataView typed array bindings #453

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jdm
This code creates a create_new function for data view object by extending the TypedArrayElement , and due to the mismatch in definition and prototype we are having errors like (Link to travis CI errors):

  1. for the length and data method
  2. for the create new method
    etc.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, you're right. I don't know if we can reuse the typed array support code for data views after all :(

$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