From 265202aa2b33235bde366b437cf7acfc3fa63acd Mon Sep 17 00:00:00 2001 From: Angelo Ceccato Date: Sat, 9 Dec 2023 23:10:26 +0100 Subject: [PATCH] implemented toStringTag --- boa_engine/src/builtins/typed_array/builtin.rs | 6 +++++- boa_engine/src/object/builtins/jstypedarray.rs | 6 ++++++ boa_examples/src/bin/jstypedarray.rs | 5 +++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/boa_engine/src/builtins/typed_array/builtin.rs b/boa_engine/src/builtins/typed_array/builtin.rs index 12433fd20a3..db3c78b7d63 100644 --- a/boa_engine/src/builtins/typed_array/builtin.rs +++ b/boa_engine/src/builtins/typed_array/builtin.rs @@ -2960,7 +2960,11 @@ impl BuiltinTypedArray { /// /// [spec]: https://tc39.es/ecma262/#sec-get-%typedarray%.prototype-@@tostringtag #[allow(clippy::unnecessary_wraps)] - fn to_string_tag(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { + pub(crate) fn to_string_tag( + this: &JsValue, + _: &[JsValue], + _: &mut Context, + ) -> JsResult { // 1. Let O be the this value. // 2. If Type(O) is not Object, return undefined. // 3. If O does not have a [[TypedArrayName]] internal slot, return undefined. diff --git a/boa_engine/src/object/builtins/jstypedarray.rs b/boa_engine/src/object/builtins/jstypedarray.rs index 1a9b96e83ea..1e923cc50e2 100644 --- a/boa_engine/src/object/builtins/jstypedarray.rs +++ b/boa_engine/src/object/builtins/jstypedarray.rs @@ -563,6 +563,12 @@ impl JsTypedArray { .expect("`with` must always return a `TypedArray` on success"), }) } + + /// Calls `TypedArray.prototype.toStringTag()`. + #[inline] + pub fn to_string_tag(&self, context: &mut Context) -> JsResult { + BuiltinTypedArray::to_string_tag(&self.inner.clone().into(), &[], context) + } } impl From for JsObject { diff --git a/boa_examples/src/bin/jstypedarray.rs b/boa_examples/src/bin/jstypedarray.rs index 544bc0780b4..ad028e4c7cb 100644 --- a/boa_examples/src/bin/jstypedarray.rs +++ b/boa_examples/src/bin/jstypedarray.rs @@ -196,6 +196,11 @@ fn main() -> JsResult<()> { // js_string!("500,00 €,8.123,00 €,12,00 €").into() // ); + // toStringTag + let array = JsUint8Array::from_iter(vec![1u8, 2u8, 3u8, 4u8, 5u8, 6u8, 7u8, 8u8], context)?; + let tag = array.to_string_tag(context)?.to_string(context)?; + assert_eq!(tag, js_string!("Uint8Array")); + context .register_global_property( js_string!("myUint8Array"),