|
2 | 2 | #define LINUX_PERF_UTILS_H |
3 | 3 |
|
4 | 4 | #include "v8-profiler.h" |
| 5 | +#include <js_native_api.h> |
| 6 | +#include <string> |
5 | 7 |
|
| 8 | +// The string conversions are the only part of the V8 C++ API used here that is |
| 9 | +// not ABI-stable: Utf8Value's constructor gained a defaulted argument in Node |
| 10 | +// 24, and WriteUtf8 gave way to WriteUtf8V2 in Node 26, so none of them |
| 11 | +// resolves on every major. Node-API is versioned and does not move, and |
| 12 | +// napi_value is layout-compatible with v8::Local<v8::Value> by construction. |
6 | 13 | static inline std::string |
7 | | -v8LocalStringToString(v8::Local<v8::String> v8String) { |
8 | | - // Utf8Value NUL-terminates, so the c-string constructor stops at the first |
9 | | - // embedded NUL, as callers expect for symbol names. It yields nullptr when |
10 | | - // the conversion throws. |
11 | | - v8::String::Utf8Value value(v8::Isolate::GetCurrent(), v8String); |
12 | | - return *value ? std::string(*value) : std::string(); |
| 14 | +v8LocalStringToString(napi_env env, v8::Local<v8::String> v8String) { |
| 15 | + if (v8String.IsEmpty()) { |
| 16 | + return std::string(); |
| 17 | + } |
| 18 | + |
| 19 | + napi_value value = reinterpret_cast<napi_value>(*v8String); |
| 20 | + size_t length = 0; |
| 21 | + if (napi_get_value_string_utf8(env, value, nullptr, 0, &length) != napi_ok) { |
| 22 | + return std::string(); |
| 23 | + } |
| 24 | + |
| 25 | + std::string result(length, '\0'); |
| 26 | + if (napi_get_value_string_utf8(env, value, &result[0], length + 1, &length) != |
| 27 | + napi_ok) { |
| 28 | + return std::string(); |
| 29 | + } |
| 30 | + |
| 31 | + result.resize(length); |
| 32 | + return result; |
13 | 33 | } |
14 | 34 |
|
15 | 35 | #endif // LINUX_PERF_UTILS_H |
0 commit comments