From 6a4280e6e52533a9fc242826dacd91c1c43142c8 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Fri, 22 May 2020 17:34:52 +0100 Subject: LibJS: Treat missing arg in Array.prototype.{indexOf,lastIndexOf}() as undefined --- Libraries/LibJS/Runtime/ArrayPrototype.cpp | 4 ++-- Libraries/LibJS/Tests/Array.prototype.indexOf.js | 2 ++ Libraries/LibJS/Tests/Array.prototype.lastIndexOf.js | 3 +++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Libraries/LibJS/Runtime/ArrayPrototype.cpp b/Libraries/LibJS/Runtime/ArrayPrototype.cpp index ecd14217c1..edfa930ed2 100644 --- a/Libraries/LibJS/Runtime/ArrayPrototype.cpp +++ b/Libraries/LibJS/Runtime/ArrayPrototype.cpp @@ -357,7 +357,7 @@ Value ArrayPrototype::index_of(Interpreter& interpreter) return {}; i32 array_size = static_cast(array->elements().size()); - if (interpreter.argument_count() == 0 || array_size == 0) + if (array_size == 0) return Value(-1); i32 from_index = 0; @@ -411,7 +411,7 @@ Value ArrayPrototype::last_index_of(Interpreter& interpreter) return {}; i32 array_size = static_cast(array->elements().size()); - if (interpreter.argument_count() == 0 || array_size == 0) + if (array_size == 0) return Value(-1); i32 from_index = 0; diff --git a/Libraries/LibJS/Tests/Array.prototype.indexOf.js b/Libraries/LibJS/Tests/Array.prototype.indexOf.js index 630745c283..12cc3e7333 100644 --- a/Libraries/LibJS/Tests/Array.prototype.indexOf.js +++ b/Libraries/LibJS/Tests/Array.prototype.indexOf.js @@ -20,6 +20,8 @@ try { assert([].indexOf('serenity') === -1); assert([].indexOf('serenity', 10) === -1); assert([].indexOf('serenity', -10) === -1); + assert([].indexOf() === -1); + assert([undefined].indexOf() === 0); console.log("PASS"); } catch (e) { diff --git a/Libraries/LibJS/Tests/Array.prototype.lastIndexOf.js b/Libraries/LibJS/Tests/Array.prototype.lastIndexOf.js index 642c897292..1ae10c4e55 100644 --- a/Libraries/LibJS/Tests/Array.prototype.lastIndexOf.js +++ b/Libraries/LibJS/Tests/Array.prototype.lastIndexOf.js @@ -15,6 +15,9 @@ try { assert([].lastIndexOf('hello') === -1); assert([].lastIndexOf('hello', 10) === -1); assert([].lastIndexOf('hello', -10) === -1); + assert([].lastIndexOf() === -1); + assert([undefined].lastIndexOf() === 0); + assert([undefined, undefined, undefined].lastIndexOf() === 2); console.log("PASS"); } catch (e) { -- cgit v1.2.3