summaryrefslogtreecommitdiff
path: root/Libraries/LibJS
diff options
context:
space:
mode:
authorLuke <luke.wilde@live.co.uk>2020-05-21 07:11:34 -0700
committerAndreas Kling <kling@serenityos.org>2020-05-21 19:44:59 +0200
commit57d15acd4cb16e479d2ec79d674318ae388dc328 (patch)
tree76df536db80824425916663b7d190df939a26a24 /Libraries/LibJS
parent4cdbdf0a84d7fe4c028f55dc691691b09819dc78 (diff)
downloadserenity-57d15acd4cb16e479d2ec79d674318ae388dc328.zip
LibJS: Add Array.prototype.every
Diffstat (limited to 'Libraries/LibJS')
-rw-r--r--Libraries/LibJS/Runtime/ArrayPrototype.cpp38
-rw-r--r--Libraries/LibJS/Runtime/ArrayPrototype.h1
-rw-r--r--Libraries/LibJS/Tests/Array.prototype.every.js44
3 files changed, 83 insertions, 0 deletions
diff --git a/Libraries/LibJS/Runtime/ArrayPrototype.cpp b/Libraries/LibJS/Runtime/ArrayPrototype.cpp
index 333c11046e..911b7be389 100644
--- a/Libraries/LibJS/Runtime/ArrayPrototype.cpp
+++ b/Libraries/LibJS/Runtime/ArrayPrototype.cpp
@@ -62,6 +62,7 @@ ArrayPrototype::ArrayPrototype()
put_native_function("find", find, 1, attr);
put_native_function("findIndex", find_index, 1, attr);
put_native_function("some", some, 1, attr);
+ put_native_function("every", every, 1, attr);
put("length", Value(0), Attribute::Configurable);
}
@@ -560,4 +561,41 @@ Value ArrayPrototype::some(Interpreter& interpreter)
return Value(false);
}
+Value ArrayPrototype::every(Interpreter& interpreter)
+{
+ auto* array = array_from(interpreter);
+ if (!array)
+ return {};
+
+ auto* callback = callback_from_args(interpreter, "every");
+ 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(false);
+ }
+
+ return Value(true);
+}
+
}
diff --git a/Libraries/LibJS/Runtime/ArrayPrototype.h b/Libraries/LibJS/Runtime/ArrayPrototype.h
index 70802e9cfa..b77096a818 100644
--- a/Libraries/LibJS/Runtime/ArrayPrototype.h
+++ b/Libraries/LibJS/Runtime/ArrayPrototype.h
@@ -57,6 +57,7 @@ private:
static Value find(Interpreter&);
static Value find_index(Interpreter&);
static Value some(Interpreter&);
+ static Value every(Interpreter&);
};
}
diff --git a/Libraries/LibJS/Tests/Array.prototype.every.js b/Libraries/LibJS/Tests/Array.prototype.every.js
new file mode 100644
index 0000000000..f660c0166f
--- /dev/null
+++ b/Libraries/LibJS/Tests/Array.prototype.every.js
@@ -0,0 +1,44 @@
+load("test-common.js");
+
+try {
+ assert(Array.prototype.every.length === 1);
+
+ assertThrowsError(() => {
+ [].every(undefined);
+ }, {
+ error: TypeError,
+ message: "undefined is not a function"
+ });
+
+ var arrayOne = ["serenity", { test: "serenity"} ];
+ var arrayTwo = [true, false, 1, 2, 3, "3"];
+
+ assert(arrayOne.every(value => value === "hello") === false);
+ assert(arrayOne.every(value => value === "serenity") === false);
+ assert(arrayOne.every((value, index, arr) => index < 2) === true);
+ assert(arrayOne.every(value => typeof(value) === "string") === false);
+ assert(arrayOne.every(value => arrayOne.pop()) === true);
+
+ assert(arrayTwo.every((value, index, arr) => index > 0) === false);
+ assert(arrayTwo.every((value, index, arr) => index >= 0) === true);
+ assert(arrayTwo.every(value => typeof(value) !== "string") === false);
+ assert(arrayTwo.every(value => typeof(value) === "number") === false);
+ assert(arrayTwo.every(value => value > 0) === false);
+ assert(arrayTwo.every(value => value >= 0 && value < 4) === true);
+ assert(arrayTwo.every(value => arrayTwo.pop()) === true);
+
+ assert(["", "hello", "friends", "serenity"].every(value => value.length >= 0) === true);
+ assert([].every(value => value === 1) === true);
+
+ arrayTwo = [true, false, 1, 2, 3, "3"];
+
+ // Every only goes up to the original length.
+ assert(arrayTwo.every((value, index, arr) => {
+ arr.push("serenity");
+ return value < 4;
+ }) === true);
+
+ console.log("PASS");
+} catch (e) {
+ console.log("FAIL: " + e);
+}