Skip to content

Commit d89f539

Browse files
Lubrsilinusg
authored andcommitted
LibJS: Add %TypedArray%.prototype.reverse
This fixes 13 test262 cases.
1 parent 275092b commit d89f539

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ void TypedArrayPrototype::initialize(GlobalObject& object)
3939
define_native_function(vm.names.values, values, 0, attr);
4040
define_native_function(vm.names.entries, entries, 0, attr);
4141
define_native_function(vm.names.set, set, 1, attr);
42+
define_native_function(vm.names.reverse, reverse, 0, attr);
4243

4344
define_native_accessor(*vm.well_known_symbol_to_string_tag(), to_string_tag_getter, nullptr, Attribute::Configurable);
4445

@@ -486,4 +487,46 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::set)
486487
return js_undefined();
487488
}
488489

490+
// 23.2.3.22 %TypedArray%.prototype.reverse ( ), https://tc39.es/ecma262/#sec-%typedarray%.prototype.reverse
491+
JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::reverse)
492+
{
493+
// 1. Let O be the this value.
494+
// 2. Perform ? ValidateTypedArray(O).
495+
auto* typed_array = typed_array_from(vm, global_object);
496+
if (!typed_array)
497+
return {};
498+
499+
// 3. Let len be O.[[ArrayLength]].
500+
auto length = typed_array->array_length();
501+
502+
// 4. Let middle be floor(len / 2).
503+
auto middle = length / 2;
504+
505+
// 5. Let lower be 0.
506+
// 6. Repeat, while lower ≠ middle,
507+
for (size_t lower = 0; lower != middle; ++lower) {
508+
// a. Let upper be len - lower - 1.
509+
auto upper = length - lower - 1;
510+
511+
// b. Let upperP be ! ToString(𝔽(upper)).
512+
// d. Let lowerValue be ! Get(O, lowerP).
513+
auto lower_value = typed_array->get(lower);
514+
515+
// c. Let lowerP be ! ToString(𝔽(lower)).
516+
// e. Let upperValue be ! Get(O, upperP).
517+
auto upper_value = typed_array->get(upper);
518+
519+
// f. Perform ! Set(O, lowerP, upperValue, true).
520+
typed_array->set(lower, upper_value, true);
521+
522+
// g. Perform ! Set(O, upperP, lowerValue, true).
523+
typed_array->set(upper, lower_value, true);
524+
525+
// h. Set lower to lower + 1.
526+
}
527+
528+
// 7. Return O.
529+
return typed_array;
530+
}
531+
489532
}

Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class TypedArrayPrototype final : public Object {
3636
JS_DECLARE_NATIVE_FUNCTION(values);
3737
JS_DECLARE_NATIVE_FUNCTION(entries);
3838
JS_DECLARE_NATIVE_FUNCTION(set);
39+
JS_DECLARE_NATIVE_FUNCTION(reverse);
3940
};
4041

4142
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
const TYPED_ARRAYS = [
2+
Uint8Array,
3+
Uint8ClampedArray,
4+
Uint16Array,
5+
Uint32Array,
6+
Int8Array,
7+
Int16Array,
8+
Int32Array,
9+
Float32Array,
10+
Float64Array,
11+
];
12+
13+
const BIGINT_TYPED_ARRAYS = [BigUint64Array, BigInt64Array];
14+
15+
test("length is 0", () => {
16+
TYPED_ARRAYS.forEach(T => {
17+
expect(T.prototype.reverse).toHaveLength(0);
18+
});
19+
20+
BIGINT_TYPED_ARRAYS.forEach(T => {
21+
expect(T.prototype.reverse).toHaveLength(0);
22+
});
23+
});
24+
25+
describe("basic functionality", () => {
26+
test("Odd length array", () => {
27+
TYPED_ARRAYS.forEach(T => {
28+
const array = new T([1, 2, 3]);
29+
expect(array.reverse()).toEqual([3, 2, 1]);
30+
expect(array).toEqual([3, 2, 1]);
31+
});
32+
33+
BIGINT_TYPED_ARRAYS.forEach(T => {
34+
const array = new T([1n, 2n, 3n]);
35+
expect(array.reverse()).toEqual([3n, 2n, 1n]);
36+
expect(array).toEqual([3n, 2n, 1n]);
37+
});
38+
});
39+
40+
test("Even length array", () => {
41+
TYPED_ARRAYS.forEach(T => {
42+
const array = new T([1, 2]);
43+
expect(array.reverse()).toEqual([2, 1]);
44+
expect(array).toEqual([2, 1]);
45+
});
46+
47+
BIGINT_TYPED_ARRAYS.forEach(T => {
48+
const array = new T([1n, 2n]);
49+
expect(array.reverse()).toEqual([2n, 1n]);
50+
expect(array).toEqual([2n, 1n]);
51+
});
52+
});
53+
54+
test("Empty array", () => {
55+
TYPED_ARRAYS.forEach(T => {
56+
const array = new T([]);
57+
expect(array.reverse()).toEqual([]);
58+
expect(array).toEqual([]);
59+
});
60+
61+
BIGINT_TYPED_ARRAYS.forEach(T => {
62+
const array = new T([]);
63+
expect(array.reverse()).toEqual([]);
64+
expect(array).toEqual([]);
65+
});
66+
});
67+
});

0 commit comments

Comments
 (0)