changeset 149:bd3464a20ad2

Array.isArray() function.
author Igor Sysoev <igor@sysoev.ru>
date Wed, 10 Aug 2016 11:57:56 +0300
parents f23c723cf833
children 050db82b0d46
files njs/njs_array.c njs/test/njs_unit_test.c
diffstat 2 files changed, 35 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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 @@
 }
 
 
+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 @@
         .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),
+    },
 };
 
 
--- 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 @@
 
     /**/
 
+    { 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") },