From 52aa777d4949f8cc88bd352f10bc7b185cfce093 Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Fri, 9 Jul 2021 17:41:04 +0300 Subject: [PATCH] LibJS: Add %TypedArray%.prototype.sort --- .../LibJS/Runtime/TypedArrayPrototype.cpp | 146 ++++++++++++++++++ .../LibJS/Runtime/TypedArrayPrototype.h | 1 + .../TypedArray/TypedArray.prototype.sort.js | 53 +++++++ 3 files changed, 200 insertions(+) create mode 100644 Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.sort.js diff --git a/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp b/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp index 049042e67d..a9c12c3540 100644 --- a/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp @@ -1,6 +1,7 @@ /* * Copyright (c) 2020-2021, Linus Groh * Copyright (c) 2021, Luke Wilde + * Copyright (c) 2021, Idan Horowitz * * SPDX-License-Identifier: BSD-2-Clause */ @@ -46,6 +47,7 @@ void TypedArrayPrototype::initialize(GlobalObject& object) define_native_function(vm.names.entries, entries, 0, attr); define_native_function(vm.names.set, set, 1, attr); define_native_function(vm.names.slice, slice, 2, attr); + define_native_function(vm.names.sort, sort, 1, attr); define_native_function(vm.names.subarray, subarray, 2, attr); define_native_function(vm.names.reverse, reverse, 0, attr); define_native_function(vm.names.copyWithin, copy_within, 2, attr); @@ -908,6 +910,150 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::slice) return new_array; } +static void typed_array_merge_sort(GlobalObject& global_object, FunctionObject* compare_function, ArrayBuffer& buffer, MarkedValueList& arr_to_sort) +{ + auto& vm = global_object.vm(); + if (arr_to_sort.size() <= 1) + return; + + MarkedValueList left(vm.heap()); + MarkedValueList right(vm.heap()); + + left.ensure_capacity(arr_to_sort.size() / 2); + right.ensure_capacity(arr_to_sort.size() / 2 + (arr_to_sort.size() & 1)); + + for (size_t i = 0; i < arr_to_sort.size(); ++i) { + if (i < arr_to_sort.size() / 2) { + left.append(arr_to_sort[i]); + } else { + right.append(arr_to_sort[i]); + } + } + + typed_array_merge_sort(global_object, compare_function, buffer, left); + if (vm.exception()) + return; + typed_array_merge_sort(global_object, compare_function, buffer, right); + if (vm.exception()) + return; + + arr_to_sort.clear(); + + size_t left_index = 0, right_index = 0; + + while (left_index < left.size() && right_index < right.size()) { + auto x = left[left_index]; + auto y = right[right_index]; + + bool number_comparison = x.is_number(); + double comparison_result; + + if (compare_function) { + auto result = vm.call(*compare_function, js_undefined(), x, y); + if (vm.exception()) + return; + + auto value = result.to_number(global_object); + if (vm.exception()) + return; + + if (buffer.is_detached()) { + vm.throw_exception(global_object, ErrorType::DetachedArrayBuffer); + return; + } + + if (value.is_nan()) + comparison_result = 0; + else + comparison_result = value.as_double(); + } else if (x.is_nan() && y.is_nan()) { + comparison_result = 0; + } else if (x.is_nan()) { + comparison_result = 1; + } else if (y.is_nan()) { + comparison_result = -1; + } else if (number_comparison ? (x.as_double() < y.as_double()) : (x.as_bigint().big_integer() < y.as_bigint().big_integer())) { + comparison_result = -1; + } else if (number_comparison ? (x.as_double() > y.as_double()) : (x.as_bigint().big_integer() > y.as_bigint().big_integer())) { + comparison_result = 1; + } else if (x.is_negative_zero() && y.is_positive_zero()) { + comparison_result = -1; + } else if (x.is_positive_zero() && y.is_negative_zero()) { + comparison_result = 1; + } else { + comparison_result = 0; + } + + if (comparison_result <= 0) { + arr_to_sort.append(left[left_index]); + left_index++; + } else { + arr_to_sort.append(right[right_index]); + right_index++; + } + } + + while (left_index < left.size()) { + arr_to_sort.append(left[left_index]); + left_index++; + } + + while (right_index < right.size()) { + arr_to_sort.append(right[right_index]); + right_index++; + } +} + +// 23.2.3.26 %TypedArray%.prototype.sort ( comparefn ), https://tc39.es/ecma262/#sec-%typedarray%.prototype.sort +JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::sort) +{ + auto compare_fn = vm.argument(0); + if (!compare_fn.is_undefined() && !compare_fn.is_function()) { + vm.throw_exception(global_object, ErrorType::NotAFunction, compare_fn.to_string_without_side_effects()); + return {}; + } + + auto* typed_array = validate_typed_array(global_object); + if (!typed_array) + return {}; + + auto length = typed_array->array_length(); + + MarkedValueList items(vm.heap()); + for (u32 k = 0; k < length; ++k) { + auto k_present = typed_array->has_property(k); + if (vm.exception()) + return {}; + + if (k_present) { + auto k_value = typed_array->get(k); + if (vm.exception()) + return {}; + + items.append(k_value); + } + } + + typed_array_merge_sort(global_object, compare_fn.is_undefined() ? nullptr : &compare_fn.as_function(), *typed_array->viewed_array_buffer(), items); + if (vm.exception()) + return {}; + + u32 j; + for (j = 0; j < items.size(); ++j) { + typed_array->set(j, items[j], true); + if (vm.exception()) + return {}; + } + + for (; j < length; ++j) { + typed_array->delete_property_or_throw(j); + if (vm.exception()) + return {}; + } + + return typed_array; +} + // 23.2.3.27 %TypedArray%.prototype.subarray ( begin, end ), https://tc39.es/ecma262/#sec-%typedarray%.prototype.subarray JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::subarray) { diff --git a/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.h b/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.h index 39c2b51bdd..088551e712 100644 --- a/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.h @@ -43,6 +43,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(entries); JS_DECLARE_NATIVE_FUNCTION(set); JS_DECLARE_NATIVE_FUNCTION(slice); + JS_DECLARE_NATIVE_FUNCTION(sort); JS_DECLARE_NATIVE_FUNCTION(subarray); JS_DECLARE_NATIVE_FUNCTION(reverse); JS_DECLARE_NATIVE_FUNCTION(copy_within); diff --git a/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.sort.js b/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.sort.js new file mode 100644 index 0000000000..02f2642047 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.sort.js @@ -0,0 +1,53 @@ +const TYPED_ARRAYS = [ + Uint8Array, + Uint8ClampedArray, + Uint16Array, + Uint32Array, + Int8Array, + Int16Array, + Int32Array, + Float32Array, + Float64Array, +]; + +const BIGINT_TYPED_ARRAYS = [BigUint64Array, BigInt64Array]; + +test("basic functionality", () => { + TYPED_ARRAYS.forEach(T => { + expect(T.prototype.sort).toHaveLength(1); + + const typedArray = new T(3); + typedArray[0] = 3; + typedArray[1] = 1; + typedArray[2] = 2; + + expect(typedArray.sort()).toBe(typedArray); + expect(typedArray[0]).toBe(1); + expect(typedArray[1]).toBe(2); + expect(typedArray[2]).toBe(3); + + expect(typedArray.sort(() => 1)).toBe(typedArray); + expect(typedArray[0]).toBe(3); + expect(typedArray[1]).toBe(2); + expect(typedArray[2]).toBe(1); + }); + + BIGINT_TYPED_ARRAYS.forEach(T => { + expect(T.prototype.sort).toHaveLength(1); + + const typedArray = new T(3); + typedArray[0] = 3n; + typedArray[1] = 1n; + typedArray[2] = 2n; + + expect(typedArray.sort()).toBe(typedArray); + expect(typedArray[0]).toBe(1n); + expect(typedArray[1]).toBe(2n); + expect(typedArray[2]).toBe(3n); + + expect(typedArray.sort(() => 1)).toBe(typedArray); + expect(typedArray[0]).toBe(3n); + expect(typedArray[1]).toBe(2n); + expect(typedArray[2]).toBe(1n); + }); +});