diff --git a/Libraries/LibJS/Runtime/ArrayPrototype.cpp b/Libraries/LibJS/Runtime/ArrayPrototype.cpp index 4b8e338c6d..85db06f95d 100644 --- a/Libraries/LibJS/Runtime/ArrayPrototype.cpp +++ b/Libraries/LibJS/Runtime/ArrayPrototype.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2020, Andreas Kling + * Copyright (c) 2020, Linus Groh * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -31,12 +32,14 @@ #include #include #include +#include #include 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(this_object); } +static Function* callback_from_args(Interpreter& interpreter, const String& name) +{ + if (interpreter.argument_count() < 1) { + interpreter.throw_exception(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(String::format("%s is not a function", callback.to_string().characters())); + return nullptr; + } + return static_cast(&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); diff --git a/Libraries/LibJS/Runtime/ArrayPrototype.h b/Libraries/LibJS/Runtime/ArrayPrototype.h index 653ff885f5..ceb50b32c0 100644 --- a/Libraries/LibJS/Runtime/ArrayPrototype.h +++ b/Libraries/LibJS/Runtime/ArrayPrototype.h @@ -1,5 +1,6 @@ /* * Copyright (c) 2020, Andreas Kling + * Copyright (c) 2020, Linus Groh * 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&); diff --git a/Libraries/LibJS/Tests/Array.prototype.forEach.js b/Libraries/LibJS/Tests/Array.prototype.forEach.js new file mode 100644 index 0000000000..5e10399aee --- /dev/null +++ b/Libraries/LibJS/Tests/Array.prototype.forEach.js @@ -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); +}