From b0ca174d49391e16aeed729e6db2af311bdebb2e Mon Sep 17 00:00:00 2001 From: Kesse Jones Date: Thu, 23 Apr 2020 20:00:27 -0300 Subject: [PATCH] LibJS: Add Array.prototype.find --- Libraries/LibJS/Runtime/ArrayPrototype.cpp | 35 +++++++++++++++++++ Libraries/LibJS/Runtime/ArrayPrototype.h | 1 + Libraries/LibJS/Tests/Array.prototype.find.js | 29 +++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 Libraries/LibJS/Tests/Array.prototype.find.js diff --git a/Libraries/LibJS/Runtime/ArrayPrototype.cpp b/Libraries/LibJS/Runtime/ArrayPrototype.cpp index 0008cc91d1..674d500fa3 100644 --- a/Libraries/LibJS/Runtime/ArrayPrototype.cpp +++ b/Libraries/LibJS/Runtime/ArrayPrototype.cpp @@ -57,6 +57,7 @@ ArrayPrototype::ArrayPrototype() put_native_function("reverse", reverse, 0); put_native_function("lastIndexOf", last_index_of, 1); put_native_function("includes", includes, 1); + put_native_function("find", find, 1); put("length", Value(0)); } @@ -423,4 +424,38 @@ Value ArrayPrototype::includes(Interpreter& interpreter) return Value(false); } +Value ArrayPrototype::find(Interpreter& interpreter) +{ + auto* array = array_from(interpreter); + if (!array) + return {}; + + auto* callback = callback_from_args(interpreter, "find"); + if (!callback) + return {}; + + auto this_value = interpreter.argument(1); + auto array_size = array->elements().size(); + + for (size_t i = 0; i < array_size; ++i) { + auto value = array->elements().at(i); + if (value.is_empty()) + continue; + + MarkedValueList arguments(interpreter.heap()); + arguments.append(value); + arguments.append(Value((i32)i)); + arguments.append(array); + + auto result = interpreter.call(callback, this_value, move(arguments)); + if (interpreter.exception()) + return {}; + + if (result.to_boolean()) + return value; + } + + return js_undefined(); +} + } diff --git a/Libraries/LibJS/Runtime/ArrayPrototype.h b/Libraries/LibJS/Runtime/ArrayPrototype.h index 7339feac4b..b5912ee96f 100644 --- a/Libraries/LibJS/Runtime/ArrayPrototype.h +++ b/Libraries/LibJS/Runtime/ArrayPrototype.h @@ -54,5 +54,6 @@ private: static Value reverse(Interpreter&); static Value last_index_of(Interpreter&); static Value includes(Interpreter&); + static Value find(Interpreter&); }; } diff --git a/Libraries/LibJS/Tests/Array.prototype.find.js b/Libraries/LibJS/Tests/Array.prototype.find.js new file mode 100644 index 0000000000..cfdcedcc33 --- /dev/null +++ b/Libraries/LibJS/Tests/Array.prototype.find.js @@ -0,0 +1,29 @@ +load("test-common.js"); + +try { + assert(Array.prototype.find.length === 1); + + assertThrowsError(() => { + [].find(undefined); + }, { + error: TypeError, + message: "undefined is not a function" + }); + + var array = ["hello", "friends", 1, 2, false]; + + assert(array.find(value => value === "hello") === "hello"); + assert(array.find((value, index, arr) => index === 1) === "friends"); + assert(array.find(value => value == "1") === 1); + assert(array.find(value => value === 1) === 1); + assert(array.find(value => typeof(value) !== "string") === 1); + assert(array.find(value => typeof(value) === "boolean") === false); + assert(array.find(value => value > 1) === 2); + assert(array.find(value => value > 1 && value < 3) === 2); + assert(array.find(value => value > 100) === undefined); + assert([].find(value => value === 1) === undefined); + + console.log("PASS"); +} catch (e) { + console.log("FAIL: " + e); +}