GeezerVison is showing github.com — node-api: use WriteV2 in napi_get_value_string_utf16 · nodejs/node@429c38d
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: src/js_native_api_v8.cc +10-8Lines changed: 10 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2520,21 +2520,23 @@ napi_status NAPI_CDECL napi_get_value_string_utf16(napi_env env, |
2520 | 2520 |
|
2521 | 2521 | v8::Local<v8::Value> val = v8impl::V8LocalValueFromJsValue(value); |
2522 | 2522 | RETURN_STATUS_IF_FALSE(env, val->IsString(), napi_string_expected); |
| 2523 | + v8::Local<v8::String> str = val.As<v8::String>(); |
2523 | 2524 |
|
2524 | 2525 | if (!buf) { |
2525 | 2526 | CHECK_ARG(env, result); |
2526 | 2527 | // V8 assumes UTF-16 length is the same as the number of characters. |
2527 | | - *result = val.As<v8::String>()->Length(); |
| 2528 | + *result = str->Length(); |
2528 | 2529 | } 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); |
2534 | 2537 |
|
2535 | | - buf[copied] = '\0'; |
2536 | 2538 | if (result != nullptr) { |
2537 | | - *result = copied; |
| 2539 | + *result = length; |
2538 | 2540 | } |
2539 | 2541 | } else if (result != nullptr) { |
2540 | 2542 | *result = 0; |
|