summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKesse Jones <kjonesfc@outlook.com>2020-04-22 08:45:26 -0300
committerAndreas Kling <kling@serenityos.org>2020-04-23 11:06:10 +0200
commit6d308113b8ab15b9ffe97534480c9e85cc461f77 (patch)
treea3d84e864b697d3cf97950187ab327ee5df3c6e9
parentedd8abc4cfe2bd768971b20854a4775c02871a38 (diff)
downloadserenity-6d308113b8ab15b9ffe97534480c9e85cc461f77.zip
LibJS: Fix Array.prototype.indexOf fromIndex negative
If negative fromIndex considers displacement from the end of the array without decreasing 1 of de size.
-rw-r--r--Libraries/LibJS/Runtime/ArrayPrototype.cpp2
-rw-r--r--Libraries/LibJS/Tests/Array.prototype.indexOf.js3
2 files changed, 4 insertions, 1 deletions
diff --git a/Libraries/LibJS/Runtime/ArrayPrototype.cpp b/Libraries/LibJS/Runtime/ArrayPrototype.cpp
index eeb69db11b..0592d5cc73 100644
--- a/Libraries/LibJS/Runtime/ArrayPrototype.cpp
+++ b/Libraries/LibJS/Runtime/ArrayPrototype.cpp
@@ -321,7 +321,7 @@ Value ArrayPrototype::index_of(Interpreter& interpreter)
if (from_index < negative_min_index)
from_index = 0;
else if (from_index < 0)
- from_index = (array_size - 1) + from_index;
+ from_index = array_size + from_index;
}
auto search_element = interpreter.argument(0);
diff --git a/Libraries/LibJS/Tests/Array.prototype.indexOf.js b/Libraries/LibJS/Tests/Array.prototype.indexOf.js
index 40e1f72440..630745c283 100644
--- a/Libraries/LibJS/Tests/Array.prototype.indexOf.js
+++ b/Libraries/LibJS/Tests/Array.prototype.indexOf.js
@@ -14,6 +14,9 @@ try {
assert(array.indexOf(1, 1000) === -1);
assert(array.indexOf(1, -1000) === 2);
assert(array.indexOf('serenity') === -1);
+ assert(array.indexOf(false, -1) === 4);
+ assert(array.indexOf(2, -1) === -1);
+ assert(array.indexOf(2, -2) === 3);
assert([].indexOf('serenity') === -1);
assert([].indexOf('serenity', 10) === -1);
assert([].indexOf('serenity', -10) === -1);