Skip to content

Commit c95d974

Browse files
addaleaxaduh95
authored andcommitted
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. PR-URL: #60244 Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com> Reviewed-By: Edy Silva <edigleyssonsilva@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ilyas Shabi <ilyasshabi94@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Vladimir Morozov <vmorozov@microsoft.com>
1 parent 51adaaa commit c95d974

File tree

12 files changed

+73
-62
lines changed

12 files changed

+73
-62
lines changed

src/api/async_resource.cc

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

src/api/hooks.cc

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,18 @@ async_context EmitAsyncInit(Isolate* isolate,
213213
Local<Object> resource,
214214
const char* name,
215215
async_id trigger_async_id) {
216+
return EmitAsyncInit(
217+
isolate, resource, std::string_view(name), trigger_async_id);
218+
}
219+
220+
async_context EmitAsyncInit(Isolate* isolate,
221+
Local<Object> resource,
222+
std::string_view name,
223+
async_id trigger_async_id) {
216224
HandleScope handle_scope(isolate);
217225
Local<String> type =
218-
String::NewFromUtf8(isolate, name, NewStringType::kInternalized)
226+
String::NewFromUtf8(
227+
isolate, name.data(), NewStringType::kInternalized, name.size())
219228
.ToLocalChecked();
220229
return EmitAsyncInit(isolate, resource, type, trigger_async_id);
221230
}

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
@@ -1458,6 +1458,10 @@ NODE_EXTERN async_context EmitAsyncInit(v8::Isolate* isolate,
14581458
v8::Local<v8::Object> resource,
14591459
const char* name,
14601460
async_id trigger_async_id = -1);
1461+
NODE_EXTERN async_context EmitAsyncInit(v8::Isolate* isolate,
1462+
v8::Local<v8::Object> resource,
1463+
std::string_view name,
1464+
async_id trigger_async_id = -1);
14611465

14621466
NODE_EXTERN async_context EmitAsyncInit(v8::Isolate* isolate,
14631467
v8::Local<v8::Object> resource,
@@ -1553,6 +1557,10 @@ class NODE_EXTERN AsyncResource {
15531557
v8::Local<v8::Object> resource,
15541558
const char* name,
15551559
async_id trigger_async_id = -1);
1560+
AsyncResource(v8::Isolate* isolate,
1561+
v8::Local<v8::Object> resource,
1562+
std::string_view name,
1563+
async_id trigger_async_id = -1);
15561564

15571565
virtual ~AsyncResource();
15581566

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),
@@ -1176,7 +1176,7 @@ class Work : public node::AsyncResource, public node::ThreadPoolWork {
11761176
: AsyncResource(
11771177
env->isolate,
11781178
async_resource,
1179-
*v8::String::Utf8Value(env->isolate, async_resource_name)),
1179+
node::Utf8Value(env->isolate, async_resource_name).ToStringView()),
11801180
ThreadPoolWork(env->node_env(), "node_api"),
11811181
_env(env),
11821182
_data(data),

src/node_buffer.cc

Lines changed: 21 additions & 25 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
}
@@ -1011,27 +1007,27 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) {
10111007
offset / 2,
10121008
is_forward);
10131009
} else {
1014-
result =
1015-
nbytes::SearchString(reinterpret_cast<const uint16_t*>(haystack),
1016-
haystack_length / 2,
1017-
reinterpret_cast<const uint16_t*>(*needle_value),
1018-
needle_value.length(),
1019-
offset / 2,
1020-
is_forward);
1010+
result = nbytes::SearchString(reinterpret_cast<const uint16_t*>(haystack),
1011+
haystack_length / 2,
1012+
needle_value.out(),
1013+
needle_value.length(),
1014+
offset / 2,
1015+
is_forward);
10211016
}
10221017
result *= 2;
10231018
} else if (enc == UTF8) {
1024-
String::Utf8Value needle_value(isolate, needle);
1019+
Utf8Value needle_value(isolate, needle);
10251020
if (*needle_value == nullptr)
10261021
return args.GetReturnValue().Set(-1);
1027-
1028-
result =
1029-
nbytes::SearchString(reinterpret_cast<const uint8_t*>(haystack),
1030-
haystack_length,
1031-
reinterpret_cast<const uint8_t*>(*needle_value),
1032-
needle_length,
1033-
offset,
1034-
is_forward);
1022+
CHECK_GE(needle_length, needle_value.length());
1023+
1024+
result = nbytes::SearchString(
1025+
reinterpret_cast<const uint8_t*>(haystack),
1026+
haystack_length,
1027+
reinterpret_cast<const uint8_t*>(needle_value.out()),
1028+
needle_length,
1029+
offset,
1030+
is_forward);
10351031
} else if (enc == LATIN1) {
10361032
uint8_t* needle_data = node::UncheckedMalloc<uint8_t>(needle_length);
10371033
if (needle_data == nullptr) {
@@ -1316,10 +1312,10 @@ static void Btoa(const FunctionCallbackInfo<Value>& args) {
13161312
input->Length(),
13171313
buffer.out());
13181314
} else {
1319-
String::Value value(env->isolate(), input);
1315+
TwoByteValue value(env->isolate(), input);
13201316
MaybeStackBuffer<char> stack_buf(value.length());
13211317
size_t out_len = simdutf::convert_utf16_to_latin1(
1322-
reinterpret_cast<const char16_t*>(*value),
1318+
reinterpret_cast<const char16_t*>(value.out()),
13231319
value.length(),
13241320
stack_buf.out());
13251321
if (out_len == 0) { // error
@@ -1370,8 +1366,8 @@ static void Atob(const FunctionCallbackInfo<Value>& args) {
13701366
buffer.SetLength(expected_length);
13711367
result = simdutf::base64_to_binary(data, input->Length(), buffer.out());
13721368
} else { // 16-bit case
1373-
String::Value value(env->isolate(), input);
1374-
auto data = reinterpret_cast<const char16_t*>(*value);
1369+
TwoByteValue value(env->isolate(), input);
1370+
auto data = reinterpret_cast<const char16_t*>(value.out());
13751371
size_t expected_length =
13761372
simdutf::maximal_binary_length_from_base64(data, value.length());
13771373
buffer.AllocateSufficientStorage(expected_length);

src/node_errors.cc

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,15 +1024,14 @@ 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 =
1030+
SPrintF("%s:%s %s",
1031+
filename,
1032+
message->GetLineNumber(env->context()).FromMaybe(-1),
1033+
msg);
1034+
USE(ProcessEmitWarningGeneric(env, warning, "V8"));
10361035
break;
10371036
}
10381037
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)