diff options
author | Idan Horowitz <idan.horowitz@gmail.com> | 2021-06-17 18:25:49 +0300 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-06-18 20:17:45 +0100 |
commit | 57db27bcc4b707945fb512dcd95e9de62c323a72 (patch) | |
tree | 571fb71e16ce317368e409003fab0241cf54d2f5 /Userland | |
parent | 2922a6c0531d2854d954b899183a81c3d515df23 (diff) | |
download | serenity-57db27bcc4b707945fb512dcd95e9de62c323a72.zip |
LibJS: Add the TypedArray.prototype.join method
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp | 33 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.h | 1 |
2 files changed, 34 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp b/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp index 5c0612c5e1..49dd8536ca 100644 --- a/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp @@ -32,6 +32,7 @@ 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_function(vm.names.join, join, 1, attr); define_native_accessor(vm.well_known_symbol_to_string_tag(), to_string_tag_getter, nullptr, Attribute::Configurable); } @@ -197,6 +198,38 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::some) return Value(result); } +// 23.2.3.15 %TypedArray%.prototype.join ( separator ), https://tc39.es/ecma262/#sec-%typedarray%.prototype.join +JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::join) +{ + auto typed_array = typed_array_from(vm, global_object); + if (!typed_array) + return {}; + auto length = typed_array->array_length(); + String separator = ","; + if (!vm.argument(0).is_undefined()) { + separator = vm.argument(0).to_string(global_object); + if (vm.exception()) + return {}; + } + + StringBuilder builder; + for (size_t i = 0; i < length; ++i) { + if (i > 0) + builder.append(separator); + auto value = typed_array->get(i).value_or(js_undefined()); + if (vm.exception()) + return {}; + if (value.is_nullish()) + continue; + auto string = value.to_string(global_object); + if (vm.exception()) + return {}; + builder.append(string); + } + + return js_string(vm, builder.to_string()); +} + // 23.2.3.1 get %TypedArray%.prototype.buffer, https://tc39.es/ecma262/#sec-get-%typedarray%.prototype.buffer JS_DEFINE_NATIVE_GETTER(TypedArrayPrototype::buffer_getter) { diff --git a/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.h b/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.h index 8940ed94a1..72d58b79a3 100644 --- a/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.h @@ -31,6 +31,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(find_index); JS_DECLARE_NATIVE_FUNCTION(for_each); JS_DECLARE_NATIVE_FUNCTION(some); + JS_DECLARE_NATIVE_FUNCTION(join); }; } |