node-api: use WriteV2 in napi_get_value_string_utf16 · nodejs/node@429c38d

@@ -2520,21 +2520,23 @@ napi_status NAPI_CDECL napi_get_value_string_utf16(napi_env env,

2520252025212521

v8::Local<v8::Value> val = v8impl::V8LocalValueFromJsValue(value);

25222522

RETURN_STATUS_IF_FALSE(env, val->IsString(), napi_string_expected);

2523+

v8::Local<v8::String> str = val.As<v8::String>();

2523252425242525

if (!buf) {

25252526

CHECK_ARG(env, result);

25262527

// V8 assumes UTF-16 length is the same as the number of characters.

2527-

*result = val.As<v8::String>()->Length();

2528+

*result = str->Length();

25282529

} else if (bufsize != 0) {

2529-

int copied = val.As<v8::String>()->Write(env->isolate,

2530-

reinterpret_cast<uint16_t*>(buf),

2531-

0,

2532-

bufsize - 1,

2533-

v8::String::NO_NULL_TERMINATION);

2530+

uint32_t length = static_cast<uint32_t>(

2531+

std::min(bufsize - 1, static_cast<size_t>(str->Length())));

2532+

str->WriteV2(env->isolate,

2533+

0,

2534+

length,

2535+

reinterpret_cast<uint16_t*>(buf),

2536+

v8::String::WriteFlags::kNullTerminate);

253425372535-

buf[copied] = '\0';

25362538

if (result != nullptr) {

2537-

*result = copied;

2539+

*result = length;

25382540

}

25392541

} else if (result != nullptr) {

25402542

*result = 0;