Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
src: move decodeUtf8 to node_encoding
  • Loading branch information
anonrig committed Dec 7, 2022
commit 815fbeef451468fd4eb57a634c09523eb0bf9aea
7 changes: 2 additions & 5 deletions lib/internal/encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ const {
} = require('internal/validators');

const {
decodeUTF8,
} = internalBinding('buffer');

const {
decodeUtf8,
encodeUtf8,
encodeIntoUtf8,
} = internalBinding('encoding_methods');
Expand Down Expand Up @@ -433,7 +430,7 @@ function makeTextDecoderICU() {
this[kUTF8FastPath] &&= !(options?.stream);

if (this[kUTF8FastPath]) {
return decodeUTF8(input, this[kIgnoreBOM]);
return decodeUtf8(input, this[kIgnoreBOM]);
}

this.#prepareConverter();
Expand Down
46 changes: 0 additions & 46 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -566,50 +566,6 @@ void StringSlice(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(ret);
}

// Convert the input into an encoded string
void DecodeUTF8(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args); // list, flags

CHECK_GE(args.Length(), 1);

if (!(args[0]->IsArrayBuffer() || args[0]->IsSharedArrayBuffer() ||
args[0]->IsArrayBufferView())) {
return node::THROW_ERR_INVALID_ARG_TYPE(
env->isolate(),
"The \"list\" argument must be an instance of SharedArrayBuffer, "
"ArrayBuffer or ArrayBufferView.");
}

ArrayBufferViewContents<char> buffer(args[0]);

bool ignore_bom = args[1]->IsTrue();

const char* data = buffer.data();
size_t length = buffer.length();

if (!ignore_bom && length >= 3) {
if (memcmp(data, "\xEF\xBB\xBF", 3) == 0) {
data += 3;
length -= 3;
}
}

if (length == 0) return args.GetReturnValue().SetEmptyString();

Local<Value> error;
MaybeLocal<Value> maybe_ret =
StringBytes::Encode(env->isolate(), data, length, UTF8, &error);
Local<Value> ret;

if (!maybe_ret.ToLocal(&ret)) {
CHECK(!error.IsEmpty());
env->isolate()->ThrowException(error);
return;
}

args.GetReturnValue().Set(ret);
}

// bytesCopied = copy(buffer, target[, targetStart][, sourceStart][, sourceEnd])
void Copy(const FunctionCallbackInfo<Value> &args) {
Environment* env = Environment::GetCurrent(args);
Expand Down Expand Up @@ -1259,7 +1215,6 @@ void Initialize(Local<Object> target,

SetMethod(context, target, "setBufferPrototype", SetBufferPrototype);
SetMethodNoSideEffect(context, target, "createFromString", CreateFromString);
SetMethodNoSideEffect(context, target, "decodeUTF8", DecodeUTF8);

SetMethodNoSideEffect(context, target, "byteLengthUtf8", ByteLengthUtf8);
SetMethod(context, target, "copy", Copy);
Expand Down Expand Up @@ -1314,7 +1269,6 @@ void Initialize(Local<Object> target,
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(SetBufferPrototype);
registry->Register(CreateFromString);
registry->Register(DecodeUTF8);

registry->Register(ByteLengthUtf8);
registry->Register(Copy);
Expand Down
50 changes: 49 additions & 1 deletion src/node_encoding.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "env-inl.h"
#include "node.h"
#include "node_errors.h"
#include "node_external_reference.h"
#include "node_internals.h"
#include "string_bytes.h"
#include "util-inl.h"
#include "v8-fast-api-calls.h"
#include "v8.h"
Expand All @@ -21,6 +23,7 @@ using v8::FastOneByteString;
using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::MaybeLocal;
using v8::Object;
using v8::String;
using v8::Uint32Array;
Expand Down Expand Up @@ -111,6 +114,49 @@ static void FastEncodeIntoUtf8(Local<Value> receiver,
results[1] = min_length;
}

void DecodeUtf8(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args); // list, flags

CHECK_GE(args.Length(), 1);

if (!(args[0]->IsArrayBuffer() || args[0]->IsSharedArrayBuffer() ||
args[0]->IsArrayBufferView())) {
return node::THROW_ERR_INVALID_ARG_TYPE(
env->isolate(),
"The \"list\" argument must be an instance of SharedArrayBuffer, "
"ArrayBuffer or ArrayBufferView.");
}

ArrayBufferViewContents<char> buffer(args[0]);

bool ignore_bom = args[1]->IsTrue();

const char* data = buffer.data();
size_t length = buffer.length();

if (!ignore_bom && length >= 3) {
if (memcmp(data, "\xEF\xBB\xBF", 3) == 0) {
data += 3;
length -= 3;
}
}

if (length == 0) return args.GetReturnValue().SetEmptyString();

Local<Value> error;
MaybeLocal<Value> maybe_ret =
StringBytes::Encode(env->isolate(), data, length, UTF8, &error);
Local<Value> ret;

if (!maybe_ret.ToLocal(&ret)) {
CHECK(!error.IsEmpty());
env->isolate()->ThrowException(error);
return;
}

args.GetReturnValue().Set(ret);
}

CFunction fast_encode_into_utf8_(CFunction::Make(FastEncodeIntoUtf8));
#endif // NODE_HAVE_I18N_SUPPORT

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

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

registry->Register(EncodeIntoUtf8);

#if defined(NODE_HAVE_I18N_SUPPORT)
registry->Register(FastEncodeIntoUtf8);
registry->Register(fast_encode_into_utf8_.GetTypeInfo());
#endif // NODE_HAVE_I18N_SUPPORT

registry->Register(DecodeUtf8);
}

} // namespace encoding_methods
Expand Down