Skip to content

Commit bd013be

Browse files
committed
src: move decodeUtf8 to node_encoding
1 parent 1250410 commit bd013be

File tree

3 files changed

+51
-52
lines changed

3 files changed

+51
-52
lines changed

lib/internal/encoding.js

+2-5
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

-46
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

+49-1
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;
@@ -120,6 +123,49 @@ static void FastEncodeIntoUtf8(Local<Value> receiver,
120123
results[1] = offset;
121124
}
122125

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

@@ -137,17 +183,19 @@ static void Initialize(Local<Object> target,
137183
#else
138184
SetMethodNoSideEffect(context, target, "encodeIntoUtf8", EncodeIntoUtf8);
139185
#endif // NODE_HAVE_I18N_SUPPORT
186+
SetMethodNoSideEffect(context, target, "decodeUtf8", DecodeUtf8);
140187
}
141188

142189
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
143190
registry->Register(EncodeUtf8);
144191

145192
registry->Register(EncodeIntoUtf8);
146-
147193
#if defined(NODE_HAVE_I18N_SUPPORT)
148194
registry->Register(FastEncodeIntoUtf8);
149195
registry->Register(fast_encode_into_utf8_.GetTypeInfo());
150196
#endif // NODE_HAVE_I18N_SUPPORT
197+
198+
registry->Register(DecodeUtf8);
151199
}
152200

153201
} // namespace encoding_methods

0 commit comments

Comments
 (0)