diff options
author | Kesse Jones <kjonesfc@outlook.com> | 2020-04-27 20:41:57 -0300 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-04-28 13:11:10 +0200 |
commit | 1c4d776cccdbc02e62323e7a1c07a1e9f329cc7e (patch) | |
tree | cfd631aab9ab7bc35935cd049194618a58df7642 /Libraries | |
parent | c14fedd5629cf45f4a7b64e6c87d87181432c42d (diff) | |
download | serenity-1c4d776cccdbc02e62323e7a1c07a1e9f329cc7e.zip |
LibJS: Add Array.prototype.some
Diffstat (limited to 'Libraries')
-rw-r--r-- | Libraries/LibJS/Runtime/ArrayPrototype.cpp | 38 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/ArrayPrototype.h | 2 | ||||
-rw-r--r-- | Libraries/LibJS/Tests/Array.prototype.some.js | 34 |
3 files changed, 74 insertions, 0 deletions
diff --git a/Libraries/LibJS/Runtime/ArrayPrototype.cpp b/Libraries/LibJS/Runtime/ArrayPrototype.cpp index 0cd228cf6a..1d3c563d42 100644 --- a/Libraries/LibJS/Runtime/ArrayPrototype.cpp +++ b/Libraries/LibJS/Runtime/ArrayPrototype.cpp @@ -61,6 +61,7 @@ ArrayPrototype::ArrayPrototype() put_native_function("includes", includes, 1, attr); put_native_function("find", find, 1, attr); put_native_function("findIndex", find_index, 1, attr); + put_native_function("some", some, 1, attr); put("length", Value(0), Attribute::Configurable); } @@ -501,4 +502,41 @@ Value ArrayPrototype::find_index(Interpreter& interpreter) return Value(-1); } +Value ArrayPrototype::some(Interpreter& interpreter) +{ + auto* array = array_from(interpreter); + if (!array) + return {}; + + auto* callback = callback_from_args(interpreter, "some"); + if (!callback) + return {}; + + auto this_value = interpreter.argument(1); + auto array_size = array->elements().size(); + + for (size_t i = 0; i < array_size; ++i) { + if (i >= array->elements().size()) + break; + + auto value = array->elements().at(i); + if (value.is_empty()) + continue; + + MarkedValueList arguments(interpreter.heap()); + arguments.append(value); + arguments.append(Value((i32)i)); + arguments.append(array); + + auto result = interpreter.call(callback, this_value, move(arguments)); + if (interpreter.exception()) + return {}; + + if (result.to_boolean()) + return Value(true); + } + + return Value(false); +} + } diff --git a/Libraries/LibJS/Runtime/ArrayPrototype.h b/Libraries/LibJS/Runtime/ArrayPrototype.h index 30f4c6f000..70802e9cfa 100644 --- a/Libraries/LibJS/Runtime/ArrayPrototype.h +++ b/Libraries/LibJS/Runtime/ArrayPrototype.h @@ -56,5 +56,7 @@ private: static Value includes(Interpreter&); static Value find(Interpreter&); static Value find_index(Interpreter&); + static Value some(Interpreter&); }; + } diff --git a/Libraries/LibJS/Tests/Array.prototype.some.js b/Libraries/LibJS/Tests/Array.prototype.some.js new file mode 100644 index 0000000000..28d5676d35 --- /dev/null +++ b/Libraries/LibJS/Tests/Array.prototype.some.js @@ -0,0 +1,34 @@ +load("test-common.js"); + +try { + assert(Array.prototype.some.length === 1); + + assertThrowsError(() => { + [].some(undefined); + }, { + error: TypeError, + message: "undefined is not a function" + }); + + var array = ["hello", "friends", 1, 2, false, -42, { name: "serenityos"}]; + + assert(array.some(value => value === "hello") === true); + assert(array.some(value => value === "serenity") === false); + assert(array.some((value, index, arr) => index === 1) === true); + assert(array.some(value => value == "1") === true); + assert(array.some(value => value === 1) === true); + assert(array.some(value => value === 13) === false); + assert(array.some(value => typeof(value) !== "string") === true); + assert(array.some(value => typeof(value) === "boolean") === true); + assert(array.some(value => value > 1) === true); + assert(array.some(value => value > 1 && value < 3) === true); + assert(array.some(value => value > 100) === false); + assert(array.some(value => value < 0) === true); + assert(array.some(value => array.pop()) === true); + assert(["", "hello", "friends", "serenity"].some(value => value.length === 0) === true); + assert([].some(value => value === 1) === false); + + console.log("PASS"); +} catch (e) { + console.log("FAIL: " + e); +} |