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

Use Heap for typed arrays. #341

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
54 changes: 29 additions & 25 deletions src/typedarray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use glue::GetUint8ClampedArrayLengthAndData;
use jsapi::GetArrayBufferLengthAndData;
use jsapi::GetArrayBufferViewLengthAndData;
use jsapi::HandleObject;
use jsapi::Heap;
use jsapi::JSContext;
use jsapi::JSObject;
use jsapi::JS_GetArrayBufferData;
use jsapi::JS_GetArrayBufferViewType;
use jsapi::JS_GetFloat32ArrayData;
Expand All @@ -41,8 +41,8 @@ use jsapi::JS_NewUint16Array;
use jsapi::JS_NewUint32Array;
use jsapi::JS_NewUint8Array;
use jsapi::JS_NewUint8ClampedArray;
use jsapi::JSObject;
use jsapi::MutableHandleObject;
use jsapi::Rooted;
use jsapi::Type;
use jsapi::UnwrapArrayBuffer;
use jsapi::UnwrapArrayBufferView;
Expand All @@ -55,7 +55,6 @@ use jsapi::UnwrapUint16Array;
use jsapi::UnwrapUint32Array;
use jsapi::UnwrapUint8Array;
use jsapi::UnwrapUint8ClampedArray;
use rust::RootedGuard;
use std::ptr;
use std::slice;

Expand All @@ -65,32 +64,33 @@ pub enum CreateWith<'a, T: 'a> {
}

/// A rooted typed array.
pub struct TypedArray<'a, T: 'a + TypedArrayElement> {
object: RootedGuard<'a, *mut JSObject>,
pub struct TypedArray<T: TypedArrayElement> {
object: Box<Heap<*mut JSObject>>,
computed: Option<(*mut T::Element, u32)>,
}

impl<'a, T: TypedArrayElement> TypedArray<'a, T> {
impl<T: TypedArrayElement> TypedArray<T> {
/// Create a typed array representation that wraps an existing JS reflector.
/// This operation will fail if attempted on a JS object that does not match
/// the expected typed array details.
pub fn from(cx: *mut JSContext,
root: &'a mut Rooted<*mut JSObject>,
object: *mut JSObject)
-> Result<Self, ()> {
rooted!(in (cx) let object = object);
if object.is_null() {
return Err(());
}
unsafe {
let mut guard = RootedGuard::new(cx, root, object);
let unwrapped = T::unwrap_array(*guard);
let unwrapped = T::unwrap_array(object.get());
if unwrapped.is_null() {
return Err(());
}

*guard = unwrapped;
let array = Box::new(Heap::new(ptr::null_mut()));
array.set(unwrapped);

Ok(TypedArray {
object: guard,
object: array,
computed: None,
})
}
Expand All @@ -101,7 +101,7 @@ impl<'a, T: TypedArrayElement> TypedArray<'a, T> {
return data;
}

let data = unsafe { T::length_and_data(*self.object) };
let data = unsafe { T::length_and_data(self.object.get()) };
self.computed = Some(data);
data
}
Expand All @@ -126,9 +126,13 @@ impl<'a, T: TypedArrayElement> TypedArray<'a, T> {
let (pointer, length) = self.data();
slice::from_raw_parts_mut(pointer, length as usize)
}

pub fn object(&self) -> &Heap<*mut JSObject> {
&self.object
}
}

impl<'a, T: TypedArrayElementCreator + TypedArrayElement> TypedArray<'a, T> {
impl<T: TypedArrayElementCreator + TypedArrayElement> TypedArray<T> {
/// Create a new JS typed array, optionally providing initial data that will
/// be copied into the newly-allocated buffer. Returns the new JS reflector.
pub unsafe fn create(cx: *mut JSContext,
Expand Down Expand Up @@ -297,29 +301,29 @@ typed_array_element!(ArrayBufferViewU8,
GetArrayBufferViewLengthAndData);

/// The Uint8ClampedArray type.
pub type Uint8ClampedArray<'a> = TypedArray<'a, ClampedU8>;
pub type Uint8ClampedArray = TypedArray<ClampedU8>;
/// The Uint8Array type.
pub type Uint8Array<'a> = TypedArray<'a, Uint8>;
pub type Uint8Array = TypedArray<Uint8>;
/// The Int8Array type.
pub type Int8Array<'a> = TypedArray<'a, Int8>;
pub type Int8Array = TypedArray<Int8>;
/// The Uint16Array type.
pub type Uint16Array<'a> = TypedArray<'a, Uint16>;
pub type Uint16Array = TypedArray<Uint16>;
/// The Int16Array type.
pub type Int16Array<'a> = TypedArray<'a, Int16>;
pub type Int16Array = TypedArray<Int16>;
/// The Uint32Array type.
pub type Uint32Array<'a> = TypedArray<'a, Uint32>;
pub type Uint32Array = TypedArray<Uint32>;
/// The Int32Array type.
pub type Int32Array<'a> = TypedArray<'a, Int32>;
pub type Int32Array = TypedArray<Int32>;
/// The Float32Array type.
pub type Float32Array<'a> = TypedArray<'a, Float32>;
pub type Float32Array = TypedArray<Float32>;
/// The Float64Array type.
pub type Float64Array<'a> = TypedArray<'a, Float64>;
pub type Float64Array = TypedArray<Float64>;
/// The ArrayBuffer type.
pub type ArrayBuffer<'a> = TypedArray<'a, ArrayBufferU8>;
pub type ArrayBuffer = TypedArray<ArrayBufferU8>;
/// The ArrayBufferView type
pub type ArrayBufferView<'a> = TypedArray<'a, ArrayBufferViewU8>;
pub type ArrayBufferView = TypedArray<ArrayBufferViewU8>;

impl<'a> ArrayBufferView<'a> {
impl ArrayBufferView {
pub fn get_array_type(&self) -> Type {
unsafe { JS_GetArrayBufferViewType(self.object.get()) }
}
Expand Down
110 changes: 89 additions & 21 deletions tests/typedarray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,89 @@
#[macro_use]
extern crate js;

use js::glue::CallObjectTracer;
use js::jsapi::CompartmentOptions;
use js::jsapi::GCTraceKindToAscii;
use js::jsapi::Heap;
use js::jsapi::JS_AddExtraGCRootsTracer;
use js::jsapi::JSAutoCompartment;
use js::jsapi::JS_NewGlobalObject;
use js::jsapi::JSObject;
use js::jsapi::JSTracer;
use js::jsapi::OnNewGlobalHookOption;
use js::jsapi::TraceKind;
use js::jsapi::Type;
use js::jsval::UndefinedValue;
use js::rust::Runtime as Runtime_;
use js::rust::SIMPLE_GLOBAL_CLASS;
use js::typedarray::{CreateWith, Uint32Array};
use js::typedarray::{ArrayBufferView, CreateWith, TypedArray, TypedArrayElement, Uint8Array, Uint16Array, Uint32Array};
use std::cell::RefCell;
use std::ops::{Deref, DerefMut};
use std::os::raw::c_void;
use std::ptr;

thread_local!(
static ROOTED_TRACEABLES: RefCell<Vec<*const Heap<*mut JSObject>>> = RefCell::new(Vec::new());
);

unsafe extern fn trace(tracer: *mut JSTracer, _: *mut c_void) {
ROOTED_TRACEABLES.with(|v| {
for &array in &*v.borrow() {
CallObjectTracer(tracer,
array as *mut _,
GCTraceKindToAscii(TraceKind::Object));
}
});
}

struct Tracer<T: TypedArrayElement> {
array: TypedArray<T>,
}

impl<T: TypedArrayElement> Tracer<T> {
fn new(array: TypedArray<T>) -> Self {
let ptr = array.object() as *const _;
ROOTED_TRACEABLES.with(|v| {
v.borrow_mut().push(ptr);
});
Tracer {
array: array,
}
}
}


impl<T: TypedArrayElement> Deref for Tracer<T> {
type Target = TypedArray<T>;
fn deref(&self) -> &Self::Target {
&self.array
}
}

impl<T: TypedArrayElement> DerefMut for Tracer<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.array
}
}

impl<T: TypedArrayElement> Drop for Tracer<T> {
fn drop(&mut self) {
let ptr = ROOTED_TRACEABLES.with(|v| {
v.borrow_mut().pop()
});
assert_eq!(self.array.object() as *const _, ptr.unwrap());
}
}


#[test]
fn typedarray() {
let rt = Runtime_::new();
let cx = rt.cx();

unsafe {
JS_AddExtraGCRootsTracer(rt.rt(), Some(trace), ptr::null_mut());

rooted!(in(cx) let global =
JS_NewGlobalObject(cx, &SIMPLE_GLOBAL_CLASS, ptr::null_mut(),
OnNewGlobalHookOption::FireOnNewGlobalHook,
Expand All @@ -35,41 +101,41 @@ fn typedarray() {
"test", 1, rval.handle_mut()).is_ok());
assert!(rval.is_object());

typedarray!(in(cx) let array: Uint8Array = rval.to_object());
assert_eq!(array.unwrap().as_slice(), &[0, 2, 4][..]);
let mut array = Tracer::new(Uint8Array::from(cx, rval.to_object()).unwrap());
assert_eq!(array.as_slice(), &[0, 2, 4][..]);

typedarray!(in(cx) let array: Uint16Array = rval.to_object());
let array = Uint16Array::from(cx, rval.to_object());
assert!(array.is_err());

typedarray!(in(cx) let view: ArrayBufferView = rval.to_object());
assert_eq!(view.unwrap().get_array_type(), Type::Uint8);
let view = Tracer::new(ArrayBufferView::from(cx, rval.to_object()).unwrap());
assert_eq!(view.get_array_type(), Type::Uint8);

rooted!(in(cx) let mut rval = ptr::null_mut());
assert!(Uint32Array::create(cx, CreateWith::Slice(&[1, 3, 5]), rval.handle_mut()).is_ok());

typedarray!(in(cx) let array: Uint32Array = rval.get());
assert_eq!(array.unwrap().as_slice(), &[1, 3, 5][..]);
let mut array = Tracer::new(Uint32Array::from(cx, rval.get()).unwrap());
assert_eq!(array.as_slice(), &[1, 3, 5][..]);

typedarray!(in(cx) let mut array: Uint32Array = rval.get());
array.as_mut().unwrap().update(&[2, 4, 6]);
assert_eq!(array.unwrap().as_slice(), &[2, 4, 6][..]);
let mut array = Tracer::new(Uint32Array::from(cx, rval.get()).unwrap());
array.update(&[2, 4, 6]);
assert_eq!(array.as_slice(), &[2, 4, 6][..]);

rooted!(in(cx) let rval = ptr::null_mut());
typedarray!(in(cx) let array: Uint8Array = rval.get());
let array = Uint8Array::from(cx, rval.get());
assert!(array.is_err());

rooted!(in(cx) let mut rval = ptr::null_mut());
assert!(Uint32Array::create(cx, CreateWith::Length(5), rval.handle_mut()).is_ok());

typedarray!(in(cx) let array: Uint32Array = rval.get());
assert_eq!(array.unwrap().as_slice(), &[0, 0, 0, 0, 0]);
let mut array = Tracer::new(Uint32Array::from(cx, rval.get()).unwrap());
assert_eq!(array.as_slice(), &[0, 0, 0, 0, 0]);

typedarray!(in(cx) let mut array: Uint32Array = rval.get());
array.as_mut().unwrap().update(&[0, 1, 2, 3]);
assert_eq!(array.unwrap().as_slice(), &[0, 1, 2, 3, 0]);
let mut array = Tracer::new(Uint32Array::from(cx, rval.get()).unwrap());
array.update(&[0, 1, 2, 3]);
assert_eq!(array.as_slice(), &[0, 1, 2, 3, 0]);

typedarray!(in(cx) let view: ArrayBufferView = rval.get());
assert_eq!(view.unwrap().get_array_type(), Type::Uint32);
let view = Tracer::new(ArrayBufferView::from(cx, rval.get()).unwrap());
assert_eq!(view.get_array_type(), Type::Uint32);
}
}

Expand All @@ -80,6 +146,8 @@ fn typedarray_update_panic() {
let cx = rt.cx();

unsafe {
JS_AddExtraGCRootsTracer(rt.rt(), Some(trace), ptr::null_mut());

rooted!(in(cx) let global =
JS_NewGlobalObject(cx, &SIMPLE_GLOBAL_CLASS, ptr::null_mut(),
OnNewGlobalHookOption::FireOnNewGlobalHook,
Expand All @@ -89,7 +157,7 @@ fn typedarray_update_panic() {
let _ac = JSAutoCompartment::new(cx, global.get());
rooted!(in(cx) let mut rval = ptr::null_mut());
let _ = Uint32Array::create(cx, CreateWith::Slice(&[1, 2, 3, 4, 5]), rval.handle_mut());
typedarray!(in(cx) let mut array: Uint32Array = rval.get());
array.as_mut().unwrap().update(&[0, 2, 4, 6, 8, 10]);
let mut array = Tracer::new(Uint32Array::from(cx, rval.get()).unwrap());
array.update(&[0, 2, 4, 6, 8, 10]);
}
}