From: Dmitry Volyntsev Date: Mon, 16 Nov 2026 01:39:43 +0000 (-0800) Subject: TextDecoder: allow omitted arguments in decode() X-Git-Url: https://git.kaiwu.me/%7Bsubstring-before(@doc,%20'.xml')%7D.html?a=commitdiff_plain;h=a574881b8bac41e1ac137de4bafe6d05452ce792;p=njs.git TextDecoder: allow omitted arguments in decode() Previously, decode() rejected an undefined input or options argument, although both are optional. The njs engine threw a TypeError for decode(undefined) and for null or undefined options, and QuickJS also rejected decode() without arguments. This prevented flushing a stream with decode() or decode(undefined, {stream: true}). Now an undefined input is treated as empty, and null or undefined options are treated as default options, as the Web IDL conversion of the TextDecoder.decode() arguments requires. The issue was introduced in 0570392f (0.4.3) and copied into QuickJS in 447d66d4 (0.8.10). --- diff --git a/src/njs_encoding.c b/src/njs_encoding.c index 26dd816f..1be139a5 100644 --- a/src/njs_encoding.c +++ b/src/njs_encoding.c @@ -515,9 +515,9 @@ njs_text_decoder_decode(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, return NJS_ERROR; } - if (nargs > 2) { - options = njs_argument(args, 2); + options = njs_arg(args, nargs, 2); + if (!njs_is_null_or_undefined(options)) { if (njs_slow_path(!njs_is_object(options))) { njs_type_error(vm, "The \"options\" argument must be " "of type object"); @@ -532,9 +532,9 @@ njs_text_decoder_decode(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, stream = njs_bool(retval); } - if (njs_fast_path(nargs > 1)) { - value = njs_argument(args, 1); + value = njs_arg(args, nargs, 1); + if (njs_fast_path(!njs_is_undefined(value))) { if (njs_is_typed_array(value)) { array = njs_typed_array(value); if (njs_slow_path(njs_is_detached_buffer(array->buffer))) { diff --git a/src/qjs.c b/src/qjs.c index 7d70d13b..3f64aedd 100644 --- a/src/qjs.c +++ b/src/qjs.c @@ -910,7 +910,7 @@ qjs_text_decoder_decode(JSContext *cx, JSValueConst this_val, int argc, stream = 0; - if (argc > 1) { + if (argc > 1 && !JS_IsNullOrUndefined(argv[1])) { ret = JS_GetPropertyStr(cx, argv[1], "stream"); if (JS_IsException(ret)) { return JS_EXCEPTION; @@ -920,9 +920,14 @@ qjs_text_decoder_decode(JSContext *cx, JSValueConst this_val, int argc, JS_FreeValue(cx, ret); } - ret = qjs_typed_array_data(cx, argv[0], &data); - if (JS_IsException(ret)) { - return ret; + data.start = NULL; + data.length = 0; + + if (!JS_IsUndefined(argv[0])) { + ret = qjs_typed_array_data(cx, argv[0], &data); + if (JS_IsException(ret)) { + return ret; + } } end = data.start + data.length; diff --git a/test/text_decoder.t.js b/test/text_decoder.t.js index b4ac551c..9b152e2d 100644 --- a/test/text_decoder.t.js +++ b/test/text_decoder.t.js @@ -172,6 +172,37 @@ let reuse_tsuite = { ], }; +let args_tsuite = { + name: "TextDecoder() optional arguments tests", + T: async (params) => { + let td = new TextDecoder('utf-8'); + let r = params.prefix ? td.decode(params.prefix, {stream: true}) : ''; + + r += td.decode.apply(td, params.args); + + if (params.exception) { + return `expected ${params.exception} not thrown`; + } + + if (r !== params.expected) { + throw Error(`unexpected output "${r}" != "${params.expected}"`); + } + + return 'SUCCESS'; + }, + + tests: [ + { args: [], expected: '' }, + { args: [undefined], expected: '' }, + { args: [new Uint8Array([0x41]), undefined], expected: 'A' }, + { args: [new Uint8Array([0x41]), null], expected: 'A' }, + { prefix: new Uint8Array([0xE2, 0x82]), args: [], expected: '\uFFFD' }, + { prefix: new Uint8Array([0xE2]), + args: [undefined, {stream: true}], expected: '' }, + { args: [null], exception: 'TypeError' }, + ], +}; + let ignoreBOM_tsuite = { name: "TextDecoder() ignoreBOM tests", T: async (params) => { @@ -269,6 +300,7 @@ run([ stream_tsuite, fatal_tsuite, reuse_tsuite, + args_tsuite, ignoreBOM_tsuite, detached_tsuite, detached_options_tsuite,