Skip to content

Commit

Permalink
src: move decodeUtf8 to node_encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Dec 6, 2022
1 parent 1f41306 commit 3642049
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 51 deletions.
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 @@ -440,7 +437,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
49 changes: 49 additions & 0 deletions 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 @@ -22,6 +24,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 @@ -116,6 +119,49 @@ static void FastEncodeIntoUtf8(
#endif // NODE_HAVE_I18N_SUPPORT
}

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));

static void Initialize(Local<Object> target,
Expand All @@ -128,6 +174,7 @@ static void Initialize(Local<Object> target,
"encodeIntoUtf8",
EncodeIntoUtf8,
&fast_encode_into_utf8_);
SetMethodNoSideEffect(context, target, "decodeUtf8", DecodeUtf8);
}

void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
Expand All @@ -136,6 +183,8 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(EncodeIntoUtf8);
registry->Register(FastEncodeIntoUtf8);
registry->Register(fast_encode_into_utf8_.GetTypeInfo());

registry->Register(DecodeUtf8);
}

} // namespace encoding_methods
Expand Down

0 comments on commit 3642049

Please sign in to comment.