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

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


details:   https://hg.nginx.org/njs/rev/9213a609cb17
branches:  
changeset: 2141:9213a609cb17
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Fri May 26 21:05:15 2023 -0700
description:
Added %TypedArray%.prototype.toReversed().

diffstat:

 src/njs_typed_array.c    |  27 +++++++++++++++++++++++----
 src/test/njs_unit_test.c |   5 +++++
 2 files changed, 28 insertions(+), 4 deletions(-)

diffs (73 lines):

diff -r fa60c896acaa -r 9213a609cb17 src/njs_typed_array.c
--- a/src/njs_typed_array.c	Fri May 26 20:58:22 2023 -0700
+++ b/src/njs_typed_array.c	Fri May 26 21:05:15 2023 -0700
@@ -1609,15 +1609,15 @@ njs_typed_array_prototype_reduce(njs_vm_
 
 static njs_int_t
 njs_typed_array_prototype_reverse(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_reversed, njs_value_t *retval)
 {
     double              *f64;
     uint8_t             *u8;
     int64_t             i, length;
     uint16_t            *u16;
     uint32_t            *u32;
-    njs_value_t         *this;
-    njs_typed_array_t   *array;
+    njs_value_t         *this, arguments[1];
+    njs_typed_array_t   *array, *self;
     njs_array_buffer_t  *buffer;
 
     this = njs_argument(args, 0);
@@ -1627,8 +1627,24 @@ njs_typed_array_prototype_reverse(njs_vm
     }
 
     array = njs_typed_array(this);
+    if (njs_slow_path(njs_is_detached_buffer(array->buffer))) {
+        njs_type_error(vm, "detached buffer");
+        return NJS_ERROR;
+    }
+
+    if (to_reversed) {
+        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);
+    }
+
     length = njs_typed_array_length(array);
-
     buffer = njs_typed_array_writable(vm, array);
     if (njs_slow_path(buffer == NULL)) {
         return NJS_ERROR;
@@ -2296,6 +2312,9 @@ static const njs_object_prop_t  njs_type
 
     NJS_DECLARE_PROP_NATIVE("subarray", njs_typed_array_prototype_slice, 2, 0),
 
+    NJS_DECLARE_PROP_NATIVE("toReversed", njs_typed_array_prototype_reverse, 0,
+                            1),
+
     NJS_DECLARE_PROP_NATIVE("toSorted", njs_typed_array_prototype_sort, 1, 1),
 
     NJS_DECLARE_PROP_NATIVE("toString", njs_array_prototype_to_string, 0, 0),
diff -r fa60c896acaa -r 9213a609cb17 src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Fri May 26 20:58:22 2023 -0700
+++ b/src/test/njs_unit_test.c	Fri May 26 21:05:15 2023 -0700
@@ -6708,6 +6708,11 @@ static njs_unit_test_t  njs_test[] =
               ".every(v=>{ return (new v([1,2,3,4])).reverse().join('|') == '4|3|2|1'})"),
       njs_str("true") },
 
+    { njs_str(NJS_TYPED_ARRAY_LIST
+              ".every(v=>{var a = new v([3,2,1]);"
+              "           return [a.toReversed(), a].toString() === '1,2,3,3,2,1'})"),
+      njs_str("true") },
+
     { njs_str("Uint8Array.prototype.sort.call(1)"),
       njs_str("TypeError: this is not a typed array") },
 


More information about the nginx-devel mailing list