summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2022-06-13 07:57:38 +0100
committerLinus Groh <mail@linusgroh.de>2022-06-13 20:26:54 +0100
commite4370b7d82ace4bd8d3a954c3d3e68c3eebb2c5f (patch)
tree1d64c3032170fb6815fc7e55922a93a1fb3736b4 /Userland/Libraries/LibJS
parente2a5a2730275c361e0a287eec3c2994ec7699970 (diff)
downloadserenity-e4370b7d82ace4bd8d3a954c3d3e68c3eebb2c5f.zip
LibJS: Implement Array.prototype.toReversed()
Diffstat (limited to 'Userland/Libraries/LibJS')
-rw-r--r--Userland/Libraries/LibJS/Runtime/ArrayPrototype.cpp37
-rw-r--r--Userland/Libraries/LibJS/Runtime/ArrayPrototype.h1
-rw-r--r--Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h1
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Array/Array.prototype-generic-functions.js7
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Array/Array.prototype.toReversed.js39
5 files changed, 85 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/ArrayPrototype.cpp b/Userland/Libraries/LibJS/Runtime/ArrayPrototype.cpp
index f067239c02..0ee1c585d2 100644
--- a/Userland/Libraries/LibJS/Runtime/ArrayPrototype.cpp
+++ b/Userland/Libraries/LibJS/Runtime/ArrayPrototype.cpp
@@ -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;
+}
+
}
diff --git a/Userland/Libraries/LibJS/Runtime/ArrayPrototype.h b/Userland/Libraries/LibJS/Runtime/ArrayPrototype.h
index 51cac35d5a..06c7fb5f63 100644
--- a/Userland/Libraries/LibJS/Runtime/ArrayPrototype.h
+++ b/Userland/Libraries/LibJS/Runtime/ArrayPrototype.h
@@ -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);
diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h
index c2e8e69764..2c3f324879 100644
--- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h
+++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h
@@ -485,6 +485,7 @@ namespace JS {
P(toPlainTime) \
P(toPlainYearMonth) \
P(toPrecision) \
+ P(toReversed) \
P(toString) \
P(total) \
P(toTemporalInstant) \
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Array/Array.prototype-generic-functions.js b/Userland/Libraries/LibJS/Tests/builtins/Array/Array.prototype-generic-functions.js
index 59c433e57a..3bf0324649 100644
--- a/Userland/Libraries/LibJS/Tests/builtins/Array/Array.prototype-generic-functions.js
+++ b/Userland/Libraries/LibJS/Tests/builtins/Array/Array.prototype-generic-functions.js
@@ -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);
+ });
});
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Array/Array.prototype.toReversed.js b/Userland/Libraries/LibJS/Tests/builtins/Array/Array.prototype.toReversed.js
new file mode 100644
index 0000000000..b19dcd28b1
--- /dev/null
+++ b/Userland/Libraries/LibJS/Tests/builtins/Array/Array.prototype.toReversed.js
@@ -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");
+ });
+});