Skip to content

Commit 5984444

Browse files
authored
node-api: use WriteOneByteV2 in napi_get_value_string_latin1
PR-URL: #58325 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
1 parent 971148c commit 5984444

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

src/js_native_api_v8.cc

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2441,21 +2441,21 @@ napi_status NAPI_CDECL napi_get_value_string_latin1(
24412441

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

24452446
if (!buf) {
24462447
CHECK_ARG(env, result);
2447-
*result = val.As<v8::String>()->Length();
2448+
*result = str->Length();
24482449
} else if (bufsize != 0) {
2449-
int copied =
2450-
val.As<v8::String>()->WriteOneByte(env->isolate,
2451-
reinterpret_cast<uint8_t*>(buf),
2452-
0,
2453-
bufsize - 1,
2454-
v8::String::NO_NULL_TERMINATION);
2455-
2456-
buf[copied] = '\0';
2450+
uint32_t length = static_cast<uint32_t>(
2451+
std::min(bufsize - 1, static_cast<size_t>(str->Length())));
2452+
str->WriteOneByteV2(env->isolate,
2453+
0,
2454+
length,
2455+
reinterpret_cast<uint8_t*>(buf),
2456+
v8::String::WriteFlags::kNullTerminate);
24572457
if (result != nullptr) {
2458-
*result = copied;
2458+
*result = length;
24592459
}
24602460
} else if (result != nullptr) {
24612461
*result = 0;
@@ -2479,12 +2479,12 @@ napi_status NAPI_CDECL napi_get_value_string_utf8(
24792479

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

24832484
if (!buf) {
24842485
CHECK_ARG(env, result);
2485-
*result = val.As<v8::String>()->Utf8LengthV2(env->isolate);
2486+
*result = str->Utf8LengthV2(env->isolate);
24862487
} else if (bufsize != 0) {
2487-
auto str = val.As<v8::String>();
24882488
size_t copied =
24892489
str->WriteUtf8V2(env->isolate,
24902490
buf,

0 commit comments

Comments
 (0)