From 87c203cde02aebb723a5ca56ee5682b303e9de0d Mon Sep 17 00:00:00 2001 From: Redfire Date: Fri, 22 Sep 2023 01:01:23 +0800 Subject: [PATCH 1/4] Added BigInt64 and BigUint64 Typed Array Support --- mozjs/src/jsglue.cpp | 2 ++ mozjs/src/typedarray.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/mozjs/src/jsglue.cpp b/mozjs/src/jsglue.cpp index 2d23b0a34a1..14f84506d42 100644 --- a/mozjs/src/jsglue.cpp +++ b/mozjs/src/jsglue.cpp @@ -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) diff --git a/mozjs/src/typedarray.rs b/mozjs/src/typedarray.rs index c87d9a7774a..72620f9718e 100644 --- a/mozjs/src/typedarray.rs +++ b/mozjs/src/typedarray.rs @@ -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; @@ -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; @@ -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; @@ -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; @@ -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, @@ -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, @@ -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); From 3743037b91e43143ccefc57294d3a49e5506d072 Mon Sep 17 00:00:00 2001 From: Redfire Date: Sat, 2 Dec 2023 14:04:56 +0800 Subject: [PATCH 2/4] Added Test for Float32Array --- mozjs/tests/typedarray.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/mozjs/tests/typedarray.rs b/mozjs/tests/typedarray.rs index f334c547723..f9d99f2b7e7 100644 --- a/mozjs/tests/typedarray.rs +++ b/mozjs/tests/typedarray.rs @@ -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::{CreateWith, Float32Array, Uint32Array}; #[test] fn typedarray() { @@ -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); @@ -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::()); typedarray!(in(context) let array: Uint8Array = rval.get()); @@ -87,5 +87,15 @@ 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::()); + 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]); } } From 8416cf247764680cbe2bda1cca69a1310c7afa4f Mon Sep 17 00:00:00 2001 From: Redfire Date: Sat, 2 Dec 2023 14:09:26 +0800 Subject: [PATCH 3/4] Added Test for BigInt64Array --- mozjs/tests/typedarray.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/mozjs/tests/typedarray.rs b/mozjs/tests/typedarray.rs index f9d99f2b7e7..d4fb6c4dbc3 100644 --- a/mozjs/tests/typedarray.rs +++ b/mozjs/tests/typedarray.rs @@ -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, Float32Array, Uint32Array}; +use mozjs::typedarray::{BigInt64Array, CreateWith, Float32Array, Uint32Array}; #[test] fn typedarray() { @@ -97,5 +97,15 @@ fn typedarray() { 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::()); + 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]); } } From db29bc047c035d4b25f3241216bd3c5fe3b3337d Mon Sep 17 00:00:00 2001 From: Redfire Date: Sat, 2 Dec 2023 15:07:54 +0800 Subject: [PATCH 4/4] Reformatted Code --- mozjs/tests/typedarray.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/mozjs/tests/typedarray.rs b/mozjs/tests/typedarray.rs index d4fb6c4dbc3..5cf94642728 100644 --- a/mozjs/tests/typedarray.rs +++ b/mozjs/tests/typedarray.rs @@ -89,7 +89,12 @@ fn typedarray() { assert_eq!(view.unwrap().is_shared(), false); rooted!(in(context) let mut rval = ptr::null_mut::()); - assert!(Float32Array::create(context, CreateWith::Slice(&[0.25, 0.5, 1.0, 2.0, 4.0]), rval.handle_mut()).is_ok()); + 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]); @@ -99,7 +104,12 @@ fn typedarray() { 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::()); - assert!(BigInt64Array::create(context, CreateWith::Slice(&[-6, -1, 0, 2, 5]), rval.handle_mut()).is_ok()); + 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]);