[njs] Fixed Array.prototype.join() with "undefined" argument.

Alexander Borisov alexander.borisov at nginx.com
Wed Oct 30 13:43:44 UTC 2019


details:   https://hg.nginx.org/njs/rev/758bbc9f105a
branches:  
changeset: 1208:758bbc9f105a
user:      Alexander Borisov <alexander.borisov at nginx.com>
date:      Wed Oct 30 16:43:12 2019 +0300
description:
Fixed Array.prototype.join() with "undefined" argument.

This closes #232 issue on GitHub.

diffstat:

 src/njs_array.c          |  26 +++++++++++++-------------
 src/test/njs_unit_test.c |   3 +++
 2 files changed, 16 insertions(+), 13 deletions(-)

diffs (59 lines):

diff -r 2f21b596ce15 -r 758bbc9f105a src/njs_array.c
--- a/src/njs_array.c	Wed Oct 30 16:43:10 2019 +0300
+++ b/src/njs_array.c	Wed Oct 30 16:43:12 2019 +0300
@@ -1144,13 +1144,22 @@ njs_array_prototype_join(njs_vm_t *vm, n
         return ret;
     }
 
-    if (nargs > 1 && !njs_is_string(&args[1])) {
-        ret = njs_value_to_string(vm, &args[1], &args[1]);
-        if (njs_slow_path(ret != NJS_OK)) {
-            return ret;
+    value = njs_arg(args, nargs, 1);
+
+    if (njs_slow_path(!njs_is_string(value))) {
+        if (njs_is_undefined(value)) {
+            value = njs_value_arg(&njs_string_comma);
+
+        } else {
+            ret = njs_value_to_string(vm, value, value);
+            if (njs_slow_path(ret != NJS_OK)) {
+                return ret;
+            }
         }
     }
 
+    (void) njs_string_prop(&separator, value);
+
     if (!njs_is_array(&args[0]) || njs_array_len(&args[0]) == 0) {
         vm->retval = njs_string_empty;
         return NJS_OK;
@@ -1232,15 +1241,6 @@ njs_array_prototype_join(njs_vm_t *vm, n
         }
     }
 
-    if (nargs > 1) {
-        value = &args[1];
-
-    } else {
-        value = njs_value_arg(&njs_string_comma);
-    }
-
-    (void) njs_string_prop(&separator, value);
-
     size += separator.size * (array->length - 1);
     length += separator.length * (array->length - 1);
 
diff -r 2f21b596ce15 -r 758bbc9f105a src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Wed Oct 30 16:43:10 2019 +0300
+++ b/src/test/njs_unit_test.c	Wed Oct 30 16:43:12 2019 +0300
@@ -7756,6 +7756,9 @@ static njs_unit_test_t  njs_test[] =
     { njs_str("[].join.call()"),
       njs_str("TypeError: cannot convert null or undefined to object") },
 
+    { njs_str("[1,2,3].join(undefined)"),
+      njs_str("1,2,3") },
+
     { njs_str("[].slice.call()"),
       njs_str("TypeError: cannot convert null or undefined to object") },
 


More information about the nginx-devel mailing list