summaryrefslogtreecommitdiff
path: root/Userland/Libraries
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2022-06-13 07:59:19 +0100
committerLinus Groh <mail@linusgroh.de>2022-06-13 20:26:54 +0100
commitce17c868c0a75cb765f0db81a60464e01f37aeb9 (patch)
tree25241a1785c70abac68e1dbcd279d74e1721642f /Userland/Libraries
parente4370b7d82ace4bd8d3a954c3d3e68c3eebb2c5f (diff)
downloadserenity-ce17c868c0a75cb765f0db81a60464e01f37aeb9.zip
LibJS: Implement Array.prototype.toSorted()
Diffstat (limited to 'Userland/Libraries')
-rw-r--r--Userland/Libraries/LibJS/Runtime/ArrayPrototype.cpp42
-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.js6
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Array/Array.prototype.toSorted.js53
5 files changed, 103 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/ArrayPrototype.cpp b/Userland/Libraries/LibJS/Runtime/ArrayPrototype.cpp
index 0ee1c585d2..9e75726970 100644
--- a/Userland/Libraries/LibJS/Runtime/ArrayPrototype.cpp
+++ b/Userland/Libraries/LibJS/Runtime/ArrayPrototype.cpp
@@ -76,6 +76,7 @@ void ArrayPrototype::initialize(GlobalObject& global_object)
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);
+ define_native_function(vm.names.toSorted, to_sorted, 1, attr);
// Use define_direct_property here instead of define_native_function so that
// Object.is(Array.prototype[Symbol.iterator], Array.prototype.values)
@@ -103,6 +104,7 @@ void ArrayPrototype::initialize(GlobalObject& global_object)
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.toSorted, 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);
@@ -1818,4 +1820,44 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::to_reversed)
return array;
}
+// 1.1.1.5 Array.prototype.toSorted ( comparefn ), https://tc39.es/proposal-change-array-by-copy/#sec-array.prototype.toSorted
+JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::to_sorted)
+{
+ auto comparefn = vm.argument(0);
+
+ // 1. If comparefn is not undefined and IsCallable(comparefn) is false, throw a TypeError exception.
+ if (!comparefn.is_undefined() && !comparefn.is_function())
+ return vm.throw_completion<TypeError>(global_object, ErrorType::NotAFunction, comparefn);
+
+ // 2. Let O be ? ToObject(this value).
+ auto* object = TRY(vm.this_value(global_object).to_object(global_object));
+
+ // 3. Let len be ? LengthOfArrayLike(O).
+ auto length = TRY(length_of_array_like(global_object, *object));
+
+ // 4. Let A be ? ArrayCreate(𝔽(len)).
+ auto* array = TRY(Array::create(global_object, length));
+
+ // 5. Let SortCompare be a new Abstract Closure with parameters (x, y) that captures comparefn and performs the following steps when called:
+ Function<ThrowCompletionOr<double>(Value, Value)> sort_compare = [&](auto x, auto y) -> ThrowCompletionOr<double> {
+ // a. Return ? CompareArrayElements(x, y, comparefn).
+ return TRY(compare_array_elements(global_object, x, y, comparefn.is_undefined() ? nullptr : &comparefn.as_function()));
+ };
+
+ // 6. Let sortedList be ? SortIndexedProperties(obj, len, SortCompare, false).
+ auto sorted_list = TRY(sort_indexed_properties(global_object, *object, length, sort_compare, false));
+
+ // 7. Let j be 0.
+ // 8. Repeat, while j < len,
+ for (size_t j = 0; j < length; ++j) {
+ // a. Perform ! CreateDataPropertyOrThrow(A, ! ToString(𝔽(j)), sortedList[j]).
+ MUST(array->create_data_property_or_throw(j, sorted_list[j]));
+
+ // b. Set j to j + 1.
+ }
+
+ // 9. Return A.
+ return array;
+}
+
}
diff --git a/Userland/Libraries/LibJS/Runtime/ArrayPrototype.h b/Userland/Libraries/LibJS/Runtime/ArrayPrototype.h
index 06c7fb5f63..1e9df74cde 100644
--- a/Userland/Libraries/LibJS/Runtime/ArrayPrototype.h
+++ b/Userland/Libraries/LibJS/Runtime/ArrayPrototype.h
@@ -57,6 +57,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(group);
JS_DECLARE_NATIVE_FUNCTION(group_to_map);
JS_DECLARE_NATIVE_FUNCTION(to_reversed);
+ JS_DECLARE_NATIVE_FUNCTION(to_sorted);
};
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 2c3f324879..9d2121236d 100644
--- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h
+++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h
@@ -486,6 +486,7 @@ namespace JS {
P(toPlainYearMonth) \
P(toPrecision) \
P(toReversed) \
+ P(toSorted) \
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 3bf0324649..f6f421fe3c 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
@@ -348,4 +348,10 @@ describe("ability to work with generic non-array objects", () => {
expect(result).toEqual([undefined, "baz", undefined, "bar", "foo"]);
expect(result).not.toBe(o);
});
+
+ test("toSorted", () => {
+ const result = Array.prototype.toSorted.call(o);
+ expect(result).toEqual(["bar", "baz", "foo", undefined, undefined]);
+ expect(result).not.toBe(o);
+ });
});
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Array/Array.prototype.toSorted.js b/Userland/Libraries/LibJS/Tests/builtins/Array/Array.prototype.toSorted.js
new file mode 100644
index 0000000000..d83b67ac13
--- /dev/null
+++ b/Userland/Libraries/LibJS/Tests/builtins/Array/Array.prototype.toSorted.js
@@ -0,0 +1,53 @@
+describe("normal behavior", () => {
+ test("length is 1", () => {
+ expect(Array.prototype.toSorted).toHaveLength(1);
+ });
+
+ test("basic functionality", () => {
+ const a = [2, 4, 1, 3, 5];
+ const b = a.toSorted();
+ expect(a).not.toBe(b);
+ expect(a).toEqual([2, 4, 1, 3, 5]);
+ expect(b).toEqual([1, 2, 3, 4, 5]);
+ });
+
+ test("custom compare function", () => {
+ const a = [2, 4, 1, 3, 5];
+ const b = a.toSorted(() => 0);
+ expect(a).not.toBe(b);
+ expect(a).toEqual([2, 4, 1, 3, 5]);
+ expect(b).toEqual([2, 4, 1, 3, 5]);
+ });
+
+ test("is unscopable", () => {
+ expect(Array.prototype[Symbol.unscopables].toSorted).toBeTrue();
+ const array = [];
+ with (array) {
+ expect(() => {
+ toSorted;
+ }).toThrowWithMessage(ReferenceError, "'toSorted' is not defined");
+ }
+ });
+});
+
+describe("errors", () => {
+ test("null or undefined this value", () => {
+ expect(() => {
+ Array.prototype.toSorted.call();
+ }).toThrowWithMessage(TypeError, "ToObject on null or undefined");
+
+ expect(() => {
+ Array.prototype.toSorted.call(undefined);
+ }).toThrowWithMessage(TypeError, "ToObject on null or undefined");
+
+ expect(() => {
+ Array.prototype.toSorted.call(null);
+ }).toThrowWithMessage(TypeError, "ToObject on null or undefined");
+ });
+
+ test("invalid compare function", () => {
+ expect(() => {
+ [].toSorted("foo");
+ }).toThrowWithMessage(TypeError, "foo is not a function");
+ });
+});