]> git.kaiwu.me - njs.git/commitdiff
Array.isArray() function.
authorIgor Sysoev <igor@sysoev.ru>
Wed, 10 Aug 2016 08:57:56 +0000 (11:57 +0300)
committerIgor Sysoev <igor@sysoev.ru>
Wed, 10 Aug 2016 08:57:56 +0000 (11:57 +0300)
njs/njs_array.c
njs/test/njs_unit_test.c

index 5cc72d37bb548d6068dcec86048086fd509a2005..34f6f771e7e1caa2888b595b3a7627500c8f048f 100644 (file)
@@ -257,6 +257,25 @@ njs_array_constructor(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
 }
 
 
+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_array_constructor_properties[] =
         .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),
+    },
 };
 
 
index 114f128f39b2a32bf85557926e30b72e3124dc21..cbe8d4f62dfa5d665b6314b614d29e0a4a214da8 100644 (file)
@@ -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") },