1
0
Fork 0
mirror of https://github.com/LadybirdBrowser/ladybird.git synced 2025-06-09 17:44:56 +09:00

LibJS: Add Array.prototype.forEach()

This commit is contained in:
Linus Groh 2020-04-13 18:49:08 +01:00 committed by Andreas Kling
parent 29253bf932
commit 866172a721
Notes: sideshowbarker 2024-07-19 07:36:25 +09:00
3 changed files with 106 additions and 0 deletions

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -31,12 +32,14 @@
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/ArrayPrototype.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/Function.h>
#include <LibJS/Runtime/Value.h>
namespace JS {
ArrayPrototype::ArrayPrototype()
{
put_native_function("forEach", for_each, 1);
put_native_function("pop", pop, 0);
put_native_function("push", push, 1);
put_native_function("shift", shift, 0);
@ -61,6 +64,43 @@ static Array* array_from(Interpreter& interpreter)
return static_cast<Array*>(this_object);
}
static Function* callback_from_args(Interpreter& interpreter, const String& name)
{
if (interpreter.argument_count() < 1) {
interpreter.throw_exception<TypeError>(String::format("Array.prototype.%s() requires at least one argument", name.characters()));
return nullptr;
}
auto callback = interpreter.argument(0);
if (!callback.is_object() || !callback.as_object().is_function()) {
interpreter.throw_exception<TypeError>(String::format("%s is not a function", callback.to_string().characters()));
return nullptr;
}
return static_cast<Function*>(&callback.as_object());
}
Value ArrayPrototype::for_each(Interpreter& interpreter)
{
auto* array = array_from(interpreter);
if (!array)
return {};
auto* callback = callback_from_args(interpreter, "forEach");
if (!callback)
return {};
auto this_value = interpreter.argument(1);
auto initial_array_size = array->elements().size();
for (size_t i = 0; i < initial_array_size; ++i) {
if (i >= array->elements().size())
break;
auto value = array->elements()[i];
if (value.is_empty())
continue;
interpreter.call(callback, this_value, { value, Value((i32)i), array });
if (interpreter.exception())
return {};
}
return js_undefined();
}
Value ArrayPrototype::push(Interpreter& interpreter)
{
auto* array = array_from(interpreter);

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -38,6 +39,7 @@ public:
private:
virtual const char* class_name() const override { return "ArrayPrototype"; }
static Value for_each(Interpreter&);
static Value pop(Interpreter&);
static Value push(Interpreter&);
static Value shift(Interpreter&);

View file

@ -0,0 +1,64 @@
load("test-common.js");
try {
assert(Array.prototype.forEach.length === 1);
try {
[].forEach();
assertNotReached();
} catch (e) {
assert(e.name === "TypeError");
assert(e.message === "Array.prototype.forEach() requires at least one argument");
}
try {
[].forEach(undefined);
assertNotReached();
} catch (e) {
assert(e.name === "TypeError");
assert(e.message === "undefined is not a function");
}
var a = [1, 2, 3];
var o = {};
var callbackCalled = 0;
var callback = () => { callbackCalled++; };
assert([].forEach(callback) === undefined);
assert(callbackCalled === 0);
assert(a.forEach(callback) === undefined);
assert(callbackCalled === 3);
callbackCalled = 0;
a.forEach(function(value, index) {
assert(value === a[index]);
assert(index === a[index] - 1);
});
callbackCalled = 0;
a.forEach(function(_, _, array) {
callbackCalled++;
assert(a.length === array.length);
a.push("test");
});
assert(callbackCalled === 3);
assert(a.length === 6);
callbackCalled = 0;
a.forEach(function(value, index) {
callbackCalled++;
this[index] = value;
}, o);
assert(callbackCalled === 6);
assert(o[0] === 1);
assert(o[1] === 2);
assert(o[2] === 3);
assert(o[3] === "test");
assert(o[4] === "test");
assert(o[5] === "test");
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}