Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ void TypedArrayPrototype::initialize(GlobalObject& object)
define_native_function(vm.names.filter, filter, 1, attr);
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.toReversed, to_reversed, 0, attr);
define_native_function(vm.names.toSorted, to_sorted, 1, attr);

define_native_accessor(*vm.well_known_symbol_to_string_tag(), to_string_tag_getter, nullptr, Attribute::Configurable);
Expand Down Expand Up @@ -1512,7 +1513,44 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::to_locale_string)
return js_string(vm, builder.to_string());
}

// 1.2.2.1.4 %TypedArray%.prototype.toSorted ( comparefn ) https://tc39.es/proposal-change-array-by-copy/#sec-%typedarray%.prototype.toSorted
// 1.2.2.1.3 %TypedArray%.prototype.toReversed ( ), https://tc39.es/proposal-change-array-by-copy/#sec-%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();

// 4. Let A be ? TypedArrayCreateSameType(O, « 𝔽(length) »).
MarkedVector<Value> arguments(vm.heap());
arguments.empend(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)).
auto from = PropertyKey { length - k - 1 };

// b. Let Pk be ! ToString(𝔽(k)).
auto property_key = PropertyKey { k };

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

// d. Perform ! Set(A, Pk, fromValue, true).
MUST(return_array->set(property_key, from_value, Object::ShouldThrowExceptions::Yes));

// e. Set k to k + 1.
}

// 7. Return A.
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)
{
auto comparefn = vm.argument(0);
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 @@ -52,6 +52,7 @@ class TypedArrayPrototype final : public Object {
JS_DECLARE_NATIVE_FUNCTION(filter);
JS_DECLARE_NATIVE_FUNCTION(map);
JS_DECLARE_NATIVE_FUNCTION(to_locale_string);
JS_DECLARE_NATIVE_FUNCTION(to_reversed);
JS_DECLARE_NATIVE_FUNCTION(to_sorted);
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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.toReversed).toHaveLength(0);
});

BIGINT_TYPED_ARRAYS.forEach(T => {
expect(T.prototype.toReversed).toHaveLength(0);
});
});

describe("basic functionality", () => {
test("Odd length array", () => {
TYPED_ARRAYS.forEach(T => {
const array = new T([1, 2, 3]);

expect(array.toReversed()).toEqual([3, 2, 1]);
expect(array).toEqual([1, 2, 3]);
});

BIGINT_TYPED_ARRAYS.forEach(T => {
const array = new T([1n, 2n, 3n]);
expect(array.toReversed()).toEqual([3n, 2n, 1n]);
expect(array).toEqual([1n, 2n, 3n]);
});
});

test("Even length array", () => {
TYPED_ARRAYS.forEach(T => {
const array = new T([1, 2]);
expect(array.toReversed()).toEqual([2, 1]);
expect(array).toEqual([1, 2]);
});

BIGINT_TYPED_ARRAYS.forEach(T => {
const array = new T([1n, 2n]);
expect(array.toReversed()).toEqual([2n, 1n]);
expect(array).toEqual([1n, 2n]);
});
});

test("Empty array", () => {
TYPED_ARRAYS.forEach(T => {
const array = new T([]);
expect(array.toReversed()).toEqual([]);
expect(array).toEqual([]);
});

BIGINT_TYPED_ARRAYS.forEach(T => {
const array = new T([]);
expect(array.toReversed()).toEqual([]);
expect(array).toEqual([]);
});
});
});