[njs] Array.isArray() function.

Igor Sysoev igor at sysoev.ru
Thu Aug 11 13:39:50 UTC 2016


details:   http://hg.nginx.org/njs/rev/bd3464a20ad2
branches:  
changeset: 149:bd3464a20ad2
user:      Igor Sysoev <igor at sysoev.ru>
date:      Wed Aug 10 11:57:56 2016 +0300
description:
Array.isArray() function.

diffstat:

 njs/njs_array.c          |  26 ++++++++++++++++++++++++++
 njs/test/njs_unit_test.c |   9 +++++++++
 2 files changed, 35 insertions(+), 0 deletions(-)

diffs (62 lines):

diff -r f23c723cf833 -r bd3464a20ad2 njs/njs_array.c
--- a/njs/njs_array.c	Tue Aug 09 14:10:33 2016 +0300
+++ b/njs/njs_array.c	Wed Aug 10 11:57:56 2016 +0300
@@ -257,6 +257,25 @@ njs_array_constructor(njs_vm_t *vm, njs_
 }
 
 
+static njs_ret_t
+njs_array_is_array(njs_vm_t *vm, njs_value_t *args,
+    nxt_uint_t nargs, njs_index_t unused)
+{
+    const njs_value_t  *value;
+
+    if (nargs > 1 && njs_is_array(&args[1])) {
+        value = &njs_string_true;
+
+    } else {
+        value = &njs_string_false;
+    }
+
+    vm->retval = *value;
+
+    return NXT_OK;
+}
+
+
 static const njs_object_prop_t  njs_array_constructor_properties[] =
 {
     /* Array.name == "Array". */
@@ -279,6 +298,13 @@ static const njs_object_prop_t  njs_arra
         .name = njs_string("prototype"),
         .value = njs_native_getter(njs_object_prototype_create),
     },
+
+    /* Array.isArray(). */
+    {
+        .type = NJS_METHOD,
+        .name = njs_string("isArray"),
+        .value = njs_native_function(njs_array_is_array, 0, 0),
+    },
 };
 
 
diff -r f23c723cf833 -r bd3464a20ad2 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Tue Aug 09 14:10:33 2016 +0300
+++ b/njs/test/njs_unit_test.c	Wed Aug 10 11:57:56 2016 +0300
@@ -2215,6 +2215,15 @@ static njs_unit_test_t  njs_test[] =
 
     /**/
 
+    { nxt_string("Array.isArray()"),
+      nxt_string("false") },
+
+    { nxt_string("Array.isArray(1)"),
+      nxt_string("false") },
+
+    { nxt_string("Array.isArray([])"),
+      nxt_string("true") },
+
     { nxt_string("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