summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJamie Mansfield <jmansfield@cadixdev.org>2022-11-19 12:05:29 +0000
committerLinus Groh <mail@linusgroh.de>2022-11-19 14:23:28 +0000
commit43456ad708838b4702b023d5a3d2d3acaf3f221b (patch)
tree7d645422f140297b2f6755da0a9e35b3d9b490ba
parent364d873e3326d52d26bee2d6dafb944c8b7a174c (diff)
downloadserenity-43456ad708838b4702b023d5a3d2d3acaf3f221b.zip
LibJS: Add spec comments to TypedArray.prototype.reduceRight
-rw-r--r--Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp26
1 files changed, 25 insertions, 1 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp b/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp
index 0915c4cbb5..d8badaa92c 100644
--- a/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp
+++ b/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp
@@ -865,30 +865,54 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::reduce)
// 23.2.3.24 %TypedArray%.prototype.reduceRight ( callbackfn [ , initialValue ] ), https://tc39.es/ecma262/#sec-%typedarray%.prototype.reduce
JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::reduce_right)
{
+ // 1. Let O be the this value.
+ // 2. Perform ? ValidateTypedArray(O).
auto* typed_array = TRY(validate_typed_array_from_this(vm));
+ // 3. Let len be O.[[ArrayLength]].
auto length = typed_array->array_length();
+ // 4. If IsCallable(callbackfn) is false, throw a TypeError exception.
auto* callback_function = TRY(callback_from_args(vm, vm.names.reduce.as_string()));
+ // 5. If len is 0 and initialValue is not present, throw a TypeError exception.
if (length == 0 && vm.argument_count() <= 1)
return vm.throw_completion<TypeError>(ErrorType::ReduceNoInitial);
+ // 6. Let k be len - 1.
i32 k = (i32)length - 1;
+
+ // 7. Let accumulator be undefined.
Value accumulator;
+
+ // 8. If initialValue is present, then
if (vm.argument_count() > 1) {
+ // a. Set accumulator to initialValue.
accumulator = vm.argument(1);
- } else {
+ }
+ // 9. Else,
+ else {
+ // a. Let Pk be ! ToString(๐”ฝ(k)).
+ // b. Set accumulator to ! Get(O, Pk).
accumulator = MUST(typed_array->get(k));
+
+ // c. Set k to k - 1.
--k;
}
+ // 10. Repeat, while k โ‰ฅ 0,
for (; k >= 0; --k) {
+ // a. Let Pk be ! ToString(๐”ฝ(k)).
+ // b. Let kValue be ! Get(O, Pk).
auto k_value = MUST(typed_array->get(k));
+ // c. Set accumulator to ? Call(callbackfn, undefined, ยซ accumulator, kValue, ๐”ฝ(k), O ยป).
accumulator = TRY(call(vm, *callback_function, js_undefined(), accumulator, k_value, Value(k), typed_array));
+
+ // d. Set k to k - 1.
}
+ // 11. Return accumulator.
return accumulator;
}