Skip to content

node-api: use WriteV2 in napi_get_value_string_utf16 #58165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
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
18 changes: 10 additions & 8 deletions src/js_native_api_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2520,21 +2520,23 @@ napi_status NAPI_CDECL napi_get_value_string_utf16(napi_env env,

v8::Local<v8::Value> val = v8impl::V8LocalValueFromJsValue(value);
RETURN_STATUS_IF_FALSE(env, val->IsString(), napi_string_expected);
v8::Local<v8::String> str = val.As<v8::String>();

if (!buf) {
CHECK_ARG(env, result);
// V8 assumes UTF-16 length is the same as the number of characters.
*result = val.As<v8::String>()->Length();
*result = str->Length();
} else if (bufsize != 0) {
int copied = val.As<v8::String>()->Write(env->isolate,
reinterpret_cast<uint16_t*>(buf),
0,
bufsize - 1,
v8::String::NO_NULL_TERMINATION);
uint32_t length = static_cast<uint32_t>(
std::min(bufsize - 1, static_cast<size_t>(str->Length())));
str->WriteV2(env->isolate,
0,
length,
reinterpret_cast<uint16_t*>(buf),
v8::String::WriteFlags::kNullTerminate);

buf[copied] = '\0';
if (result != nullptr) {
*result = copied;
*result = length;
}
} else if (result != nullptr) {
*result = 0;
Expand Down
Loading