Skip to content

Commit

Permalink
LibJS: Add %TypedArray%.prototype.toReversed
Browse files Browse the repository at this point in the history
  • Loading branch information
beesaferoot committed Jun 30, 2022
1 parent e1ee33b commit bbde673
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ void TypedArrayPrototype::initialize(GlobalObject& object)
define_native_function(vm.names.map, map, 1, attr);
define_native_function(vm.names.toLocaleString, to_locale_string, 0, attr);
define_native_function(vm.names.toSorted, to_sorted, 1, attr);
define_native_function(vm.names.toReversed, to_reversed, 0, attr);

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

Expand Down Expand Up @@ -1512,6 +1513,39 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::to_locale_string)
return js_string(vm, builder.to_string());
}

// 1.2.2.1.3 %TypedArray%.prototype.toReversed
JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::to_reversed)
{
// 1. Let O be the this value.
// 2. Perform ? ValidateTypedArray(O).
auto* typed_array = TRY(validate_typed_array_from_this(global_object));

// 3. Let length be O.[[ArrayLength]].
auto length = typed_array->array_length();

MarkedVector<Value> arguments(vm.heap());
arguments.empend(length);
// 4. Let A be ? TypedArrayCreateSameType(O, « 𝔽(length) »).
auto* return_array = TRY(typed_array_create_same_type(global_object, *typed_array, move(arguments)));

// 5. Let k be 0.
// 6. Repeat, while k < length
for(size_t k = 0; k < length; k++){
// a. Let from be ! ToString(𝔽(length - k - 1)).
// Let Pk be ! ToString(𝔽(k)).
auto from = length - k - 1;

// c. Let fromValue be ! Get(O, from).
auto from_value = MUST(typed_array->get(from));

// d. Perform ! Set(A, Pk, kValue, true).
MUST(return_array->set(k, from_value, Object::ShouldThrowExceptions::Yes));
// e. Set k to k + 1.
}

return return_array;
}

// 1.2.2.1.4 %TypedArray%.prototype.toSorted ( comparefn ) https://tc39.es/proposal-change-array-by-copy/#sec-%typedarray%.prototype.toSorted
JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::to_sorted)
{
Expand Down
1 change: 1 addition & 0 deletions Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class TypedArrayPrototype final : public Object {
JS_DECLARE_NATIVE_FUNCTION(map);
JS_DECLARE_NATIVE_FUNCTION(to_locale_string);
JS_DECLARE_NATIVE_FUNCTION(to_sorted);
JS_DECLARE_NATIVE_FUNCTION(to_reversed);
};

}
Original file line number Diff line number Diff line change
@@ -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.toReverse).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([]);
});
});
});

0 comments on commit bbde673

Please sign in to comment.