Skip to content

Commit 815fbee

Browse files
committed
src: move decodeUtf8 to node_encoding
1 parent 7601758 commit 815fbee

File tree

3 files changed

+51
-52
lines changed

3 files changed

+51
-52
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');
@@ -433,7 +430,7 @@ function makeTextDecoderICU() {
433430
this[kUTF8FastPath] &&= !(options?.stream);
434431

435432
if (this[kUTF8FastPath]) {
436-
return decodeUTF8(input, this[kIgnoreBOM]);
433+
return decodeUtf8(input, this[kIgnoreBOM]);
437434
}
438435

439436
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 & 1 deletion
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"
@@ -21,6 +23,7 @@ using v8::FastOneByteString;
2123
using v8::FunctionCallbackInfo;
2224
using v8::Isolate;
2325
using v8::Local;
26+
using v8::MaybeLocal;
2427
using v8::Object;
2528
using v8::String;
2629
using v8::Uint32Array;
@@ -111,6 +114,49 @@ static void FastEncodeIntoUtf8(Local<Value> receiver,
111114
results[1] = min_length;
112115
}
113116

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

@@ -128,17 +174,19 @@ static void Initialize(Local<Object> target,
128174
#else
129175
SetMethodNoSideEffect(context, target, "encodeIntoUtf8", EncodeIntoUtf8);
130176
#endif // NODE_HAVE_I18N_SUPPORT
177+
SetMethodNoSideEffect(context, target, "decodeUtf8", DecodeUtf8);
131178
}
132179

133180
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
134181
registry->Register(EncodeUtf8);
135182

136183
registry->Register(EncodeIntoUtf8);
137-
138184
#if defined(NODE_HAVE_I18N_SUPPORT)
139185
registry->Register(FastEncodeIntoUtf8);
140186
registry->Register(fast_encode_into_utf8_.GetTypeInfo());
141187
#endif // NODE_HAVE_I18N_SUPPORT
188+
189+
registry->Register(DecodeUtf8);
142190
}
143191

144192
} // namespace encoding_methods

0 commit comments

Comments
 (0)