From 687096cadd0bf77da0d1dad6a3e0d8c5dbbf1812 Mon Sep 17 00:00:00 2001 From: Kesse Jones Date: Thu, 23 Apr 2020 08:19:57 -0300 Subject: [PATCH] LibJS: Add Array.prototype.includes --- Libraries/LibJS/Runtime/ArrayPrototype.cpp | 35 +++++++++++++++++++ Libraries/LibJS/Runtime/ArrayPrototype.h | 1 + .../LibJS/Tests/Array.prototype.includes.js | 20 +++++++++++ 3 files changed, 56 insertions(+) create mode 100644 Libraries/LibJS/Tests/Array.prototype.includes.js diff --git a/Libraries/LibJS/Runtime/ArrayPrototype.cpp b/Libraries/LibJS/Runtime/ArrayPrototype.cpp index 0592d5cc73..0008cc91d1 100644 --- a/Libraries/LibJS/Runtime/ArrayPrototype.cpp +++ b/Libraries/LibJS/Runtime/ArrayPrototype.cpp @@ -56,6 +56,7 @@ ArrayPrototype::ArrayPrototype() put_native_function("indexOf", index_of, 1); put_native_function("reverse", reverse, 0); put_native_function("lastIndexOf", last_index_of, 1); + put_native_function("includes", includes, 1); put("length", Value(0)); } @@ -388,4 +389,38 @@ Value ArrayPrototype::last_index_of(Interpreter& interpreter) return Value(-1); } +Value ArrayPrototype::includes(Interpreter& interpreter) +{ + auto* array = array_from(interpreter); + if (!array) + return {}; + + i32 array_size = static_cast(array->elements().size()); + if (interpreter.argument_count() == 0 || array_size == 0) + return Value(false); + + i32 from_index = 0; + if (interpreter.argument_count() >= 2) { + from_index = interpreter.argument(1).to_i32(); + + if (from_index >= array_size) + return Value(false); + + 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; + } + + auto value_to_find = interpreter.argument(0); + for (i32 i = from_index; i < array_size; ++i) { + auto& element = array->elements().at(i); + if (typed_eq(interpreter, element, value_to_find).as_bool()) + return Value(true); + } + + return Value(false); +} + } diff --git a/Libraries/LibJS/Runtime/ArrayPrototype.h b/Libraries/LibJS/Runtime/ArrayPrototype.h index e7c5dd833c..7339feac4b 100644 --- a/Libraries/LibJS/Runtime/ArrayPrototype.h +++ b/Libraries/LibJS/Runtime/ArrayPrototype.h @@ -53,5 +53,6 @@ private: static Value index_of(Interpreter&); static Value reverse(Interpreter&); static Value last_index_of(Interpreter&); + static Value includes(Interpreter&); }; } diff --git a/Libraries/LibJS/Tests/Array.prototype.includes.js b/Libraries/LibJS/Tests/Array.prototype.includes.js new file mode 100644 index 0000000000..8510bd4a85 --- /dev/null +++ b/Libraries/LibJS/Tests/Array.prototype.includes.js @@ -0,0 +1,20 @@ +load("test-common.js"); + +try { + assert(Array.prototype.includes.length === 1); + + var array = ['hello', 'friends', 1, 2, false]; + + assert(array.includes('hello') === true); + assert(array.includes(1) === true); + assert(array.includes(1, -3) === true); + assert(array.includes('serenity') === false); + assert(array.includes(false, -1) === true); + assert(array.includes(2, -1) === false); + assert(array.includes(2, -100) === true); + assert(array.includes('friends', 100) === false); + + console.log("PASS"); +} catch (e) { + console.log("FAIL: " + e); +}