Skip to content
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
2 changes: 2 additions & 0 deletions mozjs/src/jsglue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,8 @@ JS_DEFINE_DATA_AND_LENGTH_ACCESSOR(Int16, int16_t)
JS_DEFINE_DATA_AND_LENGTH_ACCESSOR(Uint16, uint16_t)
JS_DEFINE_DATA_AND_LENGTH_ACCESSOR(Int32, int32_t)
JS_DEFINE_DATA_AND_LENGTH_ACCESSOR(Uint32, uint32_t)
JS_DEFINE_DATA_AND_LENGTH_ACCESSOR(BigInt64, int64_t)
JS_DEFINE_DATA_AND_LENGTH_ACCESSOR(BigUint64, uint64_t)
JS_DEFINE_DATA_AND_LENGTH_ACCESSOR(Float32, float)
JS_DEFINE_DATA_AND_LENGTH_ACCESSOR(Float64, double)

Expand Down
26 changes: 26 additions & 0 deletions mozjs/src/typedarray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use crate::conversions::ConversionResult;
use crate::conversions::FromJSValConvertible;
use crate::conversions::ToJSValConvertible;
use crate::glue::GetBigInt64ArrayLengthAndData;
use crate::glue::GetBigUint64ArrayLengthAndData;
use crate::glue::GetFloat32ArrayLengthAndData;
use crate::glue::GetFloat64ArrayLengthAndData;
use crate::glue::GetInt16ArrayLengthAndData;
Expand All @@ -26,6 +28,8 @@ use crate::jsapi::JSContext;
use crate::jsapi::JSObject;
use crate::jsapi::JSTracer;
use crate::jsapi::JS_GetArrayBufferViewType;
use crate::jsapi::JS_GetBigInt64ArrayData;
use crate::jsapi::JS_GetBigUint64ArrayData;
use crate::jsapi::JS_GetFloat32ArrayData;
use crate::jsapi::JS_GetFloat64ArrayData;
use crate::jsapi::JS_GetInt16ArrayData;
Expand All @@ -36,6 +40,8 @@ use crate::jsapi::JS_GetUint16ArrayData;
use crate::jsapi::JS_GetUint32ArrayData;
use crate::jsapi::JS_GetUint8ArrayData;
use crate::jsapi::JS_GetUint8ClampedArrayData;
use crate::jsapi::JS_NewBigInt64Array;
use crate::jsapi::JS_NewBigUint64Array;
use crate::jsapi::JS_NewFloat32Array;
use crate::jsapi::JS_NewFloat64Array;
use crate::jsapi::JS_NewInt16Array;
Expand All @@ -49,6 +55,8 @@ use crate::jsapi::NewArrayBuffer;
use crate::jsapi::Type;
use crate::jsapi::UnwrapArrayBuffer;
use crate::jsapi::UnwrapArrayBufferView;
use crate::jsapi::UnwrapBigInt64Array;
use crate::jsapi::UnwrapBigUint64Array;
use crate::jsapi::UnwrapFloat32Array;
use crate::jsapi::UnwrapFloat64Array;
use crate::jsapi::UnwrapInt16Array;
Expand Down Expand Up @@ -354,6 +362,14 @@ typed_array_element!(
JS_NewUint32Array,
JS_GetUint32ArrayData
);
typed_array_element!(
BigUint64,
u64,
UnwrapBigUint64Array,
GetBigUint64ArrayLengthAndData,
JS_NewBigUint64Array,
JS_GetBigUint64ArrayData
);
typed_array_element!(
Int8,
i8,
Expand All @@ -378,6 +394,14 @@ typed_array_element!(
JS_NewInt32Array,
JS_GetInt32ArrayData
);
typed_array_element!(
BigInt64,
i64,
UnwrapBigInt64Array,
GetBigInt64ArrayLengthAndData,
JS_NewBigInt64Array,
JS_GetBigInt64ArrayData
);
typed_array_element!(
Float32,
f32,
Expand Down Expand Up @@ -433,6 +457,8 @@ array_alias!(Uint16Array, HeapUint16Array, Uint16);
array_alias!(Int16Array, HeapInt16Array, Int16);
array_alias!(Uint32Array, HeapUint32Array, Uint32);
array_alias!(Int32Array, HeapInt32Array, Int32);
array_alias!(BigUint64Array, HeapBigUint64Array, BigUint64);
array_alias!(BigInt64Array, HeapBigInt64Array, BigInt64);
array_alias!(Float32Array, HeapFloat32Array, Float32);
array_alias!(Float64Array, HeapFloat64Array, Float64);
array_alias!(ArrayBuffer, HeapArrayBuffer, ArrayBufferU8);
Expand Down
38 changes: 34 additions & 4 deletions mozjs/tests/typedarray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use mozjs::jsval::UndefinedValue;
use mozjs::rooted;
use mozjs::rust::{JSEngine, RealmOptions, Runtime, SIMPLE_GLOBAL_CLASS};
use mozjs::typedarray;
use mozjs::typedarray::{CreateWith, Uint32Array};
use mozjs::typedarray::{BigInt64Array, CreateWith, Float32Array, Uint32Array};

#[test]
fn typedarray() {
Expand Down Expand Up @@ -42,7 +42,7 @@ fn typedarray() {
assert!(rval.is_object());

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

typedarray!(in(context) let array: Uint8Array = rval.to_object());
assert_eq!(array.unwrap().len(), 3);
Expand All @@ -62,11 +62,11 @@ fn typedarray() {
);

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

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

rooted!(in(context) let rval = ptr::null_mut::<JSObject>());
typedarray!(in(context) let array: Uint8Array = rval.get());
Expand All @@ -87,5 +87,35 @@ fn typedarray() {

typedarray!(in(context) let view: ArrayBufferView = rval.get());
assert_eq!(view.unwrap().is_shared(), false);

rooted!(in(context) let mut rval = ptr::null_mut::<JSObject>());
assert!(Float32Array::create(
context,
CreateWith::Slice(&[0.25, 0.5, 1.0, 2.0, 4.0]),
rval.handle_mut()
)
.is_ok());

typedarray!(in(context) let array: Float32Array = rval.get());
assert_eq!(array.unwrap().as_slice(), &[0.25, 0.5, 1.0, 2.0, 4.0]);

typedarray!(in(context) let mut array: Float32Array = rval.get());
array.as_mut().unwrap().update(&[0.5, 1.0, 2.0]);
assert_eq!(array.unwrap().as_slice(), &[0.5, 1.0, 2.0, 2.0, 4.0]);

rooted!(in(context) let mut rval = ptr::null_mut::<JSObject>());
assert!(BigInt64Array::create(
context,
CreateWith::Slice(&[-6, -1, 0, 2, 5]),
rval.handle_mut()
)
.is_ok());

typedarray!(in(context) let array: BigInt64Array = rval.get());
assert_eq!(array.unwrap().as_slice(), &[-6, -1, 0, 2, 5]);

typedarray!(in(context) let mut array: BigInt64Array = rval.get());
array.as_mut().unwrap().update(&[-12, -2, -1]);
assert_eq!(array.unwrap().as_slice(), &[-12, -2, -1, 2, 5]);
}
}