Skip to content

Commit 86f6bee

Browse files
committed
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.
1 parent 6de55f7 commit 86f6bee

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

src/js_native_api_v8.cc

+8-7
Original file line numberDiff line numberDiff line change
@@ -2526,15 +2526,16 @@ napi_status NAPI_CDECL napi_get_value_string_utf16(napi_env env,
25262526
// V8 assumes UTF-16 length is the same as the number of characters.
25272527
*result = val.As<v8::String>()->Length();
25282528
} 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);
2529+
size_t n_chars = std::min(
2530+
bufsize - 1, static_cast<size_t>(val.As<v8::String>()->Length()));
2531+
val.As<v8::String>()->WriteV2(env->isolate,
2532+
0,
2533+
n_chars,
2534+
reinterpret_cast<uint16_t*>(buf),
2535+
v8::String::WriteFlags::kNullTerminate);
25342536

2535-
buf[copied] = '\0';
25362537
if (result != nullptr) {
2537-
*result = copied;
2538+
*result = n_chars;
25382539
}
25392540
} else if (result != nullptr) {
25402541
*result = 0;

0 commit comments

Comments
 (0)