mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-23 04:55:15 +00:00
LibJS: Fix Array.prototype.lastIndexOf() implementation
This commit is contained in:
parent
6a4280e6e5
commit
843e000f18
Notes:
sideshowbarker
2024-07-19 06:14:37 +09:00
Author: https://github.com/linusg Commit: https://github.com/SerenityOS/serenity/commit/843e000f187 Pull-request: https://github.com/SerenityOS/serenity/pull/2332
2 changed files with 8 additions and 10 deletions
|
@ -414,22 +414,19 @@ Value ArrayPrototype::last_index_of(Interpreter& interpreter)
|
|||
if (array_size == 0)
|
||||
return Value(-1);
|
||||
|
||||
i32 from_index = 0;
|
||||
i32 from_index = array_size - 1;
|
||||
if (interpreter.argument_count() >= 2) {
|
||||
from_index = interpreter.argument(1).to_i32(interpreter);
|
||||
if (interpreter.exception())
|
||||
return {};
|
||||
if (from_index >= array_size)
|
||||
return Value(-1);
|
||||
auto negative_min_index = ((array_size - 1) * -1);
|
||||
if (from_index < negative_min_index)
|
||||
from_index = 0;
|
||||
else if (from_index < 0)
|
||||
if (from_index >= 0)
|
||||
from_index = min(from_index, array_size - 1);
|
||||
else
|
||||
from_index = array_size + from_index;
|
||||
}
|
||||
|
||||
auto search_element = interpreter.argument(0);
|
||||
for (i32 i = array_size - 1; i >= from_index; --i) {
|
||||
for (i32 i = from_index; i >= 0; --i) {
|
||||
auto& element = array->elements().at(i);
|
||||
if (strict_eq(interpreter, element, search_element))
|
||||
return Value(i);
|
||||
|
|
|
@ -6,11 +6,12 @@ try {
|
|||
var array = [1, 2, 3, 1, "hello"];
|
||||
|
||||
assert(array.lastIndexOf("hello") === 4);
|
||||
assert(array.lastIndexOf("hello", 1000) === 4);
|
||||
assert(array.lastIndexOf(1) === 3);
|
||||
assert(array.lastIndexOf(1, -1) === -1);
|
||||
assert(array.lastIndexOf(1, -1) === 3);
|
||||
assert(array.lastIndexOf(1, -2) === 3);
|
||||
assert(array.lastIndexOf(2) === 1);
|
||||
assert(array.lastIndexOf(2, -3) === -1);
|
||||
assert(array.lastIndexOf(2, -3) === 1);
|
||||
assert(array.lastIndexOf(2, -4) === 1);
|
||||
assert([].lastIndexOf('hello') === -1);
|
||||
assert([].lastIndexOf('hello', 10) === -1);
|
||||
|
|
Loading…
Add table
Reference in a new issue