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");
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))) {
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;
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;
],
};
+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) => {
stream_tsuite,
fatal_tsuite,
reuse_tsuite,
+ args_tsuite,
ignoreBOM_tsuite,
detached_tsuite,
detached_options_tsuite,