summaryrefslogtreecommitdiff
path: root/Libraries
diff options
context:
space:
mode:
Diffstat (limited to 'Libraries')
-rw-r--r--Libraries/LibJS/Runtime/ArrayPrototype.cpp12
-rw-r--r--Libraries/LibJS/Tests/Array.prototype.find.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 666b331ec5..9ee13271cc 100644
--- a/Libraries/LibJS/Runtime/ArrayPrototype.cpp
+++ b/Libraries/LibJS/Runtime/ArrayPrototype.cpp
@@ -452,12 +452,12 @@ Value ArrayPrototype::find(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.find.js b/Libraries/LibJS/Tests/Array.prototype.find.js
index cfdcedcc33..309b45cfdf 100644
--- a/Libraries/LibJS/Tests/Array.prototype.find.js
+++ b/Libraries/LibJS/Tests/Array.prototype.find.js
@@ -23,6 +23,26 @@ try {
assert(array.find(value => value > 100) === undefined);
assert([].find(value => value === 1) === undefined);
+ var callbackCalled = 0;
+ var callback = () => { callbackCalled++; };
+
+ [].find(callback)
+ assert(callbackCalled === 0);
+
+ [1, 2, 3].find(callback);
+ assert(callbackCalled === 3);
+
+ callbackCalled = 0;
+ [1, , , "foo", , undefined, , ,].find(callback);
+ assert(callbackCalled === 8);
+
+ callbackCalled = 0;
+ [1, , , "foo", , undefined, , ,].find(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 656eab3e80..e2e65c39ea 100644
--- a/Libraries/LibJS/Tests/array-shrink-during-find-crash.js
+++ b/Libraries/LibJS/Tests/array-shrink-during-find-crash.js
@@ -9,7 +9,7 @@ try {
callbackCalled++;
a.pop();
});
- assert(callbackCalled === 3);
+ assert(callbackCalled === 5);
callbackCalled = 0;
a = [1, 2, 3, 4, 5];