From 61106e17f647c0c35faeb559484351df5f94d0ea Mon Sep 17 00:00:00 2001 From: angelochecked Date: Tue, 28 Nov 2023 23:01:34 +0100 Subject: [PATCH] implemented get buffer --- boa_engine/src/builtins/typed_array/builtin.rs | 2 +- boa_engine/src/object/builtins/jstypedarray.rs | 6 ++++++ boa_examples/src/bin/jstypedarray.rs | 6 ++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/boa_engine/src/builtins/typed_array/builtin.rs b/boa_engine/src/builtins/typed_array/builtin.rs index 27b93867459..d557fbf5ed1 100644 --- a/boa_engine/src/builtins/typed_array/builtin.rs +++ b/boa_engine/src/builtins/typed_array/builtin.rs @@ -415,7 +415,7 @@ impl BuiltinTypedArray { /// - [ECMAScript reference][spec] /// /// [spec]: https://tc39.es/ecma262/#sec-get-%typedarray%.prototype.buffer - fn buffer(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { + pub(crate) fn buffer(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { // 1. Let O be the this value. // 2. Perform ? RequireInternalSlot(O, [[TypedArrayName]]). // 3. Assert: O has a [[ViewedArrayBuffer]] internal slot. diff --git a/boa_engine/src/object/builtins/jstypedarray.rs b/boa_engine/src/object/builtins/jstypedarray.rs index 43d466dbcac..31830142e04 100644 --- a/boa_engine/src/object/builtins/jstypedarray.rs +++ b/boa_engine/src/object/builtins/jstypedarray.rs @@ -61,6 +61,12 @@ impl JsTypedArray { BuiltinTypedArray::at(&self.inner.clone().into(), &[index.into().into()], context) } + /// Calls `TypedArray.prototype.buffer()`. + #[inline] + pub fn buffer(&self, context: &mut Context) -> JsResult { + BuiltinTypedArray::buffer(&self.inner.clone().into(), &[], context) + } + /// Returns `TypedArray.prototype.byteLength`. #[inline] pub fn byte_length(&self, context: &mut Context) -> JsResult { diff --git a/boa_examples/src/bin/jstypedarray.rs b/boa_examples/src/bin/jstypedarray.rs index 8937adb5cec..2923b5f0e8b 100644 --- a/boa_examples/src/bin/jstypedarray.rs +++ b/boa_examples/src/bin/jstypedarray.rs @@ -159,6 +159,12 @@ fn main() -> JsResult<()> { assert_eq!(subarray4_6.get(0, context)?, JsValue::new(5.0)); assert_eq!(subarray4_6.get(1, context)?, JsValue::new(6.0)); + // buffer + let array_buffer8 = JsArrayBuffer::new(8, context)?; + let array = JsUint8Array::from_array_buffer(array_buffer8, context)?; + + assert!(array.buffer(context)?.as_object().unwrap().is_buffer()); + context .register_global_property( js_string!("myUint8Array"),