Skip to content

Commit 3f25b66

Browse files
committed
src: use Utf8Value and TwoByteValue instead of V8 helpers
Our own helper classes have the advantage of using stack storage a lot of the time, so they should always be preferred.
1 parent da9cd74 commit 3f25b66

File tree

12 files changed

+57
-50
lines changed

12 files changed

+57
-50
lines changed

src/api/async_resource.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ AsyncResource::AsyncResource(Isolate* isolate,
1616
Local<Object> resource,
1717
const char* name,
1818
async_id trigger_async_id)
19+
: AsyncResource(isolate, resource, std::string_view(name), trigger_async_id) {}
20+
21+
AsyncResource::AsyncResource(Isolate* isolate,
22+
Local<Object> resource,
23+
std::string_view name,
24+
async_id trigger_async_id)
1925
: env_(Environment::GetCurrent(isolate)),
2026
resource_(isolate, resource),
2127
context_frame_(isolate, async_context_frame::current(isolate)) {

src/api/hooks.cc

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,17 @@ async_context EmitAsyncInit(Isolate* isolate,
201201
Local<Object> resource,
202202
const char* name,
203203
async_id trigger_async_id) {
204+
return EmitAsyncInit(isolate, resource, std::string_view(name), trigger_async_id);
205+
}
206+
207+
async_context EmitAsyncInit(Isolate* isolate,
208+
Local<Object> resource,
209+
std::string_view name,
210+
async_id trigger_async_id) {
204211
HandleScope handle_scope(isolate);
205-
Local<String> type =
206-
String::NewFromUtf8(isolate, name, NewStringType::kInternalized)
207-
.ToLocalChecked();
212+
Local<String> type = ToV8Value(isolate->GetCurrentContext(), name, isolate)
213+
.ToLocalChecked()
214+
.As<String>();
208215
return EmitAsyncInit(isolate, resource, type, trigger_async_id);
209216
}
210217

src/inspector_js_api.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,8 @@ static void AsyncTaskScheduledWrapper(const FunctionCallbackInfo<Value>& args) {
240240

241241
CHECK(args[0]->IsString());
242242
Local<String> task_name = args[0].As<String>();
243-
String::Value task_name_value(args.GetIsolate(), task_name);
244-
StringView task_name_view(*task_name_value, task_name_value.length());
243+
TwoByteValue task_name_value(args.GetIsolate(), task_name);
244+
StringView task_name_view(task_name_value.out(), task_name_value.length());
245245

246246
CHECK(args[1]->IsNumber());
247247
int64_t task_id;

src/node.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,6 +1402,10 @@ NODE_EXTERN async_context EmitAsyncInit(v8::Isolate* isolate,
14021402
v8::Local<v8::Object> resource,
14031403
const char* name,
14041404
async_id trigger_async_id = -1);
1405+
NODE_EXTERN async_context EmitAsyncInit(v8::Isolate* isolate,
1406+
v8::Local<v8::Object> resource,
1407+
std::string_view name,
1408+
async_id trigger_async_id = -1);
14051409

14061410
NODE_EXTERN async_context EmitAsyncInit(v8::Isolate* isolate,
14071411
v8::Local<v8::Object> resource,
@@ -1508,6 +1512,10 @@ class NODE_EXTERN AsyncResource {
15081512
v8::Local<v8::Object> resource,
15091513
const char* name,
15101514
async_id trigger_async_id = -1);
1515+
AsyncResource(v8::Isolate* isolate,
1516+
v8::Local<v8::Object> resource,
1517+
std::string_view name,
1518+
async_id trigger_async_id = -1);
15111519

15121520
virtual ~AsyncResource();
15131521

src/node_api.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ class ThreadSafeFunction : public node::AsyncResource {
212212
napi_threadsafe_function_call_js call_js_cb_)
213213
: AsyncResource(env_->isolate,
214214
resource,
215-
*v8::String::Utf8Value(env_->isolate, name)),
215+
node::Utf8Value(env_->isolate, name).ToStringView()),
216216
thread_count(thread_count_),
217217
is_closing(false),
218218
dispatch_state(kDispatchIdle),
@@ -1150,7 +1150,7 @@ class Work : public node::AsyncResource, public node::ThreadPoolWork {
11501150
: AsyncResource(
11511151
env->isolate,
11521152
async_resource,
1153-
*v8::String::Utf8Value(env->isolate, async_resource_name)),
1153+
node::Utf8Value(env->isolate, async_resource_name).ToStringView()),
11541154
ThreadPoolWork(env->node_env(), "node_api"),
11551155
_env(env),
11561156
_data(data),

src/node_buffer.cc

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -986,11 +986,7 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) {
986986
size_t result = haystack_length;
987987

988988
if (enc == UCS2) {
989-
String::Value needle_value(isolate, needle);
990-
if (*needle_value == nullptr) {
991-
return args.GetReturnValue().Set(-1);
992-
}
993-
989+
TwoByteValue needle_value(isolate, needle);
994990
if (haystack_length < 2 || needle_value.length() < 1) {
995991
return args.GetReturnValue().Set(-1);
996992
}
@@ -1014,21 +1010,22 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) {
10141010
result =
10151011
nbytes::SearchString(reinterpret_cast<const uint16_t*>(haystack),
10161012
haystack_length / 2,
1017-
reinterpret_cast<const uint16_t*>(*needle_value),
1013+
needle_value.out(),
10181014
needle_value.length(),
10191015
offset / 2,
10201016
is_forward);
10211017
}
10221018
result *= 2;
10231019
} else if (enc == UTF8) {
1024-
String::Utf8Value needle_value(isolate, needle);
1020+
Utf8Value needle_value(isolate, needle);
10251021
if (*needle_value == nullptr)
10261022
return args.GetReturnValue().Set(-1);
1023+
CHECK_GE(needle_length, needle_value.length());
10271024

10281025
result =
10291026
nbytes::SearchString(reinterpret_cast<const uint8_t*>(haystack),
10301027
haystack_length,
1031-
reinterpret_cast<const uint8_t*>(*needle_value),
1028+
reinterpret_cast<const uint8_t*>(needle_value.out()),
10321029
needle_length,
10331030
offset,
10341031
is_forward);
@@ -1316,10 +1313,10 @@ static void Btoa(const FunctionCallbackInfo<Value>& args) {
13161313
input->Length(),
13171314
buffer.out());
13181315
} else {
1319-
String::Value value(env->isolate(), input);
1316+
TwoByteValue value(env->isolate(), input);
13201317
MaybeStackBuffer<char> stack_buf(value.length());
13211318
size_t out_len = simdutf::convert_utf16_to_latin1(
1322-
reinterpret_cast<const char16_t*>(*value),
1319+
reinterpret_cast<const char16_t*>(value.out()),
13231320
value.length(),
13241321
stack_buf.out());
13251322
if (out_len == 0) { // error
@@ -1370,8 +1367,8 @@ static void Atob(const FunctionCallbackInfo<Value>& args) {
13701367
buffer.SetLength(expected_length);
13711368
result = simdutf::base64_to_binary(data, input->Length(), buffer.out());
13721369
} else { // 16-bit case
1373-
String::Value value(env->isolate(), input);
1374-
auto data = reinterpret_cast<const char16_t*>(*value);
1370+
TwoByteValue value(env->isolate(), input);
1371+
auto data = reinterpret_cast<const char16_t*>(value.out());
13751372
size_t expected_length =
13761373
simdutf::maximal_binary_length_from_base64(data, value.length());
13771374
buffer.AllocateSufficientStorage(expected_length);

src/node_errors.cc

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,15 +1024,11 @@ void PerIsolateMessageListener(Local<Message> message, Local<Value> error) {
10241024
break;
10251025
}
10261026
Utf8Value filename(isolate, message->GetScriptOrigin().ResourceName());
1027+
Utf8Value msg(isolate, message->Get());
10271028
// (filename):(line) (message)
1028-
std::stringstream warning;
1029-
warning << *filename;
1030-
warning << ":";
1031-
warning << message->GetLineNumber(env->context()).FromMaybe(-1);
1032-
warning << " ";
1033-
v8::String::Utf8Value msg(isolate, message->Get());
1034-
warning << *msg;
1035-
USE(ProcessEmitWarningGeneric(env, warning.str().c_str(), "V8"));
1029+
std::string warning = SPrintF("%s:%s %s",
1030+
filename, message->GetLineNumber(env->context()).FromMaybe(-1), msg);
1031+
USE(ProcessEmitWarningGeneric(env, warning, "V8"));
10361032
break;
10371033
}
10381034
case Isolate::MessageErrorLevel::kMessageError:

src/node_report.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,8 +455,7 @@ static Maybe<std::string> ErrorToString(Isolate* isolate,
455455
if (!maybe_str.ToLocal(&js_str)) {
456456
return Nothing<std::string>();
457457
}
458-
String::Utf8Value sv(isolate, js_str);
459-
return Just<>(std::string(*sv, sv.length()));
458+
return Just(Utf8Value(isolate, js_str).ToString());
460459
}
461460

462461
static void PrintEmptyJavaScriptStack(JSONWriter* writer) {

src/node_sqlite.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,8 +1508,7 @@ void DatabaseSync::CreateSession(const FunctionCallbackInfo<Value>& args) {
15081508
}
15091509

15101510
if (table_value->IsString()) {
1511-
String::Utf8Value str(env->isolate(), table_value);
1512-
table = *str;
1511+
table = Utf8Value(env->isolate(), table_value).ToString();
15131512
} else {
15141513
THROW_ERR_INVALID_ARG_TYPE(
15151514
env->isolate(), "The \"options.table\" argument must be a string.");
@@ -1529,8 +1528,7 @@ void DatabaseSync::CreateSession(const FunctionCallbackInfo<Value>& args) {
15291528
return;
15301529
}
15311530
if (db_value->IsString()) {
1532-
String::Utf8Value str(env->isolate(), db_value);
1533-
db_name = std::string(*str);
1531+
db_name = Utf8Value(env->isolate(), db_value).ToString();
15341532
} else {
15351533
THROW_ERR_INVALID_ARG_TYPE(
15361534
env->isolate(), "The \"options.db\" argument must be a string.");

src/node_v8.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,8 @@ void UpdateHeapCodeStatisticsBuffer(const FunctionCallbackInfo<Value>& args) {
242242

243243
void SetFlagsFromString(const FunctionCallbackInfo<Value>& args) {
244244
CHECK(args[0]->IsString());
245-
String::Utf8Value flags(args.GetIsolate(), args[0]);
246-
V8::SetFlagsFromString(*flags, static_cast<size_t>(flags.length()));
245+
Utf8Value flags(args.GetIsolate(), args[0]);
246+
V8::SetFlagsFromString(flags.out(), flags.length());
247247
}
248248

249249
void StartCpuProfile(const FunctionCallbackInfo<Value>& args) {

0 commit comments

Comments
 (0)