]> git.kaiwu.me - njs.git/commitdiff
Fix Array.prototype.toString() forwarding arguments to join()
authorDmitry Volyntsev <xeioex@nginx.com>
Thu, 12 Nov 2026 02:19:22 +0000 (18:19 -0800)
committerDmitry Volyntsev <xeioexception@gmail.com>
Thu, 12 Nov 2026 20:57:17 +0000 (12:57 -0800)
Previously, arguments passed to toString() were forwarded to join(),
changing the separator and the arguments seen by a custom join method.
The same issue affected typed arrays using the shared implementation.
This behavior dates back to the initial import in bffbe5e4 (0.1.0).

Call join() with only the receiver, as required by ECMA-262 section
23.1.3.36.  Extra arguments must neither reach join() nor be converted
to strings.

This closes #1150 issue on GitHub.

src/njs_array.c
src/test/njs_unit_test.c

index aea9c32b6b4e47bc3cbc11bbcf493febc4f94c42..d42a58ca5b53a473b6866fa28c50dd031b67d21e 100644 (file)
@@ -1612,7 +1612,7 @@ njs_array_prototype_to_string(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
         }
 
         if (njs_is_function(&value)) {
-            return njs_function_apply(vm, njs_function(&value), args, nargs,
+            return njs_function_apply(vm, njs_function(&value), args, 1,
                                       retval);
         }
     }
index f133be0b2694ba3359ff649617e1e3351dccf08e..45a1b252ab47e8b2bcc80cf8edd55946182f9a42 100644 (file)
@@ -5277,6 +5277,32 @@ static njs_unit_test_t  njs_test[] =
 
     /* Array.toString(). */
 
+    { njs_str("[1, 2].toString('-')"),
+      njs_str("1,2") },
+
+    { njs_str("[1, [2, 3]].toString(0)"),
+      njs_str("1,2,3") },
+
+    { njs_str("[1, 2].toString({toString() {throw 'unexpected'}})"),
+      njs_str("1,2") },
+
+    { njs_str("var a = [1, 2];"
+              "a.join = function() {return [this === a, arguments.length]};"
+              "a.toString('-', 42)"),
+      njs_str("true,0") },
+
+    { njs_str("Array.prototype.toString.call({join: function() {"
+              "return arguments.length}}, '-', 42)"),
+      njs_str("0") },
+
+    { njs_str("new Uint8Array([1, 2]).toString('-')"),
+      njs_str("1,2") },
+
+    { njs_str("var a = new Uint8Array([1, 2]);"
+              "a.join = function() {return [this === a, arguments.length]};"
+              "a.toString('-', 42)"),
+      njs_str("true,0") },
+
     { njs_str("var a = [1,2,3]; a.join = 'NO';"
                  "Object.prototype.toString = function () { return 'A' }; a"),
       njs_str("[object Array]") },