From da49a7a9f80f511022961c8b001fcf543f87b39c Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Thu, 8 Jul 2021 04:26:27 +0300 Subject: [PATCH] LibJS: Add %TypedArray%.prototype.includes --- .../LibJS/Runtime/TypedArrayPrototype.cpp | 44 +++++++++++++++++++ .../LibJS/Runtime/TypedArrayPrototype.h | 1 + .../TypedArray.prototype.includes.js | 44 +++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.includes.js diff --git a/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp b/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp index b2cbaacab5..86b37fc8c9 100644 --- a/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp @@ -34,6 +34,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.includes, includes, 1, attr); define_native_function(vm.names.some, some, 1, attr); define_native_function(vm.names.join, join, 1, attr); define_native_function(vm.names.keys, keys, 0, attr); @@ -264,6 +265,49 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::for_each) return js_undefined(); } +// 23.2.3.13 %TypedArray%.prototype.includes ( searchElement [ , fromIndex ] ), https://tc39.es/ecma262/#sec-%typedarray%.prototype.includes +JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::includes) +{ + auto typed_array = typed_array_from(vm, global_object); + if (!typed_array) + return {}; + + auto length = typed_array->array_length(); + + if (length == 0) + return Value(false); + + auto n = vm.argument(1).to_integer_or_infinity(global_object); + if (vm.exception()) + return {}; + + auto value_n = Value(n); + if (value_n.is_positive_infinity()) + return Value(false); + else if (value_n.is_negative_infinity()) + n = 0; + + u32 k; + if (n >= 0) { + k = n; + } else { + auto relative_k = length + n; + if (relative_k < 0) + relative_k = 0; + k = relative_k; + } + + auto search_element = vm.argument(0); + for (; k < length; ++k) { + auto element_k = typed_array->get(k); + + if (same_value_zero(search_element, element_k)) + return Value(true); + } + + return Value(false); +} + // 23.2.3.25 %TypedArray%.prototype.some ( callbackfn [ , thisArg ] ), https://tc39.es/ecma262/#sec-%typedarray%.prototype.some JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::some) { diff --git a/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.h b/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.h index a5cdae86df..c2aa98fe56 100644 --- a/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.h @@ -31,6 +31,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(find); JS_DECLARE_NATIVE_FUNCTION(find_index); JS_DECLARE_NATIVE_FUNCTION(for_each); + JS_DECLARE_NATIVE_FUNCTION(includes); JS_DECLARE_NATIVE_FUNCTION(some); JS_DECLARE_NATIVE_FUNCTION(join); JS_DECLARE_NATIVE_FUNCTION(keys); diff --git a/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.includes.js b/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.includes.js new file mode 100644 index 0000000000..f1a1eda27e --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.includes.js @@ -0,0 +1,44 @@ +const TYPED_ARRAYS = [ + Uint8Array, + Uint8ClampedArray, + Uint16Array, + Uint32Array, + Int8Array, + Int16Array, + Int32Array, + Float32Array, + Float64Array, +]; + +const BIGINT_TYPED_ARRAYS = [BigUint64Array, BigInt64Array]; + +test("basic functionality", () => { + TYPED_ARRAYS.forEach(T => { + expect(T.prototype.includes).toHaveLength(1); + + const typedArray = new T(3); + typedArray[0] = 1; + typedArray[1] = 2; + typedArray[2] = 3; + + expect(typedArray.includes(2)).toBe(true); + expect(typedArray.includes(-1)).toBe(false); + expect(typedArray.includes(Infinity)).toBe(false); + expect(typedArray.includes(2, 2)).toBe(false); + expect(typedArray.includes(2, -2)).toBe(true); + }); + + BIGINT_TYPED_ARRAYS.forEach(T => { + expect(T.prototype.includes).toHaveLength(1); + + const typedArray = new T(3); + typedArray[0] = 1n; + typedArray[1] = 2n; + typedArray[2] = 3n; + + expect(typedArray.includes(2n)).toBe(true); + expect(typedArray.includes(-1n)).toBe(false); + expect(typedArray.includes(2n, 2)).toBe(false); + expect(typedArray.includes(2n, -2)).toBe(true); + }); +});