summaryrefslogtreecommitdiff
path: root/Libraries
diff options
context:
space:
mode:
Diffstat (limited to 'Libraries')
-rw-r--r--Libraries/LibJS/Runtime/ArrayPrototype.cpp38
-rw-r--r--Libraries/LibJS/Runtime/ArrayPrototype.h2
-rw-r--r--Libraries/LibJS/Tests/Array.prototype.some.js34
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);
+}