]> git.kaiwu.me - njs.git/commitdiff
TextDecoder: allow omitted arguments in decode()
authorDmitry Volyntsev <xeioex@nginx.com>
Mon, 16 Nov 2026 01:39:43 +0000 (17:39 -0800)
committerDmitry Volyntsev <xeioexception@gmail.com>
Mon, 16 Nov 2026 17:10:26 +0000 (09:10 -0800)
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).

src/njs_encoding.c
src/qjs.c
test/text_decoder.t.js

index 26dd816f280b9659173db258e542b74a26f46221..1be139a571de9d81f211d10b41a55ccd1e442317 100644 (file)
@@ -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))) {
index 7d70d13b9a48236583c7a5dce07f9a3714302c1b..3f64aedddbee2ab2cc98abbad49a46a0549a8cfc 100644 (file)
--- 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;
index b4ac551c940b8e34c8e844c027767c4b9c83ffaf..9b152e2d221bfbe7eeebf9a78c8859e40208bd8a 100644 (file)
@@ -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,