From da6a60e573d2ad303f08e0ef93777fc91d25e171 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Sun, 4 May 2025 17:00:43 +0100 Subject: [PATCH] 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. --- src/js_native_api_v8.cc | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/js_native_api_v8.cc b/src/js_native_api_v8.cc index 8f97ca4d9cbb84..cc2312bd2fd1ed 100644 --- a/src/js_native_api_v8.cc +++ b/src/js_native_api_v8.cc @@ -2520,21 +2520,23 @@ napi_status NAPI_CDECL napi_get_value_string_utf16(napi_env env, v8::Local val = v8impl::V8LocalValueFromJsValue(value); RETURN_STATUS_IF_FALSE(env, val->IsString(), napi_string_expected); + v8::Local str = val.As(); if (!buf) { CHECK_ARG(env, result); // V8 assumes UTF-16 length is the same as the number of characters. - *result = val.As()->Length(); + *result = str->Length(); } else if (bufsize != 0) { - int copied = val.As()->Write(env->isolate, - reinterpret_cast(buf), - 0, - bufsize - 1, - v8::String::NO_NULL_TERMINATION); + uint32_t length = static_cast( + std::min(bufsize - 1, static_cast(str->Length()))); + str->WriteV2(env->isolate, + 0, + length, + reinterpret_cast(buf), + v8::String::WriteFlags::kNullTerminate); - buf[copied] = '\0'; if (result != nullptr) { - *result = copied; + *result = length; } } else if (result != nullptr) { *result = 0;