[njs] Array.join() now treats "null" and "undefined" as empty va...

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


details:   http://hg.nginx.org/njs/rev/87df6b787943
branches:  
changeset: 153:87df6b787943
user:      Igor Sysoev <igor at sysoev.ru>
date:      Thu Aug 11 13:21:46 2016 +0300
description:
Array.join() now treats "null" and "undefined" as empty values.

diffstat:

 njs/njs_array.c          |  14 ++++++++++----
 njs/test/njs_unit_test.c |  11 +++++++++++
 2 files changed, 21 insertions(+), 4 deletions(-)

diffs (66 lines):

diff -r 18ac628bcb6c -r 87df6b787943 njs/njs_array.c
--- a/njs/njs_array.c	Thu Aug 11 10:58:29 2016 +0300
+++ b/njs/njs_array.c	Thu Aug 11 13:21:46 2016 +0300
@@ -734,7 +734,10 @@ njs_array_prototype_join(njs_vm_t *vm, n
 
     for (i = 0; i < array->length; i++) {
         value = &array->start[i];
-        if (njs_is_valid(value) && !njs_is_string(value)) {
+        if (!njs_is_string(value)
+            && njs_is_valid(value)
+            && !njs_is_null_or_void(value))
+        {
             max++;
         }
     }
@@ -755,7 +758,10 @@ njs_array_prototype_join(njs_vm_t *vm, n
 
         for (i = 0; i < array->length; i++) {
             value = &array->start[i];
-            if (njs_is_valid(value) && !njs_is_string(value)) {
+            if (!njs_is_string(value)
+                && njs_is_valid(value)
+                && !njs_is_null_or_void(value))
+            {
                 values[n++] = *value;
 
                 if (n >= max) {
@@ -802,7 +808,7 @@ njs_array_prototype_join_continuation(nj
     for (i = 0; i < array->length; i++) {
         value = &array->start[i];
 
-        if (njs_is_valid(value)) {
+        if (njs_is_valid(value) && !njs_is_null_or_void(value)) {
 
             if (!njs_is_string(value)) {
                 value = &values[n++];
@@ -849,7 +855,7 @@ njs_array_prototype_join_continuation(nj
     for (i = 0; i < array->length; i++) {
         value = &array->start[i];
 
-        if (njs_is_valid(value)) {
+        if (njs_is_valid(value) && !njs_is_null_or_void(value)) {
             if (!njs_is_string(value)) {
                 value = &values[n++];
             }
diff -r 18ac628bcb6c -r 87df6b787943 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Thu Aug 11 10:58:29 2016 +0300
+++ b/njs/test/njs_unit_test.c	Thu Aug 11 13:21:46 2016 +0300
@@ -2124,6 +2124,17 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("a = []; a[5] = 5; a.join()"),
       nxt_string(",,,,,5") },
 
+    { nxt_string("var a = [,null,undefined,false,true,0,1]; a.join()"),
+      nxt_string(",,,false,true,0,1") },
+
+    { nxt_string("var o = { toString: function() { return null } };"
+                 "[o].join()"),
+      nxt_string("null") },
+
+    { nxt_string("var o = { toString: function() { return undefined } };"
+                 "[o].join()"),
+      nxt_string("undefined") },
+
     { nxt_string("a = []; a[5] = 5; a"),
       nxt_string(",,,,,5") },
 



More information about the nginx-devel mailing list