Skip to content

Commit

Permalink
implemented toStringTag
Browse files Browse the repository at this point in the history
  • Loading branch information
Angelo Ceccato authored and Angelo Ceccato committed Dec 9, 2023
1 parent 942e140 commit c291beb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
6 changes: 5 additions & 1 deletion boa_engine/src/builtins/typed_array/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsValue> {
pub(crate) fn to_string_tag(
this: &JsValue,
_: &[JsValue],
_: &mut Context,
) -> JsResult<JsValue> {
// 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.
Expand Down
6 changes: 6 additions & 0 deletions boa_engine/src/object/builtins/jstypedarray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,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<JsValue> {
BuiltinTypedArray::to_string_tag(&self.inner.clone().into(), &[], context)
}
}

impl From<JsTypedArray> for JsObject {
Expand Down
5 changes: 5 additions & 0 deletions boa_examples/src/bin/jstypedarray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down

0 comments on commit c291beb

Please sign in to comment.