From 711062c368442e0f7ce3842715b040c9003ea20d Mon Sep 17 00:00:00 2001 From: James M Snell Date: Mon, 30 Dec 2024 16:30:43 -0800 Subject: [PATCH] src: use v8::LocalVector consistently with other minor cleanups MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit V8 introduced `v8::LocalVector` somewhat recently as an alternative to using `std::vector>` to help ensure that Local handles are handled correctly. This updates most (but not all) of our uses of `std::vector>` to use `v8::LocalVector` with a few other minor cleanups encountered along the way. Apply suggestions from code review Co-authored-by: Michaƫl Zasso --- src/cares_wrap.cc | 6 ++- src/crypto/crypto_cipher.cc | 3 +- src/crypto/crypto_ec.cc | 3 +- src/crypto/crypto_hash.cc | 5 ++- src/crypto/crypto_util.cc | 5 ++- src/internal_only_v8.cc | 3 +- src/module_wrap.cc | 85 ++++++++++++++++++++----------------- src/node_builtins.cc | 40 ++++++++--------- src/node_contextify.cc | 9 ++-- src/node_env_var.cc | 3 +- src/node_file.cc | 9 ++-- src/node_http2.cc | 3 +- src/node_http_parser.cc | 13 +++--- src/node_modules.cc | 7 +-- src/node_options.cc | 5 ++- src/node_os.cc | 5 ++- src/node_sockaddr.cc | 8 ++-- src/node_sockaddr.h | 4 +- src/node_webstorage.cc | 3 +- 19 files changed, 122 insertions(+), 97 deletions(-) diff --git a/src/cares_wrap.cc b/src/cares_wrap.cc index 00fe28d746d61c..a19e8221f34eba 100644 --- a/src/cares_wrap.cc +++ b/src/cares_wrap.cc @@ -59,6 +59,7 @@ namespace cares_wrap { using v8::Array; using v8::Context; using v8::EscapableHandleScope; +using v8::Exception; using v8::FunctionCallbackInfo; using v8::FunctionTemplate; using v8::HandleScope; @@ -68,6 +69,7 @@ using v8::Isolate; using v8::Just; using v8::JustVoid; using v8::Local; +using v8::LocalVector; using v8::Maybe; using v8::Nothing; using v8::Null; @@ -159,7 +161,7 @@ void ares_sockstate_cb(void* data, ares_socket_t sock, int read, int write) { Local HostentToNames(Environment* env, struct hostent* host) { EscapableHandleScope scope(env->isolate()); - std::vector> names; + LocalVector names(env->isolate()); for (uint32_t i = 0; host->h_aliases[i] != nullptr; ++i) names.emplace_back(OneByteString(env->isolate(), host->h_aliases[i])); @@ -1577,7 +1579,7 @@ void ConvertIpv6StringToBuffer(const FunctionCallbackInfo& args) { unsigned char dst[16]; // IPv6 addresses are 128 bits (16 bytes) if (uv_inet_pton(AF_INET6, *ip, dst) != 0) { - isolate->ThrowException(v8::Exception::Error( + isolate->ThrowException(Exception::Error( String::NewFromUtf8(isolate, "Invalid IPv6 address").ToLocalChecked())); return; } diff --git a/src/crypto/crypto_cipher.cc b/src/crypto/crypto_cipher.cc index 51e311be705393..21e34333609880 100644 --- a/src/crypto/crypto_cipher.cc +++ b/src/crypto/crypto_cipher.cc @@ -20,6 +20,7 @@ using v8::HandleScope; using v8::Int32; using v8::Isolate; using v8::Local; +using v8::LocalVector; using v8::Object; using v8::Uint32; using v8::Value; @@ -222,7 +223,7 @@ void CipherBase::GetSSLCiphers(const FunctionCallbackInfo& args) { }; const int n = sk_SSL_CIPHER_num(ciphers); - std::vector> arr(n + arraysize(TLS13_CIPHERS)); + LocalVector arr(env->isolate(), n + arraysize(TLS13_CIPHERS)); for (int i = 0; i < n; ++i) { const SSL_CIPHER* cipher = sk_SSL_CIPHER_value(ciphers, i); diff --git a/src/crypto/crypto_ec.cc b/src/crypto/crypto_ec.cc index a42a336baedf09..78df3ae14407e3 100644 --- a/src/crypto/crypto_ec.cc +++ b/src/crypto/crypto_ec.cc @@ -28,6 +28,7 @@ using v8::Int32; using v8::Isolate; using v8::JustVoid; using v8::Local; +using v8::LocalVector; using v8::Maybe; using v8::MaybeLocal; using v8::Nothing; @@ -95,7 +96,7 @@ void ECDH::GetCurves(const FunctionCallbackInfo& args) { std::vector curves(num_curves); CHECK_EQ(EC_get_builtin_curves(curves.data(), num_curves), num_curves); - std::vector> arr(num_curves); + LocalVector arr(env->isolate(), num_curves); std::transform(curves.begin(), curves.end(), arr.begin(), [env](auto& curve) { return OneByteString(env->isolate(), OBJ_nid2sn(curve.nid)); }); diff --git a/src/crypto/crypto_hash.cc b/src/crypto/crypto_hash.cc index 23e5fea262f2a6..5fd20c2fd3968f 100644 --- a/src/crypto/crypto_hash.cc +++ b/src/crypto/crypto_hash.cc @@ -19,6 +19,7 @@ using v8::Isolate; using v8::Just; using v8::JustVoid; using v8::Local; +using v8::LocalVector; using v8::Maybe; using v8::MaybeLocal; using v8::Name; @@ -144,9 +145,9 @@ void Hash::GetCachedAliases(const FunctionCallbackInfo& args) { Isolate* isolate = args.GetIsolate(); Local context = args.GetIsolate()->GetCurrentContext(); Environment* env = Environment::GetCurrent(context); - std::vector> names; - std::vector> values; size_t size = env->alias_to_md_id_map.size(); + LocalVector names(isolate); + LocalVector values(isolate); #if OPENSSL_VERSION_MAJOR >= 3 names.reserve(size); values.reserve(size); diff --git a/src/crypto/crypto_util.cc b/src/crypto/crypto_util.cc index d4832d77be555d..9f5f8e6f03e4ab 100644 --- a/src/crypto/crypto_util.cc +++ b/src/crypto/crypto_util.cc @@ -36,6 +36,7 @@ using v8::HandleScope; using v8::Isolate; using v8::JustVoid; using v8::Local; +using v8::LocalVector; using v8::Maybe; using v8::MaybeLocal; using v8::NewStringType; @@ -236,7 +237,7 @@ MaybeLocal cryptoErrorListToException( if (errors.size() > 1) { CHECK(exception->IsObject()); Local exception_obj = exception.As(); - std::vector> stack(errors.size() - 1); + LocalVector stack(env->isolate(), errors.size() - 1); // Iterate over all but the last error in the list. auto current = errors.begin(); @@ -252,7 +253,7 @@ MaybeLocal cryptoErrorListToException( } Local stackArray = - v8::Array::New(env->isolate(), &stack[0], stack.size()); + v8::Array::New(env->isolate(), stack.data(), stack.size()); if (!exception_obj ->Set(env->context(), env->openssl_error_stack(), stackArray) diff --git a/src/internal_only_v8.cc b/src/internal_only_v8.cc index 17b0c7aba6e1f0..6a3c4e6952a8f3 100644 --- a/src/internal_only_v8.cc +++ b/src/internal_only_v8.cc @@ -10,6 +10,7 @@ using v8::FunctionCallbackInfo; using v8::Global; using v8::Isolate; using v8::Local; +using v8::LocalVector; using v8::Object; using v8::Value; @@ -56,7 +57,7 @@ void QueryObjects(const FunctionCallbackInfo& args) { PrototypeChainHas prototype_chain_has(context, proto.As()); std::vector> out; isolate->GetHeapProfiler()->QueryObjects(context, &prototype_chain_has, &out); - std::vector> result; + LocalVector result(isolate); result.reserve(out.size()); for (size_t i = 0; i < out.size(); ++i) { result.push_back(out[i].Get(isolate)); diff --git a/src/module_wrap.cc b/src/module_wrap.cc index 8be6dbd1d0262c..649ec428e2dd6f 100644 --- a/src/module_wrap.cc +++ b/src/module_wrap.cc @@ -23,25 +23,35 @@ using node::contextify::ContextifyContext; using v8::Array; using v8::ArrayBufferView; using v8::Context; +using v8::Data; using v8::EscapableHandleScope; +using v8::Exception; using v8::FixedArray; using v8::Function; using v8::FunctionCallbackInfo; using v8::FunctionTemplate; +using v8::Global; using v8::HandleScope; using v8::Int32; using v8::Integer; using v8::Isolate; +using v8::Just; using v8::Local; +using v8::LocalVector; +using v8::Maybe; using v8::MaybeLocal; using v8::MemorySpan; +using v8::Message; using v8::MicrotaskQueue; using v8::Module; using v8::ModuleRequest; +using v8::Name; +using v8::Null; using v8::Object; using v8::ObjectTemplate; using v8::PrimitiveArray; using v8::Promise; +using v8::PromiseRejectEvent; using v8::ScriptCompiler; using v8::ScriptOrigin; using v8::String; @@ -103,7 +113,7 @@ ModuleWrap* ModuleWrap::GetFromModule(Environment* env, return nullptr; } -v8::Maybe ModuleWrap::CheckUnsettledTopLevelAwait() { +Maybe ModuleWrap::CheckUnsettledTopLevelAwait() { Isolate* isolate = env()->isolate(); Local context = env()->context(); @@ -115,17 +125,17 @@ v8::Maybe ModuleWrap::CheckUnsettledTopLevelAwait() { Local module = module_.Get(isolate); // It's a synthetic module, likely a facade wrapping CJS. if (!module->IsSourceTextModule()) { - return v8::Just(true); + return Just(true); } if (!module->IsGraphAsync()) { // There is no TLA, no need to check. - return v8::Just(true); + return Just(true); } auto stalled_messages = std::get<1>(module->GetStalledTopLevelAwaitMessages(isolate)); if (stalled_messages.empty()) { - return v8::Just(true); + return Just(true); } if (env()->options()->warnings) { @@ -138,7 +148,7 @@ v8::Maybe ModuleWrap::CheckUnsettledTopLevelAwait() { } } - return v8::Just(false); + return Just(false); } Local ModuleWrap::GetHostDefinedOptions( @@ -229,7 +239,7 @@ void ModuleWrap::New(const FunctionCallbackInfo& args) { Local export_names_arr = args[2].As(); uint32_t len = export_names_arr->Length(); - std::vector> export_names(len); + LocalVector export_names(realm->isolate(), len); for (uint32_t i = 0; i < len; i++) { Local export_name_val = export_names_arr->Get(context, i).ToLocalChecked(); @@ -245,7 +255,7 @@ void ModuleWrap::New(const FunctionCallbackInfo& args) { // When we are compiling for the default loader, this will be // std::nullopt, and CompileSourceTextModule() should use // on-disk cache. - std::optional user_cached_data; + std::optional user_cached_data; if (id_symbol != realm->isolate_data()->source_text_module_default_hdo()) { user_cached_data = nullptr; @@ -324,7 +334,7 @@ void ModuleWrap::New(const FunctionCallbackInfo& args) { // be stored in an internal field. Local context_object = context->GetExtrasBindingObject(); Local synthetic_evaluation_step = - synthetic ? args[3] : Undefined(realm->isolate()).As(); + synthetic ? args[3] : Undefined(realm->isolate()).As(); ModuleWrap* obj = new ModuleWrap( realm, that, module, url, context_object, synthetic_evaluation_step); @@ -405,22 +415,22 @@ static Local createImportAttributesContainer( const int elements_per_attribute) { CHECK_EQ(raw_attributes->Length() % elements_per_attribute, 0); size_t num_attributes = raw_attributes->Length() / elements_per_attribute; - std::vector> names(num_attributes); - std::vector> values(num_attributes); + LocalVector names(isolate, num_attributes); + LocalVector values(isolate, num_attributes); for (int i = 0; i < raw_attributes->Length(); i += elements_per_attribute) { int idx = i / elements_per_attribute; - names[idx] = raw_attributes->Get(realm->context(), i).As(); + names[idx] = raw_attributes->Get(realm->context(), i).As(); values[idx] = raw_attributes->Get(realm->context(), i + 1).As(); } return Object::New( - isolate, v8::Null(isolate), names.data(), values.data(), num_attributes); + isolate, Null(isolate), names.data(), values.data(), num_attributes); } static Local createModuleRequestsContainer( Realm* realm, Isolate* isolate, Local raw_requests) { - std::vector> requests(raw_requests->Length()); + LocalVector requests(isolate, raw_requests->Length()); for (int i = 0; i < raw_requests->Length(); i++) { Local module_request = @@ -434,7 +444,7 @@ static Local createModuleRequestsContainer( Local attributes = createImportAttributesContainer(realm, isolate, raw_attributes, 3); - Local names[] = { + Local names[] = { realm->isolate_data()->specifier_string(), realm->isolate_data()->attributes_string(), }; @@ -444,8 +454,8 @@ static Local createModuleRequestsContainer( }; DCHECK_EQ(arraysize(names), arraysize(values)); - Local request = Object::New( - isolate, v8::Null(isolate), names, values, arraysize(names)); + Local request = + Object::New(isolate, Null(isolate), names, values, arraysize(names)); requests[i] = request; } @@ -481,11 +491,11 @@ void ModuleWrap::Link(const FunctionCallbackInfo& args) { Local modules = args[1].As(); CHECK_EQ(specifiers->Length(), modules->Length()); - std::vector> specifiers_buffer; + std::vector> specifiers_buffer; if (FromV8Array(context, specifiers, &specifiers_buffer).IsNothing()) { return; } - std::vector> modules_buffer; + std::vector> modules_buffer; if (FromV8Array(context, modules, &modules_buffer).IsNothing()) { return; } @@ -669,10 +679,10 @@ void ModuleWrap::EvaluateSync(const FunctionCallbackInfo& args) { // before handler was added which would remove it from the unhandled // rejection handling, since we are converting it into an error and throw // from here directly. - Local type = v8::Integer::New( - isolate, - static_cast( - v8::PromiseRejectEvent::kPromiseHandlerAddedAfterReject)); + Local type = + Integer::New(isolate, + static_cast( + PromiseRejectEvent::kPromiseHandlerAddedAfterReject)); Local args[] = {type, promise, Undefined(isolate)}; if (env->promise_reject_callback() ->Call(context, Undefined(isolate), arraysize(args), args) @@ -680,8 +690,7 @@ void ModuleWrap::EvaluateSync(const FunctionCallbackInfo& args) { return; } Local exception = promise->Result(); - Local message = - v8::Exception::CreateMessage(isolate, exception); + Local message = Exception::CreateMessage(isolate, exception); AppendExceptionLine( env, exception, message, ErrorHandlingMode::MODULE_ERROR); isolate->ThrowException(exception); @@ -718,15 +727,15 @@ void ModuleWrap::GetNamespaceSync(const FunctionCallbackInfo& args) { Local module = obj->module_.Get(isolate); switch (module->GetStatus()) { - case v8::Module::Status::kUninstantiated: - case v8::Module::Status::kInstantiating: + case Module::Status::kUninstantiated: + case Module::Status::kInstantiating: return realm->env()->ThrowError( "Cannot get namespace, module has not been instantiated"); - case v8::Module::Status::kInstantiated: - case v8::Module::Status::kEvaluated: - case v8::Module::Status::kErrored: + case Module::Status::kInstantiated: + case Module::Status::kEvaluated: + case Module::Status::kErrored: break; - case v8::Module::Status::kEvaluating: + case Module::Status::kEvaluating: UNREACHABLE(); } @@ -746,14 +755,14 @@ void ModuleWrap::GetNamespace(const FunctionCallbackInfo& args) { Local module = obj->module_.Get(isolate); switch (module->GetStatus()) { - case v8::Module::Status::kUninstantiated: - case v8::Module::Status::kInstantiating: + case Module::Status::kUninstantiated: + case Module::Status::kInstantiating: return realm->env()->ThrowError( "cannot get namespace, module has not been instantiated"); - case v8::Module::Status::kInstantiated: - case v8::Module::Status::kEvaluating: - case v8::Module::Status::kEvaluated: - case v8::Module::Status::kErrored: + case Module::Status::kInstantiated: + case Module::Status::kEvaluating: + case Module::Status::kEvaluated: + case Module::Status::kErrored: break; default: UNREACHABLE(); @@ -825,7 +834,7 @@ MaybeLocal ModuleWrap::ResolveModuleCallback( static MaybeLocal ImportModuleDynamically( Local context, - Local host_defined_options, + Local host_defined_options, Local resource_name, Local specifier, Local import_attributes) { @@ -1011,7 +1020,7 @@ void ModuleWrap::CreateCachedData(const FunctionCallbackInfo& args) { Local module = obj->module_.Get(isolate); - CHECK_LT(module->GetStatus(), v8::Module::Status::kEvaluating); + CHECK_LT(module->GetStatus(), Module::Status::kEvaluating); Local unbound_module_script = module->GetUnboundModuleScript(); diff --git a/src/node_builtins.cc b/src/node_builtins.cc index 9aaf5626fcfe4a..2170f1ba552b1e 100644 --- a/src/node_builtins.cc +++ b/src/node_builtins.cc @@ -443,7 +443,6 @@ MaybeLocal BuiltinLoader::CompileAndCall(Local context, Isolate* isolate = context->GetIsolate(); // Arguments must match the parameters specified in // BuiltinLoader::LookupAndCompile(). - std::vector> arguments; // Detects parameters of the scripts based on module ids. // internal/bootstrap/realm: process, getLinkedBinding, // getInternalBinding, primordials @@ -458,30 +457,33 @@ MaybeLocal BuiltinLoader::CompileAndCall(Local context, .ToLocal(&get_internal_binding)) { return MaybeLocal(); } - arguments = {realm->process_object(), - get_linked_binding, - get_internal_binding, - realm->primordials()}; + Local arguments[] = {realm->process_object(), + get_linked_binding, + get_internal_binding, + realm->primordials()}; + return CompileAndCall( + context, id, arraysize(arguments), &arguments[0], realm); } else if (strncmp(id, "internal/main/", strlen("internal/main/")) == 0 || strncmp(id, "internal/bootstrap/", strlen("internal/bootstrap/")) == 0) { // internal/main/*, internal/bootstrap/*: process, require, // internalBinding, primordials - arguments = {realm->process_object(), - realm->builtin_module_require(), - realm->internal_binding_loader(), - realm->primordials()}; - } else { - // This should be invoked with the other CompileAndCall() methods, as - // we are unable to generate the arguments. - // Currently there are two cases: - // internal/per_context/*: the arguments are generated in - // InitializePrimordials() - // all the other cases: the arguments are generated in the JS-land loader. - UNREACHABLE(); - } - return CompileAndCall(context, id, arguments.size(), arguments.data(), realm); + Local arguments[] = {realm->process_object(), + realm->builtin_module_require(), + realm->internal_binding_loader(), + realm->primordials()}; + return CompileAndCall( + context, id, arraysize(arguments), &arguments[0], realm); + } + + // This should be invoked with the other CompileAndCall() methods, as + // we are unable to generate the arguments. + // Currently there are two cases: + // internal/per_context/*: the arguments are generated in + // InitializePrimordials() + // all the other cases: the arguments are generated in the JS-land loader. + UNREACHABLE(); } MaybeLocal BuiltinLoader::CompileAndCall(Local context, diff --git a/src/node_contextify.cc b/src/node_contextify.cc index 3aa7986bf17e26..77d35675827c67 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -59,6 +59,7 @@ using v8::Isolate; using v8::JustVoid; using v8::KeyCollectionMode; using v8::Local; +using v8::LocalVector; using v8::Maybe; using v8::MaybeLocal; using v8::MeasureMemoryExecution; @@ -831,7 +832,7 @@ void ContextifyContext::IndexedPropertyEnumeratorCallback( } // Filter out non-number property names. - std::vector> indices; + LocalVector indices(isolate); for (uint32_t i = 0; i < properties->Length(); i++) { Local prop = properties_vec[i].Get(isolate); if (!prop->IsNumber()) { @@ -1790,20 +1791,20 @@ static void CompileFunctionForCJSLoader( } Local undefined = v8::Undefined(isolate); - std::vector> names = { + Local names[] = { env->cached_data_rejected_string(), env->source_map_url_string(), env->function_string(), FIXED_ONE_BYTE_STRING(isolate, "canParseAsESM"), }; - std::vector> values = { + Local values[] = { Boolean::New(isolate, cache_rejected), fn.IsEmpty() ? undefined : fn->GetScriptOrigin().SourceMapUrl(), fn.IsEmpty() ? undefined : fn.As(), Boolean::New(isolate, can_parse_as_esm), }; Local result = Object::New( - isolate, v8::Null(isolate), names.data(), values.data(), names.size()); + isolate, v8::Null(isolate), &names[0], &values[0], arraysize(names)); args.GetReturnValue().Set(result); } diff --git a/src/node_env_var.cc b/src/node_env_var.cc index 82b8231c435775..93dff5f1a9c649 100644 --- a/src/node_env_var.cc +++ b/src/node_env_var.cc @@ -21,6 +21,7 @@ using v8::Intercepted; using v8::Isolate; using v8::JustVoid; using v8::Local; +using v8::LocalVector; using v8::Maybe; using v8::MaybeLocal; using v8::Name; @@ -273,7 +274,7 @@ void MapKVStore::Delete(Isolate* isolate, Local key) { Local MapKVStore::Enumerate(Isolate* isolate) const { Mutex::ScopedLock lock(mutex_); - std::vector> values; + LocalVector values(isolate); values.reserve(map_.size()); for (const auto& pair : map_) { values.emplace_back( diff --git a/src/node_file.cc b/src/node_file.cc index 34a86ef7f140d7..6d097904f67b89 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -73,6 +73,7 @@ using v8::Integer; using v8::Isolate; using v8::JustVoid; using v8::Local; +using v8::LocalVector; using v8::Maybe; using v8::MaybeLocal; using v8::Nothing; @@ -902,8 +903,8 @@ void AfterScanDir(uv_fs_t* req) { Local error; int r; - std::vector> name_v; - std::vector> type_v; + LocalVector name_v(isolate); + LocalVector type_v(isolate); const bool with_file_types = req_wrap->with_file_types(); @@ -2045,8 +2046,8 @@ static void ReadDir(const FunctionCallbackInfo& args) { } int r; - std::vector> name_v; - std::vector> type_v; + LocalVector name_v(isolate); + LocalVector type_v(isolate); for (;;) { uv_dirent_t ent; diff --git a/src/node_http2.cc b/src/node_http2.cc index f9b5226aea50dc..91e9011cd91c6a 100644 --- a/src/node_http2.cc +++ b/src/node_http2.cc @@ -37,6 +37,7 @@ using v8::HandleScope; using v8::Integer; using v8::Isolate; using v8::Local; +using v8::LocalVector; using v8::MaybeLocal; using v8::NewStringType; using v8::Number; @@ -1606,7 +1607,7 @@ void Http2Session::HandleOriginFrame(const nghttp2_frame* frame) { nghttp2_ext_origin* origin = static_cast(ext.payload); size_t nov = origin->nov; - std::vector> origin_v(nov); + LocalVector origin_v(isolate, nov); for (size_t i = 0; i < nov; ++i) { const nghttp2_origin_entry& entry = origin->ov[i]; diff --git a/src/node_http_parser.cc b/src/node_http_parser.cc index dfb278151c9566..6ea4aa826e1c30 100644 --- a/src/node_http_parser.cc +++ b/src/node_http_parser.cc @@ -63,6 +63,7 @@ using v8::Int32; using v8::Integer; using v8::Isolate; using v8::Local; +using v8::LocalVector; using v8::MaybeLocal; using v8::Number; using v8::Object; @@ -1091,7 +1092,7 @@ void ConnectionsList::All(const FunctionCallbackInfo& args) { ASSIGN_OR_RETURN_UNWRAP(&list, args.This()); - std::vector> result; + LocalVector result(isolate); result.reserve(list->all_connections_.size()); for (auto parser : list->all_connections_) { result.emplace_back(parser->object()); @@ -1108,7 +1109,7 @@ void ConnectionsList::Idle(const FunctionCallbackInfo& args) { ASSIGN_OR_RETURN_UNWRAP(&list, args.This()); - std::vector> result; + LocalVector result(isolate); result.reserve(list->all_connections_.size()); for (auto parser : list->all_connections_) { if (parser->last_message_start_ == 0) { @@ -1127,7 +1128,7 @@ void ConnectionsList::Active(const FunctionCallbackInfo& args) { ASSIGN_OR_RETURN_UNWRAP(&list, args.This()); - std::vector> result; + LocalVector result(isolate); result.reserve(list->active_connections_.size()); for (auto parser : list->active_connections_) { result.emplace_back(parser->object()); @@ -1176,7 +1177,7 @@ void ConnectionsList::Expired(const FunctionCallbackInfo& args) { auto iter = list->active_connections_.begin(); auto end = list->active_connections_.end(); - std::vector> result; + LocalVector result(isolate); result.reserve(list->active_connections_.size()); while (iter != end) { Parser* parser = *iter; @@ -1335,8 +1336,8 @@ void CreatePerContextProperties(Local target, BindingData* const binding_data = realm->AddBindingData(target); if (binding_data == nullptr) return; - std::vector> methods_val; - std::vector> all_methods_val; + LocalVector methods_val(isolate); + LocalVector all_methods_val(isolate); #define V(num, name, string) \ methods_val.push_back(FIXED_ONE_BYTE_STRING(isolate, #string)); diff --git a/src/node_modules.cc b/src/node_modules.cc index 94ed9bc4b3c157..4b522a91323c9f 100644 --- a/src/node_modules.cc +++ b/src/node_modules.cc @@ -25,6 +25,7 @@ using v8::FunctionCallbackInfo; using v8::HandleScope; using v8::Isolate; using v8::Local; +using v8::LocalVector; using v8::NewStringType; using v8::Object; using v8::ObjectTemplate; @@ -477,11 +478,11 @@ void EnableCompileCache(const FunctionCallbackInfo& args) { } Utf8Value value(isolate, args[0]); CompileCacheEnableResult result = env->EnableCompileCache(*value); - std::vector> values = { + Local values[] = { v8::Integer::New(isolate, static_cast(result.status)), ToV8Value(context, result.message).ToLocalChecked(), ToV8Value(context, result.cache_directory).ToLocalChecked()}; - args.GetReturnValue().Set(Array::New(isolate, values.data(), values.size())); + args.GetReturnValue().Set(Array::New(isolate, &values[0], arraysize(values))); } void GetCompileCacheDir(const FunctionCallbackInfo& args) { @@ -522,8 +523,8 @@ void BindingData::CreatePerContextProperties(Local target, Realm* realm = Realm::GetCurrent(context); realm->AddBindingData(target); - std::vector> compile_cache_status_values; Isolate* isolate = context->GetIsolate(); + LocalVector compile_cache_status_values(isolate); #define V(status) \ compile_cache_status_values.push_back( \ diff --git a/src/node_options.cc b/src/node_options.cc index 189a03bd69b3ed..0f5b55cbc643ba 100644 --- a/src/node_options.cc +++ b/src/node_options.cc @@ -23,6 +23,7 @@ using v8::FunctionCallbackInfo; using v8::Integer; using v8::Isolate; using v8::Local; +using v8::LocalVector; using v8::Map; using v8::Name; using v8::Null; @@ -1312,8 +1313,8 @@ void GetCLIOptionsValues(const FunctionCallbackInfo& args) { Mutex::ScopedLock lock(per_process::cli_options_mutex); IterateCLIOptionsScope s(env); - std::vector> option_names; - std::vector> option_values; + LocalVector option_names(isolate); + LocalVector option_values(isolate); option_names.reserve(_ppop_instance.options_.size() * 2); option_values.reserve(_ppop_instance.options_.size() * 2); diff --git a/src/node_os.cc b/src/node_os.cc index ce2af8d83b7443..edfe34f330f73c 100644 --- a/src/node_os.cc +++ b/src/node_os.cc @@ -48,6 +48,7 @@ using v8::Int32; using v8::Integer; using v8::Isolate; using v8::Local; +using v8::LocalVector; using v8::MaybeLocal; using v8::NewStringType; using v8::Null; @@ -112,7 +113,7 @@ static void GetCPUInfo(const FunctionCallbackInfo& args) { // assemble them into objects in JS than to call Object::Set() repeatedly // The array is in the format // [model, speed, (5 entries of cpu_times), model2, speed2, ...] - std::vector> result; + LocalVector result(isolate); result.reserve(count * 7); for (int i = 0; i < count; i++) { uv_cpu_info_t* ci = cpu_infos + i; @@ -193,7 +194,7 @@ static void GetInterfaceAddresses(const FunctionCallbackInfo& args) { } Local no_scope_id = Integer::New(isolate, -1); - std::vector> result; + LocalVector result(isolate); result.reserve(count * 7); for (i = 0; i < count; i++) { const char* const raw_name = interfaces[i].name; diff --git a/src/node_sockaddr.cc b/src/node_sockaddr.cc index 19fcc6b89ac145..8cfef3726def8c 100644 --- a/src/node_sockaddr.cc +++ b/src/node_sockaddr.cc @@ -20,6 +20,7 @@ using v8::FunctionTemplate; using v8::Int32; using v8::Isolate; using v8::Local; +using v8::LocalVector; using v8::MaybeLocal; using v8::Object; using v8::Uint32; @@ -498,15 +499,14 @@ std::string SocketAddressBlockList::SocketAddressMaskRule::ToString() { MaybeLocal SocketAddressBlockList::ListRules(Environment* env) { Mutex::ScopedLock lock(mutex_); - std::vector> rules; + LocalVector rules(env->isolate()); if (!ListRules(env, &rules)) return MaybeLocal(); return Array::New(env->isolate(), rules.data(), rules.size()); } -bool SocketAddressBlockList::ListRules( - Environment* env, - std::vector>* rules) { +bool SocketAddressBlockList::ListRules(Environment* env, + LocalVector* rules) { if (parent_ && !parent_->ListRules(env, rules)) return false; for (const auto& rule : rules_) { diff --git a/src/node_sockaddr.h b/src/node_sockaddr.h index b822e186969917..a522505949a263 100644 --- a/src/node_sockaddr.h +++ b/src/node_sockaddr.h @@ -315,9 +315,7 @@ class SocketAddressBlockList : public MemoryRetainer { SET_SELF_SIZE(SocketAddressBlockList) private: - bool ListRules( - Environment* env, - std::vector>* vec); + bool ListRules(Environment* env, v8::LocalVector* vec); std::shared_ptr parent_; std::list> rules_; diff --git a/src/node_webstorage.cc b/src/node_webstorage.cc index 3d71ab33c5b41c..4d796e6df90c58 100644 --- a/src/node_webstorage.cc +++ b/src/node_webstorage.cc @@ -26,6 +26,7 @@ using v8::Intercepted; using v8::Isolate; using v8::JustVoid; using v8::Local; +using v8::LocalVector; using v8::Map; using v8::Maybe; using v8::MaybeLocal; @@ -255,7 +256,7 @@ MaybeLocal Storage::Enumerate() { int r = sqlite3_prepare_v2(db_.get(), sql.data(), sql.size(), &s, 0); CHECK_ERROR_OR_THROW(env(), r, SQLITE_OK, Local()); auto stmt = stmt_unique_ptr(s); - std::vector> values; + LocalVector values(env()->isolate()); Local value; while ((r = sqlite3_step(stmt.get())) == SQLITE_ROW) { CHECK(sqlite3_column_type(stmt.get(), 0) == SQLITE_BLOB);