diff options
author | Luke <luke.wilde@live.co.uk> | 2021-07-06 02:36:59 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-07-06 13:40:27 +0100 |
commit | d89f539d160e1abd9ddcefb93a9e976eec490207 (patch) | |
tree | e6e1f5ef42b264051da233a1a42ff8cdb04c3748 /Userland/Libraries/LibJS | |
parent | 275092bb035493df3eaf7f61ea03edf4c89e50a7 (diff) | |
download | serenity-d89f539d160e1abd9ddcefb93a9e976eec490207.zip |
LibJS: Add %TypedArray%.prototype.reverse
This fixes 13 test262 cases.
Diffstat (limited to 'Userland/Libraries/LibJS')
3 files changed, 111 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp b/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp index f5a10a18de..5171706f3e 100644 --- a/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp @@ -39,6 +39,7 @@ void TypedArrayPrototype::initialize(GlobalObject& object) define_native_function(vm.names.values, values, 0, attr); define_native_function(vm.names.entries, entries, 0, attr); define_native_function(vm.names.set, set, 1, attr); + define_native_function(vm.names.reverse, reverse, 0, attr); define_native_accessor(*vm.well_known_symbol_to_string_tag(), to_string_tag_getter, nullptr, Attribute::Configurable); @@ -486,4 +487,46 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::set) return js_undefined(); } +// 23.2.3.22 %TypedArray%.prototype.reverse ( ), https://tc39.es/ecma262/#sec-%typedarray%.prototype.reverse +JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::reverse) +{ + // 1. Let O be the this value. + // 2. Perform ? ValidateTypedArray(O). + auto* typed_array = typed_array_from(vm, global_object); + if (!typed_array) + return {}; + + // 3. Let len be O.[[ArrayLength]]. + auto length = typed_array->array_length(); + + // 4. Let middle be floor(len / 2). + auto middle = length / 2; + + // 5. Let lower be 0. + // 6. Repeat, while lower ≠ middle, + for (size_t lower = 0; lower != middle; ++lower) { + // a. Let upper be len - lower - 1. + auto upper = length - lower - 1; + + // b. Let upperP be ! ToString(𝔽(upper)). + // d. Let lowerValue be ! Get(O, lowerP). + auto lower_value = typed_array->get(lower); + + // c. Let lowerP be ! ToString(𝔽(lower)). + // e. Let upperValue be ! Get(O, upperP). + auto upper_value = typed_array->get(upper); + + // f. Perform ! Set(O, lowerP, upperValue, true). + typed_array->set(lower, upper_value, true); + + // g. Perform ! Set(O, upperP, lowerValue, true). + typed_array->set(upper, lower_value, true); + + // h. Set lower to lower + 1. + } + + // 7. Return O. + return typed_array; +} + } diff --git a/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.h b/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.h index 3d72709913..9b5b6f6221 100644 --- a/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.h @@ -36,6 +36,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(values); JS_DECLARE_NATIVE_FUNCTION(entries); JS_DECLARE_NATIVE_FUNCTION(set); + JS_DECLARE_NATIVE_FUNCTION(reverse); }; } diff --git a/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.reverse.js b/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.reverse.js new file mode 100644 index 0000000000..89169c2b6e --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.reverse.js @@ -0,0 +1,67 @@ +const TYPED_ARRAYS = [ + Uint8Array, + Uint8ClampedArray, + Uint16Array, + Uint32Array, + Int8Array, + Int16Array, + Int32Array, + Float32Array, + Float64Array, +]; + +const BIGINT_TYPED_ARRAYS = [BigUint64Array, BigInt64Array]; + +test("length is 0", () => { + TYPED_ARRAYS.forEach(T => { + expect(T.prototype.reverse).toHaveLength(0); + }); + + BIGINT_TYPED_ARRAYS.forEach(T => { + expect(T.prototype.reverse).toHaveLength(0); + }); +}); + +describe("basic functionality", () => { + test("Odd length array", () => { + TYPED_ARRAYS.forEach(T => { + const array = new T([1, 2, 3]); + expect(array.reverse()).toEqual([3, 2, 1]); + expect(array).toEqual([3, 2, 1]); + }); + + BIGINT_TYPED_ARRAYS.forEach(T => { + const array = new T([1n, 2n, 3n]); + expect(array.reverse()).toEqual([3n, 2n, 1n]); + expect(array).toEqual([3n, 2n, 1n]); + }); + }); + + test("Even length array", () => { + TYPED_ARRAYS.forEach(T => { + const array = new T([1, 2]); + expect(array.reverse()).toEqual([2, 1]); + expect(array).toEqual([2, 1]); + }); + + BIGINT_TYPED_ARRAYS.forEach(T => { + const array = new T([1n, 2n]); + expect(array.reverse()).toEqual([2n, 1n]); + expect(array).toEqual([2n, 1n]); + }); + }); + + test("Empty array", () => { + TYPED_ARRAYS.forEach(T => { + const array = new T([]); + expect(array.reverse()).toEqual([]); + expect(array).toEqual([]); + }); + + BIGINT_TYPED_ARRAYS.forEach(T => { + const array = new T([]); + expect(array.reverse()).toEqual([]); + expect(array).toEqual([]); + }); + }); +}); |