Skip to content

Commit 971148c

Browse files
authored
node-api: use WriteV2 in napi_get_value_string_utf16
Since `String::Write()` is deprecated, use `String::Write2()` instead. That requires us to compute the correct number of characters ahead of time but removes the need for dealing with the return value. PR-URL: #58165 Reviewed-By: Juan José Arboleda <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Vladimir Morozov <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
1 parent ff3a028 commit 971148c

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/js_native_api_v8.cc

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2520,21 +2520,23 @@ napi_status NAPI_CDECL napi_get_value_string_utf16(napi_env env,
25202520

25212521
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>();
25232524

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

2535-
buf[copied] = '\0';
25362538
if (result != nullptr) {
2537-
*result = copied;
2539+
*result = length;
25382540
}
25392541
} else if (result != nullptr) {
25402542
*result = 0;

0 commit comments

Comments
 (0)