summaryrefslogtreecommitdiff
path: root/Userland/Libraries
diff options
context:
space:
mode:
Diffstat (limited to 'Userland/Libraries')
-rw-r--r--Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp16
-rw-r--r--Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.h2
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.some.js68
3 files changed, 86 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp b/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp
index 5b9eaf1df8..1c109c7aab 100644
--- a/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp
+++ b/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
+ * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@@ -30,6 +31,7 @@ void TypedArrayPrototype::initialize(GlobalObject& object)
define_native_function(vm.names.find, find, 1, attr);
define_native_function(vm.names.findIndex, find_index, 1, attr);
define_native_function(vm.names.forEach, for_each, 1, attr);
+ define_native_function(vm.names.some, some, 1, attr);
}
TypedArrayPrototype::~TypedArrayPrototype()
@@ -179,6 +181,20 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::for_each)
return js_undefined();
}
+// 23.2.3.25 %TypedArray%.prototype.some ( callbackfn [ , thisArg ] ), https://tc39.es/ecma262/#sec-%typedarray%.prototype.some
+JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::some)
+{
+ auto result = false;
+ for_each_item(vm, global_object, "some", [&](auto, auto, auto callback_result) {
+ if (callback_result.to_boolean()) {
+ result = true;
+ return IterationDecision::Break;
+ }
+ return IterationDecision::Continue;
+ });
+ return Value(result);
+}
+
// 23.2.3.1 get %TypedArray%.prototype.buffer, https://tc39.es/ecma262/#sec-get-%typedarray%.prototype.buffer
JS_DEFINE_NATIVE_GETTER(TypedArrayPrototype::buffer_getter)
{
diff --git a/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.h b/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.h
index 0830745140..bb939c9997 100644
--- a/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.h
+++ b/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.h
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
+ * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@@ -29,6 +30,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(find);
JS_DECLARE_NATIVE_FUNCTION(find_index);
JS_DECLARE_NATIVE_FUNCTION(for_each);
+ JS_DECLARE_NATIVE_FUNCTION(some);
};
}
diff --git a/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.some.js b/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.some.js
new file mode 100644
index 0000000000..a09588dc38
--- /dev/null
+++ b/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.some.js
@@ -0,0 +1,68 @@
+const TYPED_ARRAYS = [
+ Uint8Array,
+ Uint16Array,
+ Uint32Array,
+ Int8Array,
+ Int16Array,
+ Int32Array,
+ Float32Array,
+ Float64Array,
+];
+
+const BIGINT_TYPED_ARRAYS = [BigUint64Array, BigInt64Array];
+
+test("length is 1", () => {
+ TYPED_ARRAYS.forEach(T => {
+ expect(T.prototype.some).toHaveLength(1);
+ });
+
+ BIGINT_TYPED_ARRAYS.forEach(T => {
+ expect(T.prototype.some).toHaveLength(1);
+ });
+});
+
+describe("errors", () => {
+ function errorTests(T) {
+ test(`requires at least one argument (${T.name})`, () => {
+ expect(() => {
+ new T().some();
+ }).toThrowWithMessage(
+ TypeError,
+ "TypedArray.prototype.some() requires at least one argument"
+ );
+ });
+
+ test(`callback must be a function (${T.name})`, () => {
+ expect(() => {
+ new T().some(undefined);
+ }).toThrowWithMessage(TypeError, "undefined is not a function");
+ });
+ }
+
+ TYPED_ARRAYS.forEach(T => errorTests(T));
+ BIGINT_TYPED_ARRAYS.forEach(T => errorTests(T));
+});
+
+test("basic functionality", () => {
+ TYPED_ARRAYS.forEach(T => {
+ const typedArray = new T([2, 4, 6]);
+ expect(typedArray.some(value => value === 2)).toBeTrue();
+ expect(typedArray.some(value => value === 4)).toBeTrue();
+ expect(typedArray.some(value => value === 6)).toBeTrue();
+ expect(typedArray.some(value => value % 2 === 0)).toBeTrue();
+ expect(typedArray.some(value => value % 2 === 1)).toBeFalse();
+ expect(typedArray.some(value => value < 2)).toBeFalse();
+ expect(typedArray.some(value => value > 2)).toBeTrue();
+ });
+
+ BIGINT_TYPED_ARRAYS.forEach(T => {
+ const typedArray = new T([2n, 4n, 6n]);
+ expect(typedArray.some(value => value === 2n)).toBeTrue();
+ expect(typedArray.some(value => value === 4n)).toBeTrue();
+ expect(typedArray.some(value => value === 6n)).toBeTrue();
+ expect(typedArray.some(value => value % 2n === 0n)).toBeTrue();
+ expect(typedArray.some(value => value % 2n === 1n)).toBeFalse();
+ expect(typedArray.some(value => value < 2n)).toBeFalse();
+ expect(typedArray.some(value => value > 2n)).toBeTrue();
+ });
+});