Skip to content

Commit a0135b4

Browse files
committed
src: use v8::LocalVector consistently with other minor cleanups
V8 introduced `v8::LocalVector` somewhat recently as an alternative to using `std::vector<v8::Local<T>>` to help ensure that Local handles are handled correctly. This updates most (but not all) of our uses of `std::vector<v8::Local<T>>` to use `v8::LocalVector<T>` with a few other minor cleanups encountered along the way.
1 parent 9731d64 commit a0135b4

19 files changed

+122
-99
lines changed

src/cares_wrap.cc

+4-2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ namespace cares_wrap {
5959
using v8::Array;
6060
using v8::Context;
6161
using v8::EscapableHandleScope;
62+
using v8::Exception;
6263
using v8::FunctionCallbackInfo;
6364
using v8::FunctionTemplate;
6465
using v8::HandleScope;
@@ -68,6 +69,7 @@ using v8::Isolate;
6869
using v8::Just;
6970
using v8::JustVoid;
7071
using v8::Local;
72+
using v8::LocalVector;
7173
using v8::Maybe;
7274
using v8::Nothing;
7375
using v8::Null;
@@ -159,7 +161,7 @@ void ares_sockstate_cb(void* data, ares_socket_t sock, int read, int write) {
159161
Local<Array> HostentToNames(Environment* env, struct hostent* host) {
160162
EscapableHandleScope scope(env->isolate());
161163

162-
std::vector<Local<Value>> names;
164+
LocalVector<Value> names(env->isolate());
163165

164166
for (uint32_t i = 0; host->h_aliases[i] != nullptr; ++i)
165167
names.emplace_back(OneByteString(env->isolate(), host->h_aliases[i]));
@@ -1577,7 +1579,7 @@ void ConvertIpv6StringToBuffer(const FunctionCallbackInfo<Value>& args) {
15771579
unsigned char dst[16]; // IPv6 addresses are 128 bits (16 bytes)
15781580

15791581
if (uv_inet_pton(AF_INET6, *ip, dst) != 0) {
1580-
isolate->ThrowException(v8::Exception::Error(
1582+
isolate->ThrowException(Exception::Error(
15811583
String::NewFromUtf8(isolate, "Invalid IPv6 address").ToLocalChecked()));
15821584
return;
15831585
}

src/crypto/crypto_cipher.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ using v8::HandleScope;
2020
using v8::Int32;
2121
using v8::Isolate;
2222
using v8::Local;
23+
using v8::LocalVector;
2324
using v8::Object;
2425
using v8::Uint32;
2526
using v8::Value;
@@ -222,7 +223,7 @@ void CipherBase::GetSSLCiphers(const FunctionCallbackInfo<Value>& args) {
222223
};
223224

224225
const int n = sk_SSL_CIPHER_num(ciphers);
225-
std::vector<Local<Value>> arr(n + arraysize(TLS13_CIPHERS));
226+
LocalVector<Value> arr(env->isolate(), n + arraysize(TLS13_CIPHERS));
226227

227228
for (int i = 0; i < n; ++i) {
228229
const SSL_CIPHER* cipher = sk_SSL_CIPHER_value(ciphers, i);

src/crypto/crypto_ec.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ using v8::Int32;
2828
using v8::Isolate;
2929
using v8::JustVoid;
3030
using v8::Local;
31+
using v8::LocalVector;
3132
using v8::Maybe;
3233
using v8::MaybeLocal;
3334
using v8::Nothing;
@@ -95,7 +96,7 @@ void ECDH::GetCurves(const FunctionCallbackInfo<Value>& args) {
9596
std::vector<EC_builtin_curve> curves(num_curves);
9697
CHECK_EQ(EC_get_builtin_curves(curves.data(), num_curves), num_curves);
9798

98-
std::vector<Local<Value>> arr(num_curves);
99+
LocalVector<Value> arr(env->isolate(), num_curves);
99100
std::transform(curves.begin(), curves.end(), arr.begin(), [env](auto& curve) {
100101
return OneByteString(env->isolate(), OBJ_nid2sn(curve.nid));
101102
});

src/crypto/crypto_hash.cc

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ using v8::Isolate;
1919
using v8::Just;
2020
using v8::JustVoid;
2121
using v8::Local;
22+
using v8::LocalVector;
2223
using v8::Maybe;
2324
using v8::MaybeLocal;
2425
using v8::Name;
@@ -144,9 +145,9 @@ void Hash::GetCachedAliases(const FunctionCallbackInfo<Value>& args) {
144145
Isolate* isolate = args.GetIsolate();
145146
Local<Context> context = args.GetIsolate()->GetCurrentContext();
146147
Environment* env = Environment::GetCurrent(context);
147-
std::vector<Local<Name>> names;
148-
std::vector<Local<Value>> values;
149148
size_t size = env->alias_to_md_id_map.size();
149+
LocalVector<Name> names(isolate);
150+
LocalVector<Value> values(isolate);
150151
#if OPENSSL_VERSION_MAJOR >= 3
151152
names.reserve(size);
152153
values.reserve(size);

src/crypto/crypto_util.cc

+3-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ using v8::HandleScope;
3636
using v8::Isolate;
3737
using v8::JustVoid;
3838
using v8::Local;
39+
using v8::LocalVector;
3940
using v8::Maybe;
4041
using v8::MaybeLocal;
4142
using v8::NewStringType;
@@ -236,7 +237,7 @@ MaybeLocal<Value> cryptoErrorListToException(
236237
if (errors.size() > 1) {
237238
CHECK(exception->IsObject());
238239
Local<Object> exception_obj = exception.As<Object>();
239-
std::vector<Local<Value>> stack(errors.size() - 1);
240+
LocalVector<Value> stack(env->isolate(), errors.size() - 1);
240241

241242
// Iterate over all but the last error in the list.
242243
auto current = errors.begin();
@@ -252,7 +253,7 @@ MaybeLocal<Value> cryptoErrorListToException(
252253
}
253254

254255
Local<v8::Array> stackArray =
255-
v8::Array::New(env->isolate(), &stack[0], stack.size());
256+
v8::Array::New(env->isolate(), stack.data(), stack.size());
256257

257258
if (!exception_obj
258259
->Set(env->context(), env->openssl_error_stack(), stackArray)

src/internal_only_v8.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ using v8::FunctionCallbackInfo;
1010
using v8::Global;
1111
using v8::Isolate;
1212
using v8::Local;
13+
using v8::LocalVector;
1314
using v8::Object;
1415
using v8::Value;
1516

@@ -56,7 +57,7 @@ void QueryObjects(const FunctionCallbackInfo<Value>& args) {
5657
PrototypeChainHas prototype_chain_has(context, proto.As<Object>());
5758
std::vector<Global<Object>> out;
5859
isolate->GetHeapProfiler()->QueryObjects(context, &prototype_chain_has, &out);
59-
std::vector<Local<Value>> result;
60+
LocalVector<Value> result(isolate);
6061
result.reserve(out.size());
6162
for (size_t i = 0; i < out.size(); ++i) {
6263
result.push_back(out[i].Get(isolate));

src/module_wrap.cc

+47-38
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,35 @@ using node::contextify::ContextifyContext;
2323
using v8::Array;
2424
using v8::ArrayBufferView;
2525
using v8::Context;
26+
using v8::Data;
2627
using v8::EscapableHandleScope;
28+
using v8::Exception;
2729
using v8::FixedArray;
2830
using v8::Function;
2931
using v8::FunctionCallbackInfo;
3032
using v8::FunctionTemplate;
33+
using v8::Global;
3134
using v8::HandleScope;
3235
using v8::Int32;
3336
using v8::Integer;
3437
using v8::Isolate;
38+
using v8::Just;
3539
using v8::Local;
40+
using v8::LocalVector;
41+
using v8::Maybe;
3642
using v8::MaybeLocal;
3743
using v8::MemorySpan;
44+
using v8::Message;
3845
using v8::MicrotaskQueue;
3946
using v8::Module;
4047
using v8::ModuleRequest;
48+
using v8::Name;
49+
using v8::Null;
4150
using v8::Object;
4251
using v8::ObjectTemplate;
4352
using v8::PrimitiveArray;
4453
using v8::Promise;
54+
using v8::PromiseRejectEvent;
4555
using v8::ScriptCompiler;
4656
using v8::ScriptOrigin;
4757
using v8::String;
@@ -103,7 +113,7 @@ ModuleWrap* ModuleWrap::GetFromModule(Environment* env,
103113
return nullptr;
104114
}
105115

106-
v8::Maybe<bool> ModuleWrap::CheckUnsettledTopLevelAwait() {
116+
Maybe<bool> ModuleWrap::CheckUnsettledTopLevelAwait() {
107117
Isolate* isolate = env()->isolate();
108118
Local<Context> context = env()->context();
109119

@@ -115,17 +125,17 @@ v8::Maybe<bool> ModuleWrap::CheckUnsettledTopLevelAwait() {
115125
Local<Module> module = module_.Get(isolate);
116126
// It's a synthetic module, likely a facade wrapping CJS.
117127
if (!module->IsSourceTextModule()) {
118-
return v8::Just(true);
128+
return Just(true);
119129
}
120130

121131
if (!module->IsGraphAsync()) { // There is no TLA, no need to check.
122-
return v8::Just(true);
132+
return Just(true);
123133
}
124134

125135
auto stalled_messages =
126136
std::get<1>(module->GetStalledTopLevelAwaitMessages(isolate));
127137
if (stalled_messages.empty()) {
128-
return v8::Just(true);
138+
return Just(true);
129139
}
130140

131141
if (env()->options()->warnings) {
@@ -138,7 +148,7 @@ v8::Maybe<bool> ModuleWrap::CheckUnsettledTopLevelAwait() {
138148
}
139149
}
140150

141-
return v8::Just(false);
151+
return Just(false);
142152
}
143153

144154
Local<PrimitiveArray> ModuleWrap::GetHostDefinedOptions(
@@ -229,7 +239,7 @@ void ModuleWrap::New(const FunctionCallbackInfo<Value>& args) {
229239
Local<Array> export_names_arr = args[2].As<Array>();
230240

231241
uint32_t len = export_names_arr->Length();
232-
std::vector<Local<String>> export_names(len);
242+
LocalVector<String> export_names(realm->isolate(), len);
233243
for (uint32_t i = 0; i < len; i++) {
234244
Local<Value> export_name_val =
235245
export_names_arr->Get(context, i).ToLocalChecked();
@@ -245,7 +255,7 @@ void ModuleWrap::New(const FunctionCallbackInfo<Value>& args) {
245255
// When we are compiling for the default loader, this will be
246256
// std::nullopt, and CompileSourceTextModule() should use
247257
// on-disk cache.
248-
std::optional<v8::ScriptCompiler::CachedData*> user_cached_data;
258+
std::optional<ScriptCompiler::CachedData*> user_cached_data;
249259
if (id_symbol !=
250260
realm->isolate_data()->source_text_module_default_hdo()) {
251261
user_cached_data = nullptr;
@@ -324,7 +334,7 @@ void ModuleWrap::New(const FunctionCallbackInfo<Value>& args) {
324334
// be stored in an internal field.
325335
Local<Object> context_object = context->GetExtrasBindingObject();
326336
Local<Value> synthetic_evaluation_step =
327-
synthetic ? args[3] : Undefined(realm->isolate()).As<v8::Value>();
337+
synthetic ? args[3] : Undefined(realm->isolate()).As<Value>();
328338

329339
ModuleWrap* obj = new ModuleWrap(
330340
realm, that, module, url, context_object, synthetic_evaluation_step);
@@ -405,22 +415,22 @@ static Local<Object> createImportAttributesContainer(
405415
const int elements_per_attribute) {
406416
CHECK_EQ(raw_attributes->Length() % elements_per_attribute, 0);
407417
size_t num_attributes = raw_attributes->Length() / elements_per_attribute;
408-
std::vector<Local<v8::Name>> names(num_attributes);
409-
std::vector<Local<v8::Value>> values(num_attributes);
418+
LocalVector<Name> names(isolate, num_attributes);
419+
LocalVector<Value> values(isolate, num_attributes);
410420

411421
for (int i = 0; i < raw_attributes->Length(); i += elements_per_attribute) {
412422
int idx = i / elements_per_attribute;
413-
names[idx] = raw_attributes->Get(realm->context(), i).As<v8::Name>();
423+
names[idx] = raw_attributes->Get(realm->context(), i).As<Name>();
414424
values[idx] = raw_attributes->Get(realm->context(), i + 1).As<Value>();
415425
}
416426

417427
return Object::New(
418-
isolate, v8::Null(isolate), names.data(), values.data(), num_attributes);
428+
isolate, Null(isolate), names.data(), values.data(), num_attributes);
419429
}
420430

421431
static Local<Array> createModuleRequestsContainer(
422432
Realm* realm, Isolate* isolate, Local<FixedArray> raw_requests) {
423-
std::vector<Local<Value>> requests(raw_requests->Length());
433+
LocalVector<Value> requests(isolate, raw_requests->Length());
424434

425435
for (int i = 0; i < raw_requests->Length(); i++) {
426436
Local<ModuleRequest> module_request =
@@ -434,7 +444,7 @@ static Local<Array> createModuleRequestsContainer(
434444
Local<Object> attributes =
435445
createImportAttributesContainer(realm, isolate, raw_attributes, 3);
436446

437-
Local<v8::Name> names[] = {
447+
Local<Name> names[] = {
438448
realm->isolate_data()->specifier_string(),
439449
realm->isolate_data()->attributes_string(),
440450
};
@@ -444,8 +454,8 @@ static Local<Array> createModuleRequestsContainer(
444454
};
445455
DCHECK_EQ(arraysize(names), arraysize(values));
446456

447-
Local<Object> request = Object::New(
448-
isolate, v8::Null(isolate), names, values, arraysize(names));
457+
Local<Object> request =
458+
Object::New(isolate, Null(isolate), names, values, arraysize(names));
449459

450460
requests[i] = request;
451461
}
@@ -481,11 +491,11 @@ void ModuleWrap::Link(const FunctionCallbackInfo<Value>& args) {
481491
Local<Array> modules = args[1].As<Array>();
482492
CHECK_EQ(specifiers->Length(), modules->Length());
483493

484-
std::vector<v8::Global<Value>> specifiers_buffer;
494+
std::vector<Global<Value>> specifiers_buffer;
485495
if (FromV8Array(context, specifiers, &specifiers_buffer).IsNothing()) {
486496
return;
487497
}
488-
std::vector<v8::Global<Value>> modules_buffer;
498+
std::vector<Global<Value>> modules_buffer;
489499
if (FromV8Array(context, modules, &modules_buffer).IsNothing()) {
490500
return;
491501
}
@@ -669,19 +679,18 @@ void ModuleWrap::EvaluateSync(const FunctionCallbackInfo<Value>& args) {
669679
// before handler was added which would remove it from the unhandled
670680
// rejection handling, since we are converting it into an error and throw
671681
// from here directly.
672-
Local<Value> type = v8::Integer::New(
673-
isolate,
674-
static_cast<int32_t>(
675-
v8::PromiseRejectEvent::kPromiseHandlerAddedAfterReject));
682+
Local<Value> type =
683+
Integer::New(isolate,
684+
static_cast<int32_t>(
685+
PromiseRejectEvent::kPromiseHandlerAddedAfterReject));
676686
Local<Value> args[] = {type, promise, Undefined(isolate)};
677687
if (env->promise_reject_callback()
678688
->Call(context, Undefined(isolate), arraysize(args), args)
679689
.IsEmpty()) {
680690
return;
681691
}
682692
Local<Value> exception = promise->Result();
683-
Local<v8::Message> message =
684-
v8::Exception::CreateMessage(isolate, exception);
693+
Local<Message> message = Exception::CreateMessage(isolate, exception);
685694
AppendExceptionLine(
686695
env, exception, message, ErrorHandlingMode::MODULE_ERROR);
687696
isolate->ThrowException(exception);
@@ -718,15 +727,15 @@ void ModuleWrap::GetNamespaceSync(const FunctionCallbackInfo<Value>& args) {
718727
Local<Module> module = obj->module_.Get(isolate);
719728

720729
switch (module->GetStatus()) {
721-
case v8::Module::Status::kUninstantiated:
722-
case v8::Module::Status::kInstantiating:
730+
case Module::Status::kUninstantiated:
731+
case Module::Status::kInstantiating:
723732
return realm->env()->ThrowError(
724733
"Cannot get namespace, module has not been instantiated");
725-
case v8::Module::Status::kInstantiated:
726-
case v8::Module::Status::kEvaluated:
727-
case v8::Module::Status::kErrored:
734+
case Module::Status::kInstantiated:
735+
case Module::Status::kEvaluated:
736+
case Module::Status::kErrored:
728737
break;
729-
case v8::Module::Status::kEvaluating:
738+
case Module::Status::kEvaluating:
730739
UNREACHABLE();
731740
}
732741

@@ -746,14 +755,14 @@ void ModuleWrap::GetNamespace(const FunctionCallbackInfo<Value>& args) {
746755
Local<Module> module = obj->module_.Get(isolate);
747756

748757
switch (module->GetStatus()) {
749-
case v8::Module::Status::kUninstantiated:
750-
case v8::Module::Status::kInstantiating:
758+
case Module::Status::kUninstantiated:
759+
case Module::Status::kInstantiating:
751760
return realm->env()->ThrowError(
752761
"cannot get namespace, module has not been instantiated");
753-
case v8::Module::Status::kInstantiated:
754-
case v8::Module::Status::kEvaluating:
755-
case v8::Module::Status::kEvaluated:
756-
case v8::Module::Status::kErrored:
762+
case Module::Status::kInstantiated:
763+
case Module::Status::kEvaluating:
764+
case Module::Status::kEvaluated:
765+
case Module::Status::kErrored:
757766
break;
758767
default:
759768
UNREACHABLE();
@@ -825,7 +834,7 @@ MaybeLocal<Module> ModuleWrap::ResolveModuleCallback(
825834

826835
static MaybeLocal<Promise> ImportModuleDynamically(
827836
Local<Context> context,
828-
Local<v8::Data> host_defined_options,
837+
Local<Data> host_defined_options,
829838
Local<Value> resource_name,
830839
Local<String> specifier,
831840
Local<FixedArray> import_attributes) {
@@ -1011,7 +1020,7 @@ void ModuleWrap::CreateCachedData(const FunctionCallbackInfo<Value>& args) {
10111020

10121021
Local<Module> module = obj->module_.Get(isolate);
10131022

1014-
CHECK_LT(module->GetStatus(), v8::Module::Status::kEvaluating);
1023+
CHECK_LT(module->GetStatus(), Module::Status::kEvaluating);
10151024

10161025
Local<UnboundModuleScript> unbound_module_script =
10171026
module->GetUnboundModuleScript();

0 commit comments

Comments
 (0)