summaryrefslogtreecommitdiff
path: root/src/node_buffer.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/node_buffer.cc')
-rw-r--r--src/node_buffer.cc23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/node_buffer.cc b/src/node_buffer.cc
index d05858ecbd..b3f5793f89 100644
--- a/src/node_buffer.cc
+++ b/src/node_buffer.cc
@@ -1200,6 +1200,27 @@ void Swap64(const FunctionCallbackInfo<Value>& args) {
}
+// Encode a single string to a UTF-8 Uint8Array (not Buffer).
+// Used in TextEncoder.prototype.encode.
+static void EncodeUtf8String(const FunctionCallbackInfo<Value>& args) {
+ Environment* env = Environment::GetCurrent(args);
+ CHECK_GE(args.Length(), 1);
+ CHECK(args[0]->IsString());
+
+ Local<String> str = args[0].As<String>();
+ size_t length = str->Utf8Length();
+ char* data = node::UncheckedMalloc(length);
+ str->WriteUtf8(data,
+ -1, // We are certain that `data` is sufficiently large
+ NULL,
+ String::NO_NULL_TERMINATION | String::REPLACE_INVALID_UTF8);
+ auto array_buf = ArrayBuffer::New(env->isolate(), data, length,
+ ArrayBufferCreationMode::kInternalized);
+ auto array = Uint8Array::New(array_buf, 0, length);
+ args.GetReturnValue().Set(array);
+}
+
+
// pass Buffer object to load prototype methods
void SetupBufferJS(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
@@ -1266,6 +1287,8 @@ void Initialize(Local<Object> target,
env->SetMethod(target, "swap32", Swap32);
env->SetMethod(target, "swap64", Swap64);
+ env->SetMethod(target, "encodeUtf8String", EncodeUtf8String);
+
target->Set(env->context(),
FIXED_ONE_BYTE_STRING(env->isolate(), "kMaxLength"),
Integer::NewFromUnsigned(env->isolate(), kMaxLength)).FromJust();