diff options
author | Idan Horowitz <idan.horowitz@gmail.com> | 2021-06-17 18:24:48 +0300 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-06-18 20:17:45 +0100 |
commit | 2922a6c0531d2854d954b899183a81c3d515df23 (patch) | |
tree | 2457b1dc6282f89891e033872813874df6026efa /Userland | |
parent | c5073cb2879843528a5c8127f80aaea0bc5d4066 (diff) | |
download | serenity-2922a6c0531d2854d954b899183a81c3d515df23.zip |
LibJS: Add the TypedArray.prototype[Symbol.toString] getter accessor
Diffstat (limited to 'Userland')
4 files changed, 22 insertions, 1 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/TypedArray.cpp b/Userland/Libraries/LibJS/Runtime/TypedArray.cpp index 9ba10cc508..5ecadf71e6 100644 --- a/Userland/Libraries/LibJS/Runtime/TypedArray.cpp +++ b/Userland/Libraries/LibJS/Runtime/TypedArray.cpp @@ -200,6 +200,11 @@ void TypedArrayBase::visit_edges(Visitor& visitor) } \ ClassName::~ClassName() { } \ \ + String ClassName::element_name() const \ + { \ + return vm().names.ClassName.as_string(); \ + } \ + \ PrototypeName::PrototypeName(GlobalObject& global_object) \ : Object(*global_object.typed_array_prototype()) \ { \ diff --git a/Userland/Libraries/LibJS/Runtime/TypedArray.h b/Userland/Libraries/LibJS/Runtime/TypedArray.h index 855299317c..44c943f69b 100644 --- a/Userland/Libraries/LibJS/Runtime/TypedArray.h +++ b/Userland/Libraries/LibJS/Runtime/TypedArray.h @@ -28,6 +28,7 @@ public: void set_viewed_array_buffer(ArrayBuffer* array_buffer) { m_viewed_array_buffer = array_buffer; } virtual size_t element_size() const = 0; + virtual String element_name() const = 0; protected: explicit TypedArrayBase(Object& prototype) @@ -151,6 +152,7 @@ private: virtual ~ClassName(); \ static ClassName* create(GlobalObject&, u32 length); \ ClassName(u32 length, Object& prototype); \ + virtual String element_name() const override; \ }; \ class PrototypeName final : public Object { \ JS_OBJECT(PrototypeName, Object); \ diff --git a/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp b/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp index 1c109c7aab..5c0612c5e1 100644 --- a/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp @@ -32,6 +32,8 @@ void TypedArrayPrototype::initialize(GlobalObject& object) define_native_function(vm.names.findIndex, find_index, 1, attr); define_native_function(vm.names.forEach, for_each, 1, attr); define_native_function(vm.names.some, some, 1, attr); + + define_native_accessor(vm.well_known_symbol_to_string_tag(), to_string_tag_getter, nullptr, Attribute::Configurable); } TypedArrayPrototype::~TypedArrayPrototype() @@ -232,4 +234,16 @@ JS_DEFINE_NATIVE_GETTER(TypedArrayPrototype::byte_offset_getter) return Value(typed_array->byte_offset()); } +// 23.2.3.32 get %TypedArray%.prototype [ @@toStringTag ], https://tc39.es/ecma262/#sec-get-%typedarray%.prototype-@@tostringtag +JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::to_string_tag_getter) +{ + auto this_value = vm.this_value(global_object); + if (!this_value.is_object()) + return js_undefined(); + auto& this_object = this_value.as_object(); + if (!this_object.is_typed_array()) + return js_undefined(); + return js_string(vm, static_cast<TypedArrayBase&>(this_object).element_name()); +} + } diff --git a/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.h b/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.h index bb939c9997..8940ed94a1 100644 --- a/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.h @@ -24,7 +24,7 @@ private: JS_DECLARE_NATIVE_GETTER(buffer_getter); JS_DECLARE_NATIVE_GETTER(byte_length_getter); JS_DECLARE_NATIVE_GETTER(byte_offset_getter); - + JS_DECLARE_NATIVE_FUNCTION(to_string_tag_getter); JS_DECLARE_NATIVE_FUNCTION(at); JS_DECLARE_NATIVE_FUNCTION(every); JS_DECLARE_NATIVE_FUNCTION(find); |