From: Dmitry Volyntsev Date: Thu, 12 Nov 2026 02:19:22 +0000 (-0800) Subject: Fix Array.prototype.toString() forwarding arguments to join() X-Git-Url: https://git.kaiwu.me/ngx_http_auth_basic_module.html?a=commitdiff_plain;h=fb50b5953e26d7c01c3994c30170bb08ed7502f0;p=njs.git Fix Array.prototype.toString() forwarding arguments to join() 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. --- diff --git a/src/njs_array.c b/src/njs_array.c index aea9c32b..d42a58ca 100644 --- a/src/njs_array.c +++ b/src/njs_array.c @@ -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); } } diff --git a/src/test/njs_unit_test.c b/src/test/njs_unit_test.c index f133be0b..45a1b252 100644 --- a/src/test/njs_unit_test.c +++ b/src/test/njs_unit_test.c @@ -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]") },