[njs] Added %TypedArray%.prototype.toSorted().

Dmitry Volyntsev xeioex at nginx.com
Sat May 27 17:06:14 UTC 2023


details:   https://hg.nginx.org/njs/rev/fa60c896acaa
branches:  
changeset: 2140:fa60c896acaa
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Fri May 26 20:58:22 2023 -0700
description:
Added %TypedArray%.prototype.toSorted().

diffstat:

 src/njs_typed_array.c    |  20 +++++++++++++++++---
 src/test/njs_unit_test.c |  11 +++++++++++
 2 files changed, 28 insertions(+), 3 deletions(-)

diffs (69 lines):

diff -r 84eadc155d46 -r fa60c896acaa src/njs_typed_array.c
--- a/src/njs_typed_array.c	Fri May 26 20:58:19 2023 -0700
+++ b/src/njs_typed_array.c	Fri May 26 20:58:22 2023 -0700
@@ -1897,13 +1897,13 @@ exception:
 
 static njs_int_t
 njs_typed_array_prototype_sort(njs_vm_t *vm, njs_value_t *args,
-    njs_uint_t nargs, njs_index_t unused, njs_value_t *retval)
+    njs_uint_t nargs, njs_index_t to_sorted, njs_value_t *retval)
 {
     u_char                      *base, *orig;
     int64_t                     length;
     uint32_t                    element_size;
-    njs_value_t                 *this, *comparefn;
-    njs_typed_array_t           *array;
+    njs_value_t                 *this, *comparefn, arguments[1];
+    njs_typed_array_t           *array, *self;
     njs_array_buffer_t          *buffer;
     njs_typed_array_cmp_t       cmp;
     njs_typed_array_sort_ctx_t  ctx;
@@ -1920,6 +1920,18 @@ njs_typed_array_prototype_sort(njs_vm_t 
         return NJS_ERROR;
     }
 
+    if (to_sorted) {
+        self = array;
+        njs_set_number(&arguments[0], njs_typed_array_length(self));
+        array = njs_typed_array_alloc(vm, arguments, 1, 0, self->type);
+        if (njs_slow_path(array == NULL)) {
+            return NJS_ERROR;
+        }
+
+        memcpy(&array->buffer->u.u8[0], &self->buffer->u.u8[0],
+               self->byte_length);
+    }
+
     ctx.vm = vm;
     ctx.buffer = array->buffer;
     ctx.exception = 0;
@@ -2284,6 +2296,8 @@ static const njs_object_prop_t  njs_type
 
     NJS_DECLARE_PROP_NATIVE("subarray", njs_typed_array_prototype_slice, 2, 0),
 
+    NJS_DECLARE_PROP_NATIVE("toSorted", njs_typed_array_prototype_sort, 1, 1),
+
     NJS_DECLARE_PROP_NATIVE("toString", njs_array_prototype_to_string, 0, 0),
 
     NJS_DECLARE_PROP_NATIVE("values", njs_typed_array_prototype_iterator_obj,
diff -r 84eadc155d46 -r fa60c896acaa src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Fri May 26 20:58:19 2023 -0700
+++ b/src/test/njs_unit_test.c	Fri May 26 20:58:22 2023 -0700
@@ -6751,6 +6751,17 @@ static njs_unit_test_t  njs_test[] =
     { njs_str("(new Float64Array([255,255,NaN,3,NaN,Infinity,3,-Infinity,0,-0,2,1,-5])).slice(2).sort()"),
       njs_str("-Infinity,-5,0,0,1,2,3,3,Infinity,NaN,NaN") },
 
+    { njs_str(NJS_TYPED_ARRAY_LIST
+              ".every(v=>{var a = new v([3,2,1]);"
+              "           return [a.toSorted(),a].toString() === '1,2,3,3,2,1'})"),
+      njs_str("true") },
+
+    { njs_str(NJS_TYPED_ARRAY_LIST
+              ".every(v=>{var a = (new v([3,2,1]));"
+              "           a.constructor = (v == Uint8Array) ? Uint32Array : Uint8Array;"
+              "           return Object.getPrototypeOf(a.toSorted()) === v.prototype})"),
+      njs_str("true") },
+
     { njs_str("(new DataView(new ArrayBuffer(3)))"),
       njs_str("[object DataView]") },
 


More information about the nginx-devel mailing list