diff options
author | Linus Groh <mail@linusgroh.de> | 2020-05-22 17:42:20 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-05-24 23:51:14 +0200 |
commit | 9b9b6a4cffd1292db3d143dbc5f76872b8278491 (patch) | |
tree | 4ab11a8959ef791d2d010be94f68afca00f761fb | |
parent | 174ac5d34842ea4f20d01b340945e265df8e4afc (diff) | |
download | serenity-9b9b6a4cffd1292db3d143dbc5f76872b8278491.zip |
LibJS: Make Array.prototype.indexOf() generic
-rw-r--r-- | Libraries/LibJS/Runtime/ArrayPrototype.cpp | 29 | ||||
-rw-r--r-- | Libraries/LibJS/Tests/Array.prototype-generic-functions.js | 9 |
2 files changed, 22 insertions, 16 deletions
diff --git a/Libraries/LibJS/Runtime/ArrayPrototype.cpp b/Libraries/LibJS/Runtime/ArrayPrototype.cpp index 6b8e29684b..fbae593b4d 100644 --- a/Libraries/LibJS/Runtime/ArrayPrototype.cpp +++ b/Libraries/LibJS/Runtime/ArrayPrototype.cpp @@ -357,35 +357,32 @@ Value ArrayPrototype::slice(Interpreter& interpreter) Value ArrayPrototype::index_of(Interpreter& interpreter) { - auto* array = array_from(interpreter); - if (!array) + auto* this_object = interpreter.this_value().to_object(interpreter); + if (!this_object) return {}; - - i32 array_size = static_cast<i32>(array->elements().size()); - if (array_size == 0) + i32 length = get_length(interpreter, *this_object); + if (interpreter.exception()) + return {}; + if (length == 0) return Value(-1); - i32 from_index = 0; if (interpreter.argument_count() >= 2) { from_index = interpreter.argument(1).to_i32(interpreter); if (interpreter.exception()) return {}; - if (from_index >= array_size) + if (from_index >= length) 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) - from_index = array_size + from_index; + if (from_index < 0) + from_index = max(length + from_index, 0); } - auto search_element = interpreter.argument(0); - for (i32 i = from_index; i < array_size; ++i) { - auto& element = array->elements().at(i); + for (i32 i = from_index; i < length; ++i) { + auto element = this_object->get_by_index(i); + if (interpreter.exception()) + return {}; if (strict_eq(interpreter, element, search_element)) return Value(i); } - return Value(-1); } diff --git a/Libraries/LibJS/Tests/Array.prototype-generic-functions.js b/Libraries/LibJS/Tests/Array.prototype-generic-functions.js index 702d575a4b..7ad699c33a 100644 --- a/Libraries/LibJS/Tests/Array.prototype-generic-functions.js +++ b/Libraries/LibJS/Tests/Array.prototype-generic-functions.js @@ -43,6 +43,15 @@ try { assert(Array.prototype.toString.call({ join: () => "foo" }) === "foo"); } + { + assert(Array.prototype.indexOf.call({}) === -1); + assert(Array.prototype.indexOf.call({ 0: undefined }) === -1); + assert(Array.prototype.indexOf.call({ length: 1, 0: undefined }) === 0); + assert(Array.prototype.indexOf.call({ length: 1, 2: "foo" }, "foo") === -1); + assert(Array.prototype.indexOf.call({ length: 5, 2: "foo" }, "foo") === 2); + assert(Array.prototype.indexOf.call({ length: 5, 2: "foo", 4: "foo" }, "foo", 3) === 4); + } + const o = { length: 5, 0: "foo", 1: "bar", 3: "baz" }; { |