diff --git a/src/node_i18n.cc b/src/node_i18n.cc index c537a247f55ff0..6c56c9dd166027 100644 --- a/src/node_i18n.cc +++ b/src/node_i18n.cc @@ -425,7 +425,11 @@ void ConverterObject::Create(const FunctionCallbackInfo& args) { nullptr, nullptr, nullptr, &status); } - new ConverterObject(env, obj, conv, flags); + auto converter = new ConverterObject(env, obj, conv, flags); + size_t sublen = ucnv_getMinCharSize(conv); + std::string sub(sublen, '?'); + converter->set_subst_chars(sub.c_str()); + args.GetReturnValue().Set(obj); } @@ -473,6 +477,11 @@ void ConverterObject::Decode(const FunctionCallbackInfo& args) { size_t source_length = input.length(); UChar* target = *result; + // printf("source c: %c\n", *source); + // printf("source d: %d\n", *source); + // printf("source s: %s\n", source); + // printf("source_length: %zu\n", source_length); + // printf("limit: %zu\n", limit); ucnv_toUnicode(converter->conv(), &target, target + limit, @@ -485,6 +494,7 @@ void ConverterObject::Decode(const FunctionCallbackInfo& args) { if (U_SUCCESS(status)) { bool omit_initial_bom = false; if (limit > 0) { + // printf("result[0]: %d\n", result[0]); result.SetLength(target - &result[0]); if (result.length() > 0 && converter->unicode() && diff --git a/test/parallel/test-whatwg-encoding-custom-textdecoder.js b/test/parallel/test-whatwg-encoding-custom-textdecoder.js index 1fa65164c70678..a3a0cb58f15b86 100644 --- a/test/parallel/test-whatwg-encoding-custom-textdecoder.js +++ b/test/parallel/test-whatwg-encoding-custom-textdecoder.js @@ -199,3 +199,10 @@ if (common.hasIntl) { const str = decoder.decode(chunk); assert.strictEqual(str, 'foo\ufffd'); } + +{ + const decoder = new TextDecoder('Shift_JIS'); + const chunk = new Uint8Array([-1]); + const str = decoder.decode(chunk); + assert.strictEqual(str, '\ufffd'); +}