Skip to content

Commit 4e5f054

Browse files
committed
src: fixup lint issues after dictionary template change
1 parent 8388688 commit 4e5f054

File tree

7 files changed

+127
-65
lines changed

7 files changed

+127
-65
lines changed

src/node_sqlite.cc

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ namespace {
124124
Local<DictionaryTemplate> getLazyIterTemplate(Environment* env) {
125125
auto iter_template = env->iter_template();
126126
if (iter_template.IsEmpty()) {
127-
std::string_view iter_keys[] = {"done", "value"};
127+
static constexpr std::string_view iter_keys[] = {"done", "value"};
128128
iter_template = DictionaryTemplate::New(env->isolate(), iter_keys);
129129
env->set_iter_template(iter_template);
130130
}
@@ -2254,12 +2254,11 @@ void StatementSync::Columns(const FunctionCallbackInfo<Value>& args) {
22542254
LocalVector<Value> cols(isolate);
22552255
auto sqlite_column_template = env->sqlite_column_template();
22562256
if (sqlite_column_template.IsEmpty()) {
2257-
std::string_view col_keys[] = {
2257+
static constexpr std::string_view col_keys[] = {
22582258
"column", "database", "name", "table", "type"};
22592259
sqlite_column_template = DictionaryTemplate::New(isolate, col_keys);
22602260
env->set_sqlite_column_template(sqlite_column_template);
22612261
}
2262-
Local<Value> value;
22632262

22642263
cols.reserve(num_cols);
22652264
for (int i = 0; i < num_cols; ++i) {
@@ -2275,8 +2274,12 @@ void StatementSync::Columns(const FunctionCallbackInfo<Value>& args) {
22752274
isolate, sqlite3_column_decltype(stmt->statement_, i)),
22762275
};
22772276

2278-
cols.emplace_back(
2279-
sqlite_column_template->NewInstance(env->context(), values));
2277+
Local<Object> col;
2278+
if (!NewDictionaryInstance(env->context(), sqlite_column_template, values)
2279+
.ToLocal(&col)) {
2280+
return;
2281+
}
2282+
cols.emplace_back(col);
22802283
}
22812284

22822285
args.GetReturnValue().Set(Array::New(isolate, cols.data(), cols.size()));
@@ -2516,8 +2519,11 @@ void StatementSyncIterator::Next(const FunctionCallbackInfo<Value>& args) {
25162519
Boolean::New(isolate, true),
25172520
Null(isolate),
25182521
};
2519-
args.GetReturnValue().Set(
2520-
iter_template->NewInstance(env->context(), values));
2522+
Local<Object> result;
2523+
if (NewDictionaryInstance(env->context(), iter_template, values)
2524+
.ToLocal(&result)) {
2525+
args.GetReturnValue().Set(result);
2526+
}
25212527
return;
25222528
}
25232529

@@ -2527,8 +2533,11 @@ void StatementSyncIterator::Next(const FunctionCallbackInfo<Value>& args) {
25272533
env->isolate(), iter->stmt_->db_.get(), r, SQLITE_DONE, void());
25282534
sqlite3_reset(iter->stmt_->statement_);
25292535
MaybeLocal<Value> values[] = {Boolean::New(isolate, true), Null(isolate)};
2530-
args.GetReturnValue().Set(
2531-
iter_template->NewInstance(env->context(), values));
2536+
Local<Object> result;
2537+
if (NewDictionaryInstance(env->context(), iter_template, values)
2538+
.ToLocal(&result)) {
2539+
args.GetReturnValue().Set(result);
2540+
}
25322541
return;
25332542
}
25342543

@@ -2557,7 +2566,11 @@ void StatementSyncIterator::Next(const FunctionCallbackInfo<Value>& args) {
25572566
}
25582567

25592568
MaybeLocal<Value> values[] = {Boolean::New(isolate, false), row_value};
2560-
args.GetReturnValue().Set(iter_template->NewInstance(env->context(), values));
2569+
Local<Object> result;
2570+
if (NewDictionaryInstance(env->context(), iter_template, values)
2571+
.ToLocal(&result)) {
2572+
args.GetReturnValue().Set(result);
2573+
}
25612574
}
25622575

25632576
void StatementSyncIterator::Return(const FunctionCallbackInfo<Value>& args) {
@@ -2574,8 +2587,11 @@ void StatementSyncIterator::Return(const FunctionCallbackInfo<Value>& args) {
25742587
auto iter_template = getLazyIterTemplate(env);
25752588
MaybeLocal<Value> values[] = {Boolean::New(isolate, true), Null(isolate)};
25762589

2577-
Local<Object> result = iter_template->NewInstance(env->context(), values);
2578-
args.GetReturnValue().Set(result);
2590+
Local<Object> result;
2591+
if (NewDictionaryInstance(env->context(), iter_template, values)
2592+
.ToLocal(&result)) {
2593+
args.GetReturnValue().Set(result);
2594+
}
25792595
}
25802596

25812597
Session::Session(Environment* env,

src/node_url_pattern.cc

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ using v8::FunctionTemplate;
6161
using v8::Global;
6262
using v8::Isolate;
6363
using v8::Local;
64-
using v8::LocalVector;
6564
using v8::MaybeLocal;
6665
using v8::Name;
6766
using v8::NewStringType;
@@ -400,7 +399,7 @@ MaybeLocal<Value> URLPattern::URLPatternResult::ToJSValue(
400399

401400
auto tmpl = env->urlpatternresult_template();
402401
if (tmpl.IsEmpty()) {
403-
std::string_view namesVec[] = {
402+
static constexpr std::string_view namesVec[] = {
404403
"inputs",
405404
"protocol",
406405
"username",
@@ -416,10 +415,8 @@ MaybeLocal<Value> URLPattern::URLPatternResult::ToJSValue(
416415
}
417416

418417
size_t index = 0;
419-
auto context = isolate->GetCurrentContext();
420-
421418
MaybeLocal<Value> vals[] = {
422-
Array::New(context,
419+
Array::New(env->context(),
423420
result.inputs.size(),
424421
[&index, &inputs = result.inputs, env]() {
425422
auto& input = inputs[index++];
@@ -440,9 +437,8 @@ MaybeLocal<Value> URLPattern::URLPatternResult::ToJSValue(
440437
URLPatternComponentResult::ToJSObject(env, result.port),
441438
URLPatternComponentResult::ToJSObject(env, result.pathname),
442439
URLPatternComponentResult::ToJSObject(env, result.search),
443-
URLPatternComponentResult::ToJSObject(env, result.hash),
444-
};
445-
return tmpl->NewInstance(env->context(), vals);
440+
URLPatternComponentResult::ToJSObject(env, result.hash)};
441+
return NewDictionaryInstance(env->context(), tmpl, vals);
446442
}
447443

448444
std::optional<ada::url_pattern_options>

src/node_util.cc

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -267,13 +267,14 @@ static void GetCallSites(const FunctionCallbackInfo<Value>& args) {
267267

268268
auto callsite_template = env->callsite_template();
269269
if (callsite_template.IsEmpty()) {
270-
std::string_view names[] = {"functionName",
271-
"scriptId",
272-
"scriptName",
273-
"lineNumber",
274-
"columnNumber",
275-
// TODO(legendecas): deprecate CallSite.column.
276-
"column"};
270+
static constexpr std::string_view names[] = {
271+
"functionName",
272+
"scriptId",
273+
"scriptName",
274+
"lineNumber",
275+
"columnNumber",
276+
// TODO(legendecas): deprecate CallSite.column.
277+
"column"};
277278
callsite_template = DictionaryTemplate::New(isolate, names);
278279
env->set_callsite_template(callsite_template);
279280
}
@@ -304,8 +305,12 @@ static void GetCallSites(const FunctionCallbackInfo<Value>& args) {
304305
Integer::NewFromUnsigned(isolate, stack_frame->GetColumn()),
305306
};
306307

307-
callsite_objects.push_back(
308-
callsite_template->NewInstance(env->context(), values));
308+
Local<Object> callsite;
309+
if (!NewDictionaryInstance(env->context(), callsite_template, values)
310+
.ToLocal(&callsite)) {
311+
return;
312+
}
313+
callsite_objects.push_back(callsite);
309314
}
310315

311316
Local<Array> callsites =

src/node_v8.cc

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ using v8::Isolate;
4747
using v8::Local;
4848
using v8::LocalVector;
4949
using v8::MaybeLocal;
50-
using v8::Name;
5150
using v8::Object;
5251
using v8::ScriptCompiler;
5352
using v8::String;
@@ -338,16 +337,18 @@ static MaybeLocal<Object> ConvertHeapStatsToJSObject(
338337
auto space_stats_tmpl = env->space_stats_template();
339338
auto heap_stats_tmpl = env->v8_heap_statistics_template();
340339
if (object_stats_template.IsEmpty()) {
341-
std::string_view object_stats_names[] = {"allocated_bytes", "object_count"};
340+
static constexpr std::string_view object_stats_names[] = {"allocated_bytes",
341+
"object_count"};
342342
object_stats_template =
343343
DictionaryTemplate::New(isolate, object_stats_names);
344344
env->set_object_stats_template(object_stats_template);
345345
}
346346
if (page_stats_tmpl.IsEmpty()) {
347-
std::string_view page_stats_names[] = {"committed_size_bytes",
348-
"resident_size_bytes",
349-
"used_size_bytes",
350-
"object_statistics"};
347+
static constexpr std::string_view page_stats_names[] = {
348+
"committed_size_bytes",
349+
"resident_size_bytes",
350+
"used_size_bytes",
351+
"object_statistics"};
351352
page_stats_tmpl = DictionaryTemplate::New(isolate, page_stats_names);
352353
env->set_page_stats_template(page_stats_tmpl);
353354
}
@@ -359,21 +360,23 @@ static MaybeLocal<Object> ConvertHeapStatsToJSObject(
359360
env->set_free_list_statistics_template(free_list_statistics_template);
360361
}
361362
if (space_stats_tmpl.IsEmpty()) {
362-
std::string_view space_stats_names[] = {"name",
363-
"committed_size_bytes",
364-
"resident_size_bytes",
365-
"used_size_bytes",
366-
"page_stats",
367-
"free_list_stats"};
363+
static constexpr std::string_view space_stats_names[] = {
364+
"name",
365+
"committed_size_bytes",
366+
"resident_size_bytes",
367+
"used_size_bytes",
368+
"page_stats",
369+
"free_list_stats"};
368370
space_stats_tmpl = DictionaryTemplate::New(isolate, space_stats_names);
369371
env->set_space_stats_template(space_stats_tmpl);
370372
}
371373
if (heap_stats_tmpl.IsEmpty()) {
372-
std::string_view heap_statistics_names[] = {"committed_size_bytes",
373-
"resident_size_bytes",
374-
"used_size_bytes",
375-
"space_statistics",
376-
"type_names"};
374+
static constexpr std::string_view heap_statistics_names[] = {
375+
"committed_size_bytes",
376+
"resident_size_bytes",
377+
"used_size_bytes",
378+
"space_statistics",
379+
"type_names"};
377380
heap_stats_tmpl = DictionaryTemplate::New(isolate, heap_statistics_names);
378381
env->set_v8_heap_statistics_template(heap_stats_tmpl);
379382
}
@@ -398,8 +401,12 @@ static MaybeLocal<Object> ConvertHeapStatsToJSObject(
398401
isolate, static_cast<uint32_t>(object_stats.allocated_bytes)),
399402
Uint32::NewFromUnsigned(
400403
isolate, static_cast<uint32_t>(object_stats.object_count))};
401-
Local<Object> object_stats_object =
402-
object_stats_template->NewInstance(context, object_stats_values);
404+
Local<Object> object_stats_object;
405+
if (!NewDictionaryInstance(
406+
context, object_stats_template, object_stats_values)
407+
.ToLocal(&object_stats_object)) {
408+
return MaybeLocal<Object>();
409+
}
403410
object_statistics_array.emplace_back(object_stats_object);
404411
}
405412

@@ -414,8 +421,12 @@ static MaybeLocal<Object> ConvertHeapStatsToJSObject(
414421
Array::New(isolate,
415422
object_statistics_array.data(),
416423
object_statistics_array.size())};
417-
page_statistics_array.emplace_back(
418-
page_stats_tmpl->NewInstance(context, page_stats_values));
424+
Local<Object> page_stats_object;
425+
if (!NewDictionaryInstance(context, page_stats_tmpl, page_stats_values)
426+
.ToLocal(&page_stats_object)) {
427+
return MaybeLocal<Object>();
428+
}
429+
page_statistics_array.emplace_back(page_stats_object);
419430
}
420431

421432
// Free List Statistics
@@ -427,9 +438,13 @@ static MaybeLocal<Object> ConvertHeapStatsToJSObject(
427438
ToV8ValuePrimitiveArray(
428439
context, space_stats.free_list_stats.free_size, isolate)};
429440

430-
Local<Object> free_list_statistics_obj =
431-
free_list_statistics_template->NewInstance(context,
432-
free_list_statistics_values);
441+
Local<Object> free_list_statistics_obj;
442+
if (!NewDictionaryInstance(context,
443+
free_list_statistics_template,
444+
free_list_statistics_values)
445+
.ToLocal(&free_list_statistics_obj)) {
446+
return MaybeLocal<Object>();
447+
}
433448

434449
// Set Space Statistics
435450
Local<Value> name_value;
@@ -453,8 +468,12 @@ static MaybeLocal<Object> ConvertHeapStatsToJSObject(
453468
page_statistics_array.size()),
454469
free_list_statistics_obj,
455470
};
456-
space_statistics_array.emplace_back(
457-
space_stats_tmpl->NewInstance(context, space_stats_values));
471+
Local<Object> space_stats_object;
472+
if (!NewDictionaryInstance(context, space_stats_tmpl, space_stats_values)
473+
.ToLocal(&space_stats_object)) {
474+
return MaybeLocal<Object>();
475+
}
476+
space_statistics_array.emplace_back(space_stats_object);
458477
}
459478

460479
Local<Value> type_names_value;
@@ -474,7 +493,8 @@ static MaybeLocal<Object> ConvertHeapStatsToJSObject(
474493
space_statistics_array.size()),
475494
type_names_value};
476495

477-
return heap_stats_tmpl->NewInstance(context, heap_statistics_values);
496+
return NewDictionaryInstance(
497+
context, heap_stats_tmpl, heap_statistics_values);
478498
}
479499

480500
static void GetCppHeapStatistics(const FunctionCallbackInfo<Value>& args) {

src/node_worker.cc

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ using v8::Float64Array;
3232
using v8::FunctionCallbackInfo;
3333
using v8::FunctionTemplate;
3434
using v8::HandleScope;
35+
using v8::HeapStatistics;
3536
using v8::Integer;
3637
using v8::Isolate;
3738
using v8::Local;
3839
using v8::Locker;
3940
using v8::Maybe;
4041
using v8::MaybeLocal;
41-
using v8::Name;
4242
using v8::NewStringType;
4343
using v8::Null;
4444
using v8::Number;
@@ -878,7 +878,7 @@ void Worker::CpuUsage(const FunctionCallbackInfo<Value>& args) {
878878
} else {
879879
auto tmpl = env->cpu_usage_template();
880880
if (tmpl.IsEmpty()) {
881-
std::string_view names[] = {
881+
static constexpr std::string_view names[] = {
882882
"user",
883883
"system",
884884
};
@@ -894,7 +894,10 @@ void Worker::CpuUsage(const FunctionCallbackInfo<Value>& args) {
894894
1e6 * cpu_usage_stats->ru_stime.tv_sec +
895895
cpu_usage_stats->ru_stime.tv_usec),
896896
};
897-
argv[1] = tmpl->NewInstance(env->context(), values);
897+
if (!NewDictionaryInstance(env->context(), tmpl, values)
898+
.ToLocal(&argv[1])) {
899+
return;
900+
}
898901
}
899902

900903
taker->MakeCallback(env->ondone_string(), arraysize(argv), argv);
@@ -1063,7 +1066,7 @@ void Worker::GetHeapStatistics(const FunctionCallbackInfo<Value>& args) {
10631066
env](Environment* worker_env) mutable {
10641067
// We create a unique pointer to HeapStatistics so that the actual object
10651068
// it's not copied in the lambda, but only the pointer is.
1066-
auto heap_stats = std::make_unique<v8::HeapStatistics>();
1069+
auto heap_stats = std::make_unique<HeapStatistics>();
10671070
worker_env->isolate()->GetHeapStatistics(heap_stats.get());
10681071

10691072
// Here, the worker thread temporarily owns the WorkerHeapStatisticsTaker
@@ -1096,7 +1099,7 @@ void Worker::GetHeapStatistics(const FunctionCallbackInfo<Value>& args) {
10961099
"used_global_handles_size",
10971100
"external_memory",
10981101
};
1099-
tmpl = v8::DictionaryTemplate::New(isolate, heap_stats_names);
1102+
tmpl = DictionaryTemplate::New(isolate, heap_stats_names);
11001103
env->set_heap_statistics_template(tmpl);
11011104
}
11021105

@@ -1117,10 +1120,12 @@ void Worker::GetHeapStatistics(const FunctionCallbackInfo<Value>& args) {
11171120
Number::New(isolate, heap_stats->used_global_handles_size()),
11181121
Number::New(isolate, heap_stats->external_memory())};
11191122

1120-
DCHECK_EQ(arraysize(heap_stats_names), arraysize(heap_stats_values));
1121-
1122-
Local<Value> args[] = {
1123-
tmpl->NewInstance(env->context(), heap_stats_values)};
1123+
Local<Object> obj;
1124+
if (!NewDictionaryInstance(env->context(), tmpl, heap_stats_values)
1125+
.ToLocal(&obj)) {
1126+
return;
1127+
}
1128+
Local<Value> args[] = {obj};
11241129
taker->get()->MakeCallback(
11251130
env->ondone_string(), arraysize(args), args);
11261131
// implicitly delete `taker`

src/util-inl.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,16 @@ inline std::wstring ConvertToWideString(const std::string& str,
704704
}
705705
#endif // _WIN32
706706

707+
inline v8::MaybeLocal<v8::Object> NewDictionaryInstance(
708+
v8::Local<v8::Context> context,
709+
v8::Local<v8::DictionaryTemplate> tmpl,
710+
v8::MemorySpan<v8::MaybeLocal<v8::Value>> property_values) {
711+
for (auto& value : property_values) {
712+
if (value.IsEmpty()) return v8::MaybeLocal<v8::Object>();
713+
}
714+
return tmpl->NewInstance(context, property_values);
715+
}
716+
707717
} // namespace node
708718

709719
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

0 commit comments

Comments
 (0)