Skip to content

Commit 3642049

Browse files
committed
src: move decodeUtf8 to node_encoding
1 parent 1f41306 commit 3642049

File tree

3 files changed

+51
-51
lines changed

3 files changed

+51
-51
lines changed

lib/internal/encoding.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,7 @@ const {
5151
} = require('internal/validators');
5252

5353
const {
54-
decodeUTF8,
55-
} = internalBinding('buffer');
56-
57-
const {
54+
decodeUtf8,
5855
encodeUtf8,
5956
encodeIntoUtf8,
6057
} = internalBinding('encoding_methods');
@@ -440,7 +437,7 @@ function makeTextDecoderICU() {
440437
this[kUTF8FastPath] &&= !(options?.stream);
441438

442439
if (this[kUTF8FastPath]) {
443-
return decodeUTF8(input, this[kIgnoreBOM]);
440+
return decodeUtf8(input, this[kIgnoreBOM]);
444441
}
445442

446443
this.#prepareConverter();

src/node_buffer.cc

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -566,50 +566,6 @@ void StringSlice(const FunctionCallbackInfo<Value>& args) {
566566
args.GetReturnValue().Set(ret);
567567
}
568568

569-
// Convert the input into an encoded string
570-
void DecodeUTF8(const FunctionCallbackInfo<Value>& args) {
571-
Environment* env = Environment::GetCurrent(args); // list, flags
572-
573-
CHECK_GE(args.Length(), 1);
574-
575-
if (!(args[0]->IsArrayBuffer() || args[0]->IsSharedArrayBuffer() ||
576-
args[0]->IsArrayBufferView())) {
577-
return node::THROW_ERR_INVALID_ARG_TYPE(
578-
env->isolate(),
579-
"The \"list\" argument must be an instance of SharedArrayBuffer, "
580-
"ArrayBuffer or ArrayBufferView.");
581-
}
582-
583-
ArrayBufferViewContents<char> buffer(args[0]);
584-
585-
bool ignore_bom = args[1]->IsTrue();
586-
587-
const char* data = buffer.data();
588-
size_t length = buffer.length();
589-
590-
if (!ignore_bom && length >= 3) {
591-
if (memcmp(data, "\xEF\xBB\xBF", 3) == 0) {
592-
data += 3;
593-
length -= 3;
594-
}
595-
}
596-
597-
if (length == 0) return args.GetReturnValue().SetEmptyString();
598-
599-
Local<Value> error;
600-
MaybeLocal<Value> maybe_ret =
601-
StringBytes::Encode(env->isolate(), data, length, UTF8, &error);
602-
Local<Value> ret;
603-
604-
if (!maybe_ret.ToLocal(&ret)) {
605-
CHECK(!error.IsEmpty());
606-
env->isolate()->ThrowException(error);
607-
return;
608-
}
609-
610-
args.GetReturnValue().Set(ret);
611-
}
612-
613569
// bytesCopied = copy(buffer, target[, targetStart][, sourceStart][, sourceEnd])
614570
void Copy(const FunctionCallbackInfo<Value> &args) {
615571
Environment* env = Environment::GetCurrent(args);
@@ -1259,7 +1215,6 @@ void Initialize(Local<Object> target,
12591215

12601216
SetMethod(context, target, "setBufferPrototype", SetBufferPrototype);
12611217
SetMethodNoSideEffect(context, target, "createFromString", CreateFromString);
1262-
SetMethodNoSideEffect(context, target, "decodeUTF8", DecodeUTF8);
12631218

12641219
SetMethodNoSideEffect(context, target, "byteLengthUtf8", ByteLengthUtf8);
12651220
SetMethod(context, target, "copy", Copy);
@@ -1314,7 +1269,6 @@ void Initialize(Local<Object> target,
13141269
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
13151270
registry->Register(SetBufferPrototype);
13161271
registry->Register(CreateFromString);
1317-
registry->Register(DecodeUTF8);
13181272

13191273
registry->Register(ByteLengthUtf8);
13201274
registry->Register(Copy);

src/node_encoding.cc

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#include "env-inl.h"
22
#include "node.h"
3+
#include "node_errors.h"
34
#include "node_external_reference.h"
45
#include "node_internals.h"
6+
#include "string_bytes.h"
57
#include "util-inl.h"
68
#include "v8-fast-api-calls.h"
79
#include "v8.h"
@@ -22,6 +24,7 @@ using v8::FastOneByteString;
2224
using v8::FunctionCallbackInfo;
2325
using v8::Isolate;
2426
using v8::Local;
27+
using v8::MaybeLocal;
2528
using v8::Object;
2629
using v8::String;
2730
using v8::Uint32Array;
@@ -116,6 +119,49 @@ static void FastEncodeIntoUtf8(
116119
#endif // NODE_HAVE_I18N_SUPPORT
117120
}
118121

122+
void DecodeUtf8(const FunctionCallbackInfo<Value>& args) {
123+
Environment* env = Environment::GetCurrent(args); // list, flags
124+
125+
CHECK_GE(args.Length(), 1);
126+
127+
if (!(args[0]->IsArrayBuffer() || args[0]->IsSharedArrayBuffer() ||
128+
args[0]->IsArrayBufferView())) {
129+
return node::THROW_ERR_INVALID_ARG_TYPE(
130+
env->isolate(),
131+
"The \"list\" argument must be an instance of SharedArrayBuffer, "
132+
"ArrayBuffer or ArrayBufferView.");
133+
}
134+
135+
ArrayBufferViewContents<char> buffer(args[0]);
136+
137+
bool ignore_bom = args[1]->IsTrue();
138+
139+
const char* data = buffer.data();
140+
size_t length = buffer.length();
141+
142+
if (!ignore_bom && length >= 3) {
143+
if (memcmp(data, "\xEF\xBB\xBF", 3) == 0) {
144+
data += 3;
145+
length -= 3;
146+
}
147+
}
148+
149+
if (length == 0) return args.GetReturnValue().SetEmptyString();
150+
151+
Local<Value> error;
152+
MaybeLocal<Value> maybe_ret =
153+
StringBytes::Encode(env->isolate(), data, length, UTF8, &error);
154+
Local<Value> ret;
155+
156+
if (!maybe_ret.ToLocal(&ret)) {
157+
CHECK(!error.IsEmpty());
158+
env->isolate()->ThrowException(error);
159+
return;
160+
}
161+
162+
args.GetReturnValue().Set(ret);
163+
}
164+
119165
CFunction fast_encode_into_utf8_(CFunction::Make(FastEncodeIntoUtf8));
120166

121167
static void Initialize(Local<Object> target,
@@ -128,6 +174,7 @@ static void Initialize(Local<Object> target,
128174
"encodeIntoUtf8",
129175
EncodeIntoUtf8,
130176
&fast_encode_into_utf8_);
177+
SetMethodNoSideEffect(context, target, "decodeUtf8", DecodeUtf8);
131178
}
132179

133180
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
@@ -136,6 +183,8 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
136183
registry->Register(EncodeIntoUtf8);
137184
registry->Register(FastEncodeIntoUtf8);
138185
registry->Register(fast_encode_into_utf8_.GetTypeInfo());
186+
187+
registry->Register(DecodeUtf8);
139188
}
140189

141190
} // namespace encoding_methods

0 commit comments

Comments
 (0)