From 9214ab30b79c9c7afa1af2dcb8aa46c20602c09a Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Thu, 3 Nov 2022 16:06:57 -0400 Subject: [PATCH] util: change implementation to not use buffers --- src/node_i18n.cc | 43 +++++++++++++++++++------------------------ 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/src/node_i18n.cc b/src/node_i18n.cc index 1fc2e0c9cbbe40..8ff5e76d6b4eff 100644 --- a/src/node_i18n.cc +++ b/src/node_i18n.cc @@ -97,7 +97,6 @@ using v8::NewStringType; using v8::Object; using v8::ObjectTemplate; using v8::String; -using v8::Uint8Array; using v8::Value; namespace i18n { @@ -446,7 +445,6 @@ void ConverterObject::Decode(const FunctionCallbackInfo& args) { UErrorCode status = U_ZERO_ERROR; MaybeStackBuffer result; - MaybeLocal ret; UBool flush = (flags & CONVERTER_FLAGS_FLUSH) == CONVERTER_FLAGS_FLUSH; @@ -502,31 +500,28 @@ void ConverterObject::Decode(const FunctionCallbackInfo& args) { converter->set_bom_seen(true); } } - ret = ToBufferEndian(env, &result); - if (!ret.IsEmpty()) { - CHECK(ret.ToLocalChecked()->IsUint8Array()); - - if (omit_initial_bom) { - // Perform `ret = ret.slice(2)`. - Local orig_ret = ret.ToLocalChecked().As(); - ret = Buffer::New(env, - orig_ret->Buffer(), - orig_ret->ByteOffset() + 2, - orig_ret->ByteLength() - 2) - .FromMaybe(Local()); - } + Local error; + char16_t* output = result.out(); + size_t beginning = 0; + size_t length = 0; - Local error; - ArrayBufferViewContents buf(ret.ToLocalChecked()); - MaybeLocal encoded = StringBytes::Encode( - env->isolate(), buf.data(), buf.length(), encoding::UCS2, &error); + for (size_t i = 0; i < result.length(); i++) length += sizeof(output[i]); - if (!encoded.IsEmpty()) { - args.GetReturnValue().Set(encoded.ToLocalChecked()); - } else { - args.GetReturnValue().Set(error); - } + if (omit_initial_bom) { + // Perform `ret = ret.slice(2)`. + beginning += 2; + length -= 2; + } + + const char* value = reinterpret_cast(output) + beginning; + MaybeLocal encoded = + StringBytes::Encode(env->isolate(), value, length, UCS2, &error); + + if (!encoded.IsEmpty()) { + args.GetReturnValue().Set(encoded.ToLocalChecked()); + } else { + args.GetReturnValue().Set(error); } return;