mirror of
https://github.com/RGBCube/serenity
synced 2025-05-14 09:14:58 +00:00
LibJS: Implement Array.prototype.toReversed()
This commit is contained in:
parent
e2a5a27302
commit
e4370b7d82
5 changed files with 85 additions and 0 deletions
|
@ -75,6 +75,7 @@ void ArrayPrototype::initialize(GlobalObject& global_object)
|
|||
define_native_function(vm.names.copyWithin, copy_within, 2, attr);
|
||||
define_native_function(vm.names.group, group, 1, attr);
|
||||
define_native_function(vm.names.groupToMap, group_to_map, 1, attr);
|
||||
define_native_function(vm.names.toReversed, to_reversed, 0, attr);
|
||||
|
||||
// Use define_direct_property here instead of define_native_function so that
|
||||
// Object.is(Array.prototype[Symbol.iterator], Array.prototype.values)
|
||||
|
@ -85,6 +86,7 @@ void ArrayPrototype::initialize(GlobalObject& global_object)
|
|||
// 23.1.3.35 Array.prototype [ @@unscopables ], https://tc39.es/ecma262/#sec-array.prototype-@@unscopables
|
||||
// With find from last proposal, https://tc39.es/proposal-array-find-from-last/#sec-array.prototype-@@unscopables
|
||||
// With array grouping proposal, https://tc39.es/proposal-array-grouping/#sec-array.prototype-@@unscopables
|
||||
// With change array by copy proposal, https://tc39.es/proposal-change-array-by-copy/#sec-array.prototype-@@unscopables
|
||||
auto* unscopable_list = Object::create(global_object, nullptr);
|
||||
MUST(unscopable_list->create_data_property_or_throw(vm.names.at, Value(true)));
|
||||
MUST(unscopable_list->create_data_property_or_throw(vm.names.copyWithin, Value(true)));
|
||||
|
@ -100,6 +102,7 @@ void ArrayPrototype::initialize(GlobalObject& global_object)
|
|||
MUST(unscopable_list->create_data_property_or_throw(vm.names.groupToMap, Value(true)));
|
||||
MUST(unscopable_list->create_data_property_or_throw(vm.names.includes, Value(true)));
|
||||
MUST(unscopable_list->create_data_property_or_throw(vm.names.keys, Value(true)));
|
||||
MUST(unscopable_list->create_data_property_or_throw(vm.names.toReversed, Value(true)));
|
||||
MUST(unscopable_list->create_data_property_or_throw(vm.names.values, Value(true)));
|
||||
|
||||
define_direct_property(*vm.well_known_symbol_unscopables(), unscopable_list, Attribute::Configurable);
|
||||
|
@ -1781,4 +1784,38 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::group_to_map)
|
|||
return map;
|
||||
}
|
||||
|
||||
// 1.1.1.4 Array.prototype.toReversed ( ), https://tc39.es/proposal-change-array-by-copy/#sec-array.prototype.toReversed
|
||||
JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::to_reversed)
|
||||
{
|
||||
// 1. Let O be ? ToObject(this value).
|
||||
auto* object = TRY(vm.this_value(global_object).to_object(global_object));
|
||||
|
||||
// 2. Let len be ? LengthOfArrayLike(O).
|
||||
auto length = TRY(length_of_array_like(global_object, *object));
|
||||
|
||||
// 3. Let A be ? ArrayCreate(𝔽(len)).
|
||||
auto* array = TRY(Array::create(global_object, length));
|
||||
|
||||
// 4. Let k be 0.
|
||||
// 5. Repeat, while k < len,
|
||||
for (size_t k = 0; k < length; ++k) {
|
||||
// a. Let from be ! ToString(𝔽(len - 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 = TRY(object->get(from));
|
||||
|
||||
// d. Perform ! CreateDataPropertyOrThrow(A, Pk, fromValue).
|
||||
MUST(array->create_data_property_or_throw(property_key, from_value));
|
||||
|
||||
// e. Set k to k + 1.
|
||||
}
|
||||
|
||||
// 6. Return A.
|
||||
return array;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -56,6 +56,7 @@ private:
|
|||
JS_DECLARE_NATIVE_FUNCTION(copy_within);
|
||||
JS_DECLARE_NATIVE_FUNCTION(group);
|
||||
JS_DECLARE_NATIVE_FUNCTION(group_to_map);
|
||||
JS_DECLARE_NATIVE_FUNCTION(to_reversed);
|
||||
};
|
||||
|
||||
ThrowCompletionOr<void> array_merge_sort(GlobalObject&, FunctionObject* compare_func, MarkedVector<Value>& arr_to_sort);
|
||||
|
|
|
@ -485,6 +485,7 @@ namespace JS {
|
|||
P(toPlainTime) \
|
||||
P(toPlainYearMonth) \
|
||||
P(toPrecision) \
|
||||
P(toReversed) \
|
||||
P(toString) \
|
||||
P(total) \
|
||||
P(toTemporalInstant) \
|
||||
|
|
|
@ -252,6 +252,7 @@ describe("ability to work with generic non-array objects", () => {
|
|||
});
|
||||
|
||||
test("reverse", () => {
|
||||
const o = { length: 5, 0: "foo", 1: "bar", 3: "baz" };
|
||||
expect(Array.prototype.reverse.call(o)).toEqual({
|
||||
length: 5,
|
||||
4: "foo",
|
||||
|
@ -341,4 +342,10 @@ describe("ability to work with generic non-array objects", () => {
|
|||
const trueResult = result.get(trueObject);
|
||||
expect(trueResult).toEqual(["bar", "baz"]);
|
||||
});
|
||||
|
||||
test("toReversed", () => {
|
||||
const result = Array.prototype.toReversed.call(o);
|
||||
expect(result).toEqual([undefined, "baz", undefined, "bar", "foo"]);
|
||||
expect(result).not.toBe(o);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
describe("normal behavior", () => {
|
||||
test("length is 0", () => {
|
||||
expect(Array.prototype.toReversed).toHaveLength(0);
|
||||
});
|
||||
|
||||
test("basic functionality", () => {
|
||||
const a = [1, 2, 3, 4, 5];
|
||||
const b = a.toReversed();
|
||||
expect(a).not.toBe(b);
|
||||
expect(a).toEqual([1, 2, 3, 4, 5]);
|
||||
expect(b).toEqual([5, 4, 3, 2, 1]);
|
||||
});
|
||||
|
||||
test("is unscopable", () => {
|
||||
expect(Array.prototype[Symbol.unscopables].toReversed).toBeTrue();
|
||||
const array = [];
|
||||
with (array) {
|
||||
expect(() => {
|
||||
toReversed;
|
||||
}).toThrowWithMessage(ReferenceError, "'toReversed' is not defined");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("null or undefined this value", () => {
|
||||
expect(() => {
|
||||
Array.prototype.toReversed.call();
|
||||
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
|
||||
|
||||
expect(() => {
|
||||
Array.prototype.toReversed.call(undefined);
|
||||
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
|
||||
|
||||
expect(() => {
|
||||
Array.prototype.toReversed.call(null);
|
||||
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue