[njs] Array.of() method.

Andrey Zelenkov zelenkov at nginx.com
Thu Mar 30 19:03:57 UTC 2017


details:   http://hg.nginx.org/njs/rev/b7d65eb7d6fa
branches:  
changeset: 324:b7d65eb7d6fa
user:      Andrey Zelenkov <zelenkov at nginx.com>
date:      Thu Mar 30 22:01:17 2017 +0300
description:
Array.of() method.

diffstat:

 njs/njs_array.c          |  34 ++++++++++++++++++++++++++++++++++
 njs/test/njs_unit_test.c |  12 ++++++++++++
 2 files changed, 46 insertions(+), 0 deletions(-)

diffs (73 lines):

diff -r 90743d1bb614 -r b7d65eb7d6fa njs/njs_array.c
--- a/njs/njs_array.c	Wed Mar 29 15:54:37 2017 +0300
+++ b/njs/njs_array.c	Thu Mar 30 22:01:17 2017 +0300
@@ -291,6 +291,32 @@ njs_array_is_array(njs_vm_t *vm, njs_val
 }
 
 
+static njs_ret_t
+njs_array_of(njs_vm_t *vm, njs_value_t *args,
+    nxt_uint_t nargs, njs_index_t unused)
+{
+        uint32_t     length, i;
+        njs_array_t  *array;
+
+        length = nargs > 1 ? nargs - 1 : 0;
+
+        array = njs_array_alloc(vm, length, NJS_ARRAY_SPARE);
+        if (nxt_slow_path(array == NULL)) {
+            return NXT_ERROR;
+        }
+
+        vm->retval.data.u.array = array;
+        vm->retval.type = NJS_ARRAY;
+        vm->retval.data.truth = 1;
+
+        for (i = 0; i < length; i++) {
+            array->start[i] = args[i + 1];
+        }
+
+        return NXT_OK;
+}
+
+
 static const njs_object_prop_t  njs_array_constructor_properties[] =
 {
     /* Array.name == "Array". */
@@ -320,6 +346,14 @@ static const njs_object_prop_t  njs_arra
         .name = njs_string("isArray"),
         .value = njs_native_function(njs_array_is_array, 0, 0),
     },
+
+    /* ES6. */
+    /* Array.of(). */
+    {
+        .type = NJS_METHOD,
+        .name = njs_string("of"),
+        .value = njs_native_function(njs_array_of, 0, 0),
+    },
 };
 
 
diff -r 90743d1bb614 -r b7d65eb7d6fa njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Wed Mar 29 15:54:37 2017 +0300
+++ b/njs/test/njs_unit_test.c	Thu Mar 30 22:01:17 2017 +0300
@@ -2499,6 +2499,18 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("Array.isArray([])"),
       nxt_string("true") },
 
+    { nxt_string("Array.of()"),
+      nxt_string("") },
+
+    { nxt_string("Array.of(1,2,3)"),
+      nxt_string("1,2,3") },
+
+    { nxt_string("Array.of(undefined,1)"),
+      nxt_string(",1") },
+
+    { nxt_string("Array.of(NaN,-1,{})"),
+      nxt_string("NaN,-1,[object Object]") },
+
     { nxt_string("var a = [1,2,3]; a.concat(4, [5, 6, 7], 8)"),
       nxt_string("1,2,3,4,5,6,7,8") },
 


More information about the nginx-devel mailing list