From 241f9f21d471097f4e6229989f818527dae52f6e Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Thu, 8 Jul 2021 05:07:30 +0300 Subject: [PATCH] LibJS: Add %TypedArray%.prototype.reduceRight --- .../LibJS/Runtime/TypedArrayPrototype.cpp | 39 +++++++++++++++ .../LibJS/Runtime/TypedArrayPrototype.h | 1 + .../TypedArray.prototype.reduceRight.js | 47 +++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.reduceRight.js diff --git a/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp b/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp index 4376d8fef78..a5477675673 100644 --- a/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp @@ -38,6 +38,7 @@ void TypedArrayPrototype::initialize(GlobalObject& object) define_native_function(vm.names.indexOf, index_of, 1, attr); define_native_function(vm.names.lastIndexOf, last_index_of, 1, attr); define_native_function(vm.names.reduce, reduce, 1, attr); + define_native_function(vm.names.reduceRight, reduce_right, 1, attr); define_native_function(vm.names.some, some, 1, attr); define_native_function(vm.names.join, join, 1, attr); define_native_function(vm.names.keys, keys, 0, attr); @@ -443,6 +444,44 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::reduce) return accumulator; } +// 23.2.3.21 %TypedArray%.prototype.reduceRight ( callbackfn [ , initialValue ] ), https://tc39.es/ecma262/#sec-%typedarray%.prototype.reduce +JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::reduce_right) +{ + auto* typed_array = typed_array_from(vm, global_object); + if (!typed_array) + return {}; + + auto length = typed_array->array_length(); + + auto* callback_function = callback_from_args(global_object, vm.names.reduce.as_string()); + if (!callback_function) + return {}; + + if (length == 0 && vm.argument_count() <= 1) { + vm.throw_exception(global_object, ErrorType::ReduceNoInitial); + return {}; + } + + i32 k = (i32)length - 1; + Value accumulator; + if (vm.argument_count() > 1) { + accumulator = vm.argument(1); + } else { + accumulator = typed_array->get(k); + --k; + } + + for (; k >= 0; --k) { + auto k_value = typed_array->get(k); + + accumulator = vm.call(*callback_function, js_undefined(), accumulator, k_value, Value(k), typed_array); + if (vm.exception()) + return {}; + } + + return accumulator; +} + // 23.2.3.25 %TypedArray%.prototype.some ( callbackfn [ , thisArg ] ), https://tc39.es/ecma262/#sec-%typedarray%.prototype.some JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::some) { diff --git a/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.h b/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.h index 34d63af5353..0e34c940800 100644 --- a/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.h @@ -35,6 +35,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(index_of); JS_DECLARE_NATIVE_FUNCTION(last_index_of); JS_DECLARE_NATIVE_FUNCTION(reduce); + JS_DECLARE_NATIVE_FUNCTION(reduce_right); JS_DECLARE_NATIVE_FUNCTION(some); JS_DECLARE_NATIVE_FUNCTION(join); JS_DECLARE_NATIVE_FUNCTION(keys); diff --git a/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.reduceRight.js b/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.reduceRight.js new file mode 100644 index 00000000000..6bc0df3fde4 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.reduceRight.js @@ -0,0 +1,47 @@ +const TYPED_ARRAYS = [ + Uint8Array, + Uint8ClampedArray, + Uint16Array, + Uint32Array, + Int8Array, + Int16Array, + Int32Array, + Float32Array, + Float64Array, +]; + +const BIGINT_TYPED_ARRAYS = [BigUint64Array, BigInt64Array]; + +test("basic functionality", () => { + TYPED_ARRAYS.forEach(T => { + expect(T.prototype.reduceRight).toHaveLength(1); + + const typedArray = new T(3); + typedArray[0] = 1; + typedArray[1] = 2; + typedArray[2] = 3; + + expect(typedArray.reduceRight((accumulator, value) => accumulator + value)).toBe(6); + expect(typedArray.reduceRight((accumulator, value) => accumulator + value, -5)).toBe(1); + + const order = []; + typedArray.reduceRight((accumulator, value) => order.push(value), 0); + expect(order).toEqual([3, 2, 1]); + }); + + BIGINT_TYPED_ARRAYS.forEach(T => { + expect(T.prototype.reduceRight).toHaveLength(1); + + const typedArray = new T(3); + typedArray[0] = 1n; + typedArray[1] = 2n; + typedArray[2] = 3n; + + expect(typedArray.reduceRight((accumulator, value) => accumulator + value)).toBe(6n); + expect(typedArray.reduceRight((accumulator, value) => accumulator + value, -5n)).toBe(1n); + + const order = []; + typedArray.reduceRight((accumulator, value) => order.push(value), 0); + expect(order).toEqual([3n, 2n, 1n]); + }); +});