summaryrefslogtreecommitdiff
path: root/Libraries
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2020-04-28 18:10:40 +0100
committerAndreas Kling <kling@serenityos.org>2020-04-28 20:15:38 +0200
commit315255942273d4488b94a06ce266cab92277f21f (patch)
tree63eb827e98b24abd52a6a120bb6a1896ab66f8ea /Libraries
parent823cc7bc1c206a60577021b4af2138eecdefb549 (diff)
downloadserenity-315255942273d4488b94a06ce266cab92277f21f.zip
LibJS: Call Array.prototype.findIndex() callback for empty elements
If the array value at the current index is empty, the callback will be called with undefined as value.
Diffstat (limited to 'Libraries')
-rw-r--r--Libraries/LibJS/Runtime/ArrayPrototype.cpp12
-rw-r--r--Libraries/LibJS/Tests/Array.prototype.findIndex.js20
-rw-r--r--Libraries/LibJS/Tests/array-shrink-during-find-crash.js2
3 files changed, 27 insertions, 7 deletions
diff --git a/Libraries/LibJS/Runtime/ArrayPrototype.cpp b/Libraries/LibJS/Runtime/ArrayPrototype.cpp
index 9ee13271cc..5d30b07c20 100644
--- a/Libraries/LibJS/Runtime/ArrayPrototype.cpp
+++ b/Libraries/LibJS/Runtime/ArrayPrototype.cpp
@@ -489,12 +489,12 @@ Value ArrayPrototype::find_index(Interpreter& interpreter)
auto array_size = array->elements().size();
for (size_t i = 0; i < array_size; ++i) {
- if (i >= array->elements().size())
- break;
-
- auto value = array->elements().at(i);
- if (value.is_empty())
- continue;
+ auto value = js_undefined();
+ if (i < array->elements().size()) {
+ value = array->elements().at(i);
+ if (value.is_empty())
+ value = js_undefined();
+ }
MarkedValueList arguments(interpreter.heap());
arguments.append(value);
diff --git a/Libraries/LibJS/Tests/Array.prototype.findIndex.js b/Libraries/LibJS/Tests/Array.prototype.findIndex.js
index 0795a66474..f5b2295930 100644
--- a/Libraries/LibJS/Tests/Array.prototype.findIndex.js
+++ b/Libraries/LibJS/Tests/Array.prototype.findIndex.js
@@ -23,6 +23,26 @@ try {
assert(array.findIndex(value => value > 100) === -1);
assert([].findIndex(value => value === 1) === -1);
+ var callbackCalled = 0;
+ var callback = () => { callbackCalled++; };
+
+ [].findIndex(callback)
+ assert(callbackCalled === 0);
+
+ [1, 2, 3].findIndex(callback);
+ assert(callbackCalled === 3);
+
+ callbackCalled = 0;
+ [1, , , "foo", , undefined, , ,].findIndex(callback);
+ assert(callbackCalled === 8);
+
+ callbackCalled = 0;
+ [1, , , "foo", , undefined, , ,].findIndex(value => {
+ callbackCalled++;
+ return value === undefined;
+ });
+ assert(callbackCalled === 2);
+
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
diff --git a/Libraries/LibJS/Tests/array-shrink-during-find-crash.js b/Libraries/LibJS/Tests/array-shrink-during-find-crash.js
index e2e65c39ea..16a041a8e7 100644
--- a/Libraries/LibJS/Tests/array-shrink-during-find-crash.js
+++ b/Libraries/LibJS/Tests/array-shrink-during-find-crash.js
@@ -17,7 +17,7 @@ try {
callbackCalled++;
a.pop();
});
- assert(callbackCalled === 3);
+ assert(callbackCalled === 5);
console.log("PASS");
} catch (e) {