Skip to content

Commit

Permalink
implemented subarray
Browse files Browse the repository at this point in the history
  • Loading branch information
AngeloChecked committed Nov 28, 2023
1 parent 3a919a6 commit 2c581bb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
17 changes: 17 additions & 0 deletions boa_engine/src/object/builtins/jstypedarray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,23 @@ impl JsTypedArray {
Ok(self.clone())
}

/// Calls `TypedArray.prototype.subarray()`.
#[inline]
pub fn subarray(&self, begin: i64, end: i64, context: &mut Context) -> JsResult<Self> {
let subarray = BuiltinTypedArray::subarray(
&self.inner.clone().into(),
&[begin.into(), end.into()],
context,
)?;

Ok(Self {
inner: subarray
.as_object()
.cloned()
.expect("`subarray` must always return a `TypedArray` on success"),
})
}

/// Calls `TypedArray.prototype.filter()`.
#[inline]
pub fn filter(
Expand Down
14 changes: 14 additions & 0 deletions boa_examples/src/bin/jstypedarray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,20 @@ fn main() -> JsResult<()> {
assert_eq!(initialized8_array.get(7, context)?, JsValue::new(0));
assert_eq!(initialized8_array.get(8, context)?, JsValue::Undefined);

// subarray
let array = JsUint8Array::from_iter(vec![1u8, 2u8, 3u8, 4u8, 5u8, 6u8, 7u8, 8u8], context)?;
let subarray2_6 = array.subarray(2, 6, context)?;
assert_eq!(subarray2_6.length(context)?, 4);
assert_eq!(subarray2_6.get(0, context)?, JsValue::new(3.0));
assert_eq!(subarray2_6.get(1, context)?, JsValue::new(4.0));
assert_eq!(subarray2_6.get(2, context)?, JsValue::new(5.0));
assert_eq!(subarray2_6.get(3, context)?, JsValue::new(6.0));

let subarray4_6 = array.subarray(-4, 6, context)?;
assert_eq!(subarray4_6.length(context)?, 2);
assert_eq!(subarray4_6.get(0, context)?, JsValue::new(5.0));
assert_eq!(subarray4_6.get(1, context)?, JsValue::new(6.0));

context
.register_global_property(
js_string!("myUint8Array"),
Expand Down

0 comments on commit 2c581bb

Please sign in to comment.