diff --git a/src/async_wrap.cc b/src/async_wrap.cc index 70fc86702f0b10..193fec7ca7087a 100644 --- a/src/async_wrap.cc +++ b/src/async_wrap.cc @@ -201,15 +201,15 @@ PromiseWrap* PromiseWrap::New(Environment* env, Local promise, PromiseWrap* parent_wrap, bool silent) { - Local object = env->promise_wrap_template() - ->NewInstance(env->context()).ToLocalChecked(); - object->SetInternalField(PromiseWrap::kIsChainedPromiseField, - parent_wrap != nullptr ? - v8::True(env->isolate()) : - v8::False(env->isolate())); + Local obj; + if (!env->promise_wrap_template()->NewInstance(env->context()).ToLocal(&obj)) + return nullptr; + obj->SetInternalField(PromiseWrap::kIsChainedPromiseField, + parent_wrap != nullptr ? v8::True(env->isolate()) + : v8::False(env->isolate())); CHECK_EQ(promise->GetAlignedPointerFromInternalField(0), nullptr); - promise->SetInternalField(0, object); - return new PromiseWrap(env, object, silent); + promise->SetInternalField(0, obj); + return new PromiseWrap(env, obj, silent); } void PromiseWrap::getIsChainedPromise(Local property, @@ -242,6 +242,7 @@ static void PromiseHook(PromiseHookType type, Local promise, PromiseWrap* parent_wrap = extractPromiseWrap(parent_promise); if (parent_wrap == nullptr) { parent_wrap = PromiseWrap::New(env, parent_promise, nullptr, true); + if (parent_wrap == nullptr) return; } AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(parent_wrap); @@ -251,7 +252,8 @@ static void PromiseHook(PromiseHookType type, Local promise, } } - CHECK_NOT_NULL(wrap); + if (wrap == nullptr) return; + if (type == PromiseHookType::kBefore) { env->async_hooks()->push_async_ids( wrap->get_async_id(), wrap->get_trigger_async_id()); diff --git a/src/connection_wrap.cc b/src/connection_wrap.cc index 67308bdb9c8d95..b894382c0370d5 100644 --- a/src/connection_wrap.cc +++ b/src/connection_wrap.cc @@ -48,9 +48,10 @@ void ConnectionWrap::OnConnection(uv_stream_t* handle, if (status == 0) { // Instantiate the client javascript object and handle. - Local client_obj = WrapType::Instantiate(env, - wrap_data, - WrapType::SOCKET); + Local client_obj; + if (!WrapType::Instantiate(env, wrap_data, WrapType::SOCKET) + .ToLocal(&client_obj)) + return; // Unwrap the client javascript object. WrapType* wrap; diff --git a/src/env.cc b/src/env.cc index 97cfc72a9dd58e..78ed42f89af9fc 100644 --- a/src/env.cc +++ b/src/env.cc @@ -289,29 +289,10 @@ Environment::~Environment() { } } -void Environment::Start(const std::vector& args, - const std::vector& exec_args, - bool start_profiler_idle_notifier) { +void Environment::Start(bool start_profiler_idle_notifier) { HandleScope handle_scope(isolate()); Context::Scope context_scope(context()); - if (*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED( - TRACING_CATEGORY_NODE1(environment)) != 0) { - auto traced_value = tracing::TracedValue::Create(); - traced_value->BeginArray("args"); - for (const std::string& arg : args) - traced_value->AppendString(arg); - traced_value->EndArray(); - traced_value->BeginArray("exec_args"); - for (const std::string& arg : exec_args) - traced_value->AppendString(arg); - traced_value->EndArray(); - TRACE_EVENT_NESTABLE_ASYNC_BEGIN1( - TRACING_CATEGORY_NODE1(environment), - "Environment", this, - "args", std::move(traced_value)); - } - CHECK_EQ(0, uv_timer_init(event_loop(), timer_handle())); uv_unref(reinterpret_cast(timer_handle())); @@ -346,14 +327,37 @@ void Environment::Start(const std::vector& args, StartProfilerIdleNotifier(); } - Local process_object = CreateProcessObject(this, args, exec_args); - set_process_object(process_object); - static uv_once_t init_once = UV_ONCE_INIT; uv_once(&init_once, InitThreadLocalOnce); uv_key_set(&thread_local_env, this); } +MaybeLocal Environment::CreateProcessObject( + const std::vector& args, + const std::vector& exec_args) { + if (*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED( + TRACING_CATEGORY_NODE1(environment)) != 0) { + auto traced_value = tracing::TracedValue::Create(); + traced_value->BeginArray("args"); + for (const std::string& arg : args) traced_value->AppendString(arg); + traced_value->EndArray(); + traced_value->BeginArray("exec_args"); + for (const std::string& arg : exec_args) traced_value->AppendString(arg); + traced_value->EndArray(); + TRACE_EVENT_NESTABLE_ASYNC_BEGIN1(TRACING_CATEGORY_NODE1(environment), + "Environment", + this, + "args", + std::move(traced_value)); + } + + Local process_object = + node::CreateProcessObject(this, args, exec_args) + .FromMaybe(Local()); + set_process_object(process_object); + return process_object; +} + void Environment::RegisterHandleCleanups() { HandleCleanupCb close_and_finish = [](Environment* env, uv_handle_t* handle, void* arg) { diff --git a/src/env.h b/src/env.h index 9de45a464bd421..29e749ba7c4839 100644 --- a/src/env.h +++ b/src/env.h @@ -610,9 +610,10 @@ class Environment { v8::Local context); ~Environment(); - void Start(const std::vector& args, - const std::vector& exec_args, - bool start_profiler_idle_notifier); + void Start(bool start_profiler_idle_notifier); + v8::MaybeLocal CreateProcessObject( + const std::vector& args, + const std::vector& exec_args); typedef void (*HandleCleanupCb)(Environment* env, uv_handle_t* handle, diff --git a/src/node.cc b/src/node.cc index 1fab882a611271..d4a69ffaf11792 100644 --- a/src/node.cc +++ b/src/node.cc @@ -1213,7 +1213,8 @@ Environment* CreateEnvironment(IsolateData* isolate_data, std::vector args(argv, argv + argc); std::vector exec_args(exec_argv, exec_argv + exec_argc); Environment* env = new Environment(isolate_data, context); - env->Start(args, exec_args, per_process::v8_is_profiling); + env->Start(per_process::v8_is_profiling); + env->CreateProcessObject(args, exec_args); return env; } @@ -1287,7 +1288,8 @@ inline int Start(Isolate* isolate, IsolateData* isolate_data, Local context = NewContext(isolate); Context::Scope context_scope(context); Environment env(isolate_data, context); - env.Start(args, exec_args, per_process::v8_is_profiling); + env.Start(per_process::v8_is_profiling); + env.CreateProcessObject(args, exec_args); const char* path = args.size() > 1 ? args[1].c_str() : nullptr; StartInspector(&env, path); diff --git a/src/node_contextify.cc b/src/node_contextify.cc index fc89f58f8bef65..8eedf8fa665b71 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -104,12 +104,12 @@ Local Uint32ToName(Local context, uint32_t index) { ContextifyContext::ContextifyContext( Environment* env, Local sandbox_obj, const ContextOptions& options) : env_(env) { - Local v8_context = CreateV8Context(env, sandbox_obj, options); - context_.Reset(env->isolate(), v8_context); + MaybeLocal v8_context = CreateV8Context(env, sandbox_obj, options); - // Allocation failure or maximum call stack size reached - if (context_.IsEmpty()) - return; + // Allocation failure, maximum call stack size reached, termination, etc. + if (v8_context.IsEmpty()) return; + + context_.Reset(env->isolate(), v8_context.ToLocalChecked()); context_.SetWeak(this, WeakCallback, WeakCallbackType::kParameter); } @@ -119,20 +119,19 @@ ContextifyContext::ContextifyContext( // pass the main JavaScript context object we're embedded in, then the // NamedPropertyHandler will store a reference to it forever and keep it // from getting gc'd. -Local ContextifyContext::CreateDataWrapper(Environment* env) { - EscapableHandleScope scope(env->isolate()); - Local wrapper = - env->script_data_constructor_function() - ->NewInstance(env->context()).FromMaybe(Local()); - if (wrapper.IsEmpty()) - return scope.Escape(Local::New(env->isolate(), Local())); +MaybeLocal ContextifyContext::CreateDataWrapper(Environment* env) { + Local wrapper; + if (!env->script_data_constructor_function() + ->NewInstance(env->context()) + .ToLocal(&wrapper)) { + return MaybeLocal(); + } wrapper->SetAlignedPointerInInternalField(0, this); - return scope.Escape(wrapper); + return wrapper; } - -Local ContextifyContext::CreateV8Context( +MaybeLocal ContextifyContext::CreateV8Context( Environment* env, Local sandbox_obj, const ContextOptions& options) { @@ -145,13 +144,17 @@ Local ContextifyContext::CreateV8Context( Local object_template = function_template->InstanceTemplate(); + Local data_wrapper; + if (!CreateDataWrapper(env).ToLocal(&data_wrapper)) + return MaybeLocal(); + NamedPropertyHandlerConfiguration config(PropertyGetterCallback, PropertySetterCallback, PropertyDescriptorCallback, PropertyDeleterCallback, PropertyEnumeratorCallback, PropertyDefinerCallback, - CreateDataWrapper(env)); + data_wrapper); IndexedPropertyHandlerConfiguration indexed_config( IndexedPropertyGetterCallback, @@ -160,7 +163,7 @@ Local ContextifyContext::CreateV8Context( IndexedPropertyDeleterCallback, PropertyEnumeratorCallback, IndexedPropertyDefinerCallback, - CreateDataWrapper(env)); + data_wrapper); object_template->SetHandler(config); object_template->SetHandler(indexed_config); @@ -169,7 +172,7 @@ Local ContextifyContext::CreateV8Context( if (ctx.IsEmpty()) { env->ThrowError("Could not instantiate context"); - return Local(); + return MaybeLocal(); } ctx->SetSecurityToken(env->context()->GetSecurityToken()); diff --git a/src/node_contextify.h b/src/node_contextify.h index 5f4f20554aed50..cb3ca83553a61c 100644 --- a/src/node_contextify.h +++ b/src/node_contextify.h @@ -23,9 +23,10 @@ class ContextifyContext { v8::Local sandbox_obj, const ContextOptions& options); - v8::Local CreateDataWrapper(Environment* env); - v8::Local CreateV8Context(Environment* env, - v8::Local sandbox_obj, const ContextOptions& options); + v8::MaybeLocal CreateDataWrapper(Environment* env); + v8::MaybeLocal CreateV8Context(Environment* env, + v8::Local sandbox_obj, + const ContextOptions& options); static void Init(Environment* env, v8::Local target); static ContextifyContext* ContextFromContextifiedSandbox( diff --git a/src/node_crypto.cc b/src/node_crypto.cc index 01593914a1f501..fbd509a6fb0c3c 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -3327,15 +3327,18 @@ Local KeyObject::Initialize(Environment* env, Local target) { return function; } -Local KeyObject::Create(Environment* env, - KeyType key_type, - const ManagedEVPPKey& pkey) { +MaybeLocal KeyObject::Create(Environment* env, + KeyType key_type, + const ManagedEVPPKey& pkey) { CHECK_NE(key_type, kKeyTypeSecret); Local type = Integer::New(env->isolate(), key_type); - Local obj = - env->crypto_key_object_constructor()->NewInstance(env->context(), - 1, &type) - .ToLocalChecked(); + Local obj; + if (!env->crypto_key_object_constructor() + ->NewInstance(env->context(), 1, &type) + .ToLocal(&obj)) { + return MaybeLocal(); + } + KeyObject* key = Unwrap(obj); CHECK(key); if (key_type == kKeyTypePublic) @@ -5823,24 +5826,22 @@ class GenerateKeyPairJob : public CryptoJob { if (public_key_encoding_.output_key_object_) { // Note that this has the downside of containing sensitive data of the // private key. - *pubkey = KeyObject::Create(env, kKeyTypePublic, pkey_); + if (!KeyObject::Create(env, kKeyTypePublic, pkey_).ToLocal(pubkey)) + return false; } else { - MaybeLocal maybe_pubkey = - WritePublicKey(env, pkey_.get(), public_key_encoding_); - if (maybe_pubkey.IsEmpty()) + if (!WritePublicKey(env, pkey_.get(), public_key_encoding_) + .ToLocal(pubkey)) return false; - *pubkey = maybe_pubkey.ToLocalChecked(); } // Now do the same for the private key. if (private_key_encoding_.output_key_object_) { - *privkey = KeyObject::Create(env, kKeyTypePrivate, pkey_); + if (!KeyObject::Create(env, kKeyTypePrivate, pkey_).ToLocal(privkey)) + return false; } else { - MaybeLocal maybe_privkey = - WritePrivateKey(env, pkey_.get(), private_key_encoding_); - if (maybe_privkey.IsEmpty()) + if (!WritePrivateKey(env, pkey_.get(), private_key_encoding_) + .ToLocal(privkey)) return false; - *privkey = maybe_privkey.ToLocalChecked(); } return true; diff --git a/src/node_crypto.h b/src/node_crypto.h index 1b950846a7240c..cfe5ebdbdbc2a8 100644 --- a/src/node_crypto.h +++ b/src/node_crypto.h @@ -442,9 +442,9 @@ class KeyObject : public BaseObject { static v8::Local Initialize(Environment* env, v8::Local target); - static v8::Local Create(Environment* env, - KeyType type, - const ManagedEVPPKey& pkey); + static v8::MaybeLocal Create(Environment* env, + KeyType type, + const ManagedEVPPKey& pkey); // TODO(tniessen): track the memory used by OpenSSL types SET_NO_MEMORY_INFO() diff --git a/src/node_env_var.cc b/src/node_env_var.cc index 60582d7a31c804..d994a2199a5202 100644 --- a/src/node_env_var.cc +++ b/src/node_env_var.cc @@ -17,6 +17,7 @@ using v8::EscapableHandleScope; using v8::Integer; using v8::Isolate; using v8::Local; +using v8::MaybeLocal; using v8::Name; using v8::NamedPropertyHandlerConfiguration; using v8::NewStringType; @@ -209,15 +210,13 @@ static void EnvEnumerator(const PropertyCallbackInfo& info) { info.GetReturnValue().Set(envarr); } -Local CreateEnvVarProxy(Local context, - Isolate* isolate, - Local data) { +MaybeLocal CreateEnvVarProxy(Local context, + Isolate* isolate, + Local data) { EscapableHandleScope scope(isolate); Local env_proxy_template = ObjectTemplate::New(isolate); env_proxy_template->SetHandler(NamedPropertyHandlerConfiguration( EnvGetter, EnvSetter, EnvQuery, EnvDeleter, EnvEnumerator, data)); - Local env_proxy = - env_proxy_template->NewInstance(context).ToLocalChecked(); - return scope.Escape(env_proxy); + return scope.EscapeMaybe(env_proxy_template->NewInstance(context)); } } // namespace node diff --git a/src/node_http2.cc b/src/node_http2.cc index ea60fb459ceff1..5b172dfee78e87 100644 --- a/src/node_http2.cc +++ b/src/node_http2.cc @@ -697,7 +697,8 @@ void Http2Stream::EmitStatistics() { } buffer[IDX_STREAM_STATS_SENTBYTES] = entry->sent_bytes(); buffer[IDX_STREAM_STATS_RECEIVEDBYTES] = entry->received_bytes(); - entry->Notify(entry->ToObject()); + Local obj; + if (entry->ToObject().ToLocal(&obj)) entry->Notify(obj); }, static_cast(entry)); } @@ -726,7 +727,8 @@ void Http2Session::EmitStatistics() { buffer[IDX_SESSION_STATS_DATA_RECEIVED] = entry->data_received(); buffer[IDX_SESSION_STATS_MAX_CONCURRENT_STREAMS] = entry->max_concurrent_streams(); - entry->Notify(entry->ToObject()); + Local obj; + if (entry->ToObject().ToLocal(&obj)) entry->Notify(obj); }, static_cast(entry)); } diff --git a/src/node_i18n.cc b/src/node_i18n.cc index edc7f56093a137..e207ed968892b9 100644 --- a/src/node_i18n.cc +++ b/src/node_i18n.cc @@ -171,6 +171,11 @@ class ConverterObject : public BaseObject, Converter { Environment* env = Environment::GetCurrent(args); HandleScope scope(env->isolate()); + Local t = ObjectTemplate::New(env->isolate()); + t->SetInternalFieldCount(1); + Local obj; + if (!t->NewInstance(env->context()).ToLocal(&obj)) return; + CHECK_GE(args.Length(), 2); Utf8Value label(env->isolate(), args[0]); int flags = args[1]->Uint32Value(env->context()).ToChecked(); @@ -190,9 +195,6 @@ class ConverterObject : public BaseObject, Converter { nullptr, nullptr, nullptr, &status); } - Local t = ObjectTemplate::New(env->isolate()); - t->SetInternalFieldCount(1); - Local obj = t->NewInstance(env->context()).ToLocalChecked(); new ConverterObject(env, obj, conv, ignoreBOM); args.GetReturnValue().Set(obj); } diff --git a/src/node_perf.cc b/src/node_perf.cc index 45d66ad5917ab2..7f244dc80c01c0 100644 --- a/src/node_perf.cc +++ b/src/node_perf.cc @@ -20,6 +20,7 @@ using v8::HandleScope; using v8::Integer; using v8::Isolate; using v8::Local; +using v8::MaybeLocal; using v8::Name; using v8::NewStringType; using v8::Number; @@ -102,10 +103,13 @@ inline void InitObject(const PerformanceEntry& entry, Local obj) { } // Create a new PerformanceEntry object -const Local PerformanceEntry::ToObject() const { - Local obj = - env_->performance_entry_template() - ->NewInstance(env_->context()).ToLocalChecked(); +MaybeLocal PerformanceEntry::ToObject() const { + Local obj; + if (!env_->performance_entry_template() + ->NewInstance(env_->context()) + .ToLocal(&obj)) { + return MaybeLocal(); + } InitObject(*this, obj); return obj; } @@ -154,7 +158,8 @@ void Mark(const FunctionCallbackInfo& args) { *name, now / 1000); PerformanceEntry entry(env, *name, "mark", now, now); - Local obj = entry.ToObject(); + Local obj; + if (!entry.ToObject().ToLocal(&obj)) return; PerformanceEntry::Notify(env, entry.kind(), obj); args.GetReturnValue().Set(obj); } @@ -217,7 +222,8 @@ void Measure(const FunctionCallbackInfo& args) { *name, *name, endTimestamp / 1000); PerformanceEntry entry(env, *name, "measure", startTimestamp, endTimestamp); - Local obj = entry.ToObject(); + Local obj; + if (!entry.ToObject().ToLocal(&obj)) return; PerformanceEntry::Notify(env, entry.kind(), obj); args.GetReturnValue().Set(obj); } @@ -242,14 +248,16 @@ void SetupPerformanceObservers(const FunctionCallbackInfo& args) { // Creates a GC Performance Entry and passes it to observers void PerformanceGCCallback(Environment* env, void* ptr) { - GCPerformanceEntry* entry = static_cast(ptr); + std::unique_ptr entry{ + static_cast(ptr)}; HandleScope scope(env->isolate()); Local context = env->context(); AliasedBuffer& observers = env->performance_state()->observers; if (observers[NODE_PERFORMANCE_ENTRY_TYPE_GC]) { - Local obj = entry->ToObject(); + Local obj; + if (!entry->ToObject().ToLocal(&obj)) return; PropertyAttribute attr = static_cast(ReadOnly | DontDelete); obj->DefineOwnProperty(context, @@ -258,8 +266,6 @@ void PerformanceGCCallback(Environment* env, void* ptr) { attr).FromJust(); PerformanceEntry::Notify(env, entry->kind(), obj); } - - delete entry; } // Marks the start of a GC cycle @@ -354,7 +360,8 @@ void TimerFunctionCall(const FunctionCallbackInfo& args) { return; PerformanceEntry entry(env, *name, "function", start, end); - Local obj = entry.ToObject(); + Local obj; + if (!entry.ToObject().ToLocal(&obj)) return; for (idx = 0; idx < count; idx++) obj->Set(context, idx, args[idx]).FromJust(); PerformanceEntry::Notify(env, entry.kind(), obj); diff --git a/src/node_perf.h b/src/node_perf.h index fe418ad441718a..56140bd62dee51 100644 --- a/src/node_perf.h +++ b/src/node_perf.h @@ -75,7 +75,7 @@ class PerformanceEntry { virtual ~PerformanceEntry() { } - virtual const Local ToObject() const; + virtual v8::MaybeLocal ToObject() const; Environment* env() const { return env_; } diff --git a/src/node_process.h b/src/node_process.h index 865223c1634010..452805ada31449 100644 --- a/src/node_process.h +++ b/src/node_process.h @@ -7,9 +7,9 @@ namespace node { -v8::Local CreateEnvVarProxy(v8::Local context, - v8::Isolate* isolate, - v8::Local data); +v8::MaybeLocal CreateEnvVarProxy(v8::Local context, + v8::Isolate* isolate, + v8::Local data); // Most of the time, it's best to use `console.error` to write // to the process.stderr stream. However, in some cases, such as @@ -31,7 +31,7 @@ v8::Maybe ProcessEmitDeprecationWarning(Environment* env, const char* warning, const char* deprecation_code); -v8::Local CreateProcessObject( +v8::MaybeLocal CreateProcessObject( Environment* env, const std::vector& args, const std::vector& exec_args); diff --git a/src/node_process_object.cc b/src/node_process_object.cc index 980b5002b69b0b..c1f8806110ffef 100644 --- a/src/node_process_object.cc +++ b/src/node_process_object.cc @@ -19,6 +19,7 @@ using v8::Integer; using v8::Isolate; using v8::Just; using v8::Local; +using v8::MaybeLocal; using v8::Name; using v8::NewStringType; using v8::None; @@ -66,19 +67,22 @@ static void GetParentProcessId(Local property, info.GetReturnValue().Set(uv_os_getppid()); } -Local CreateProcessObject(Environment* env, - const std::vector& args, - const std::vector& exec_args) { +MaybeLocal CreateProcessObject( + Environment* env, + const std::vector& args, + const std::vector& exec_args) { Isolate* isolate = env->isolate(); EscapableHandleScope scope(isolate); Local context = env->context(); Local process_template = FunctionTemplate::New(isolate); process_template->SetClassName(FIXED_ONE_BYTE_STRING(isolate, "process")); - Local process = process_template->GetFunction(context) - .ToLocalChecked() - ->NewInstance(context) - .ToLocalChecked(); + Local process_ctor; + Local process; + if (!process_template->GetFunction(context).ToLocal(&process_ctor) || + !process_ctor->NewInstance(context).ToLocal(&process)) { + return MaybeLocal(); + } // process.title auto title_string = FIXED_ONE_BYTE_STRING(env->isolate(), "title"); @@ -145,11 +149,16 @@ Local CreateProcessObject(Environment* env, ToV8Value(env->context(), exec_args) .ToLocalChecked()).FromJust(); + Local env_var_proxy; + if (!CreateEnvVarProxy(context, isolate, env->as_external()) + .ToLocal(&env_var_proxy)) + return MaybeLocal(); + // process.env process ->Set(env->context(), FIXED_ONE_BYTE_STRING(env->isolate(), "env"), - CreateEnvVarProxy(context, isolate, env->as_external())) + env_var_proxy) .FromJust(); READONLY_PROPERTY(process, "pid", diff --git a/src/node_worker.cc b/src/node_worker.cc index cb496005844e21..e5ba438bc1501c 100644 --- a/src/node_worker.cc +++ b/src/node_worker.cc @@ -132,9 +132,9 @@ Worker::Worker(Environment* env, env_->set_worker_context(this); env_->set_thread_id(thread_id_); - env_->Start(std::vector{}, - std::vector{}, - env->profiler_idle_notifier_started()); + env_->Start(env->profiler_idle_notifier_started()); + env_->CreateProcessObject(std::vector{}, + std::vector{}); // Done while on the parent thread AddWorkerInspector(env, env_.get(), thread_id_, url_); } diff --git a/src/pipe_wrap.cc b/src/pipe_wrap.cc index 8ad1e0f594aa26..06bb3dd73d1b3c 100644 --- a/src/pipe_wrap.cc +++ b/src/pipe_wrap.cc @@ -42,16 +42,16 @@ using v8::FunctionTemplate; using v8::HandleScope; using v8::Int32; using v8::Local; +using v8::MaybeLocal; using v8::Object; using v8::String; using v8::Value; using AsyncHooks = Environment::AsyncHooks; - -Local PipeWrap::Instantiate(Environment* env, - AsyncWrap* parent, - PipeWrap::SocketType type) { +MaybeLocal PipeWrap::Instantiate(Environment* env, + AsyncWrap* parent, + PipeWrap::SocketType type) { EscapableHandleScope handle_scope(env->isolate()); AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(parent); CHECK_EQ(false, env->pipe_constructor_template().IsEmpty()); @@ -60,9 +60,8 @@ Local PipeWrap::Instantiate(Environment* env, .ToLocalChecked(); CHECK_EQ(false, constructor.IsEmpty()); Local type_value = Int32::New(env->isolate(), type); - Local instance = - constructor->NewInstance(env->context(), 1, &type_value).ToLocalChecked(); - return handle_scope.Escape(instance); + return handle_scope.EscapeMaybe( + constructor->NewInstance(env->context(), 1, &type_value)); } diff --git a/src/pipe_wrap.h b/src/pipe_wrap.h index b98d850439f0f4..473179a4f6fba0 100644 --- a/src/pipe_wrap.h +++ b/src/pipe_wrap.h @@ -38,9 +38,9 @@ class PipeWrap : public ConnectionWrap { IPC }; - static v8::Local Instantiate(Environment* env, - AsyncWrap* parent, - SocketType type); + static v8::MaybeLocal Instantiate(Environment* env, + AsyncWrap* parent, + SocketType type); static void Initialize(v8::Local target, v8::Local unused, v8::Local context, diff --git a/src/stream_wrap.cc b/src/stream_wrap.cc index ae54b019fea034..36a490f986f693 100644 --- a/src/stream_wrap.cc +++ b/src/stream_wrap.cc @@ -44,6 +44,7 @@ using v8::FunctionCallbackInfo; using v8::FunctionTemplate; using v8::HandleScope; using v8::Local; +using v8::MaybeLocal; using v8::Object; using v8::ReadOnly; using v8::Signature; @@ -195,10 +196,9 @@ void LibuvStreamWrap::OnUvAlloc(size_t suggested_size, uv_buf_t* buf) { *buf = EmitAlloc(suggested_size); } - - template -static Local AcceptHandle(Environment* env, LibuvStreamWrap* parent) { +static MaybeLocal AcceptHandle(Environment* env, + LibuvStreamWrap* parent) { static_assert(std::is_base_of::value || std::is_base_of::value, "Can only accept stream handles"); @@ -206,8 +206,7 @@ static Local AcceptHandle(Environment* env, LibuvStreamWrap* parent) { EscapableHandleScope scope(env->isolate()); Local wrap_obj; - wrap_obj = WrapType::Instantiate(env, parent, WrapType::SOCKET); - if (wrap_obj.IsEmpty()) + if (!WrapType::Instantiate(env, parent, WrapType::SOCKET).ToLocal(&wrap_obj)) return Local(); HandleWrap* wrap = Unwrap(wrap_obj); @@ -237,7 +236,7 @@ void LibuvStreamWrap::OnUvRead(ssize_t nread, const uv_buf_t* buf) { CHECK_EQ(persistent().IsEmpty(), false); if (nread > 0) { - Local pending_obj; + MaybeLocal pending_obj; if (type == UV_TCP) { pending_obj = AcceptHandle(env(), this); @@ -250,9 +249,11 @@ void LibuvStreamWrap::OnUvRead(ssize_t nread, const uv_buf_t* buf) { } if (!pending_obj.IsEmpty()) { - object()->Set(env()->context(), - env()->pending_handle_string(), - pending_obj).FromJust(); + object() + ->Set(env()->context(), + env()->pending_handle_string(), + pending_obj.ToLocalChecked()) + .FromJust(); } } diff --git a/src/tcp_wrap.cc b/src/tcp_wrap.cc index dac621ec879e5c..cb6e634006709d 100644 --- a/src/tcp_wrap.cc +++ b/src/tcp_wrap.cc @@ -46,6 +46,7 @@ using v8::HandleScope; using v8::Int32; using v8::Integer; using v8::Local; +using v8::MaybeLocal; using v8::Object; using v8::String; using v8::Uint32; @@ -53,10 +54,9 @@ using v8::Value; using AsyncHooks = Environment::AsyncHooks; - -Local TCPWrap::Instantiate(Environment* env, - AsyncWrap* parent, - TCPWrap::SocketType type) { +MaybeLocal TCPWrap::Instantiate(Environment* env, + AsyncWrap* parent, + TCPWrap::SocketType type) { EscapableHandleScope handle_scope(env->isolate()); AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(parent); CHECK_EQ(env->tcp_constructor_template().IsEmpty(), false); @@ -65,9 +65,8 @@ Local TCPWrap::Instantiate(Environment* env, .ToLocalChecked(); CHECK_EQ(constructor.IsEmpty(), false); Local type_value = Int32::New(env->isolate(), type); - Local instance = - constructor->NewInstance(env->context(), 1, &type_value).ToLocalChecked(); - return handle_scope.Escape(instance); + return handle_scope.EscapeMaybe( + constructor->NewInstance(env->context(), 1, &type_value)); } diff --git a/src/tcp_wrap.h b/src/tcp_wrap.h index db269f65281639..0467a1c3f3bf20 100644 --- a/src/tcp_wrap.h +++ b/src/tcp_wrap.h @@ -37,9 +37,9 @@ class TCPWrap : public ConnectionWrap { SERVER }; - static v8::Local Instantiate(Environment* env, - AsyncWrap* parent, - SocketType type); + static v8::MaybeLocal Instantiate(Environment* env, + AsyncWrap* parent, + SocketType type); static void Initialize(v8::Local target, v8::Local unused, v8::Local context, diff --git a/src/udp_wrap.cc b/src/udp_wrap.cc index d838ef6e725b96..e4aca28c89500e 100644 --- a/src/udp_wrap.cc +++ b/src/udp_wrap.cc @@ -30,12 +30,12 @@ namespace node { using v8::Array; using v8::Context; -using v8::EscapableHandleScope; using v8::FunctionCallbackInfo; using v8::FunctionTemplate; using v8::HandleScope; using v8::Integer; using v8::Local; +using v8::MaybeLocal; using v8::Object; using v8::PropertyAttribute; using v8::Signature; @@ -518,18 +518,14 @@ void UDPWrap::OnRecv(uv_udp_t* handle, wrap->MakeCallback(env->onmessage_string(), arraysize(argv), argv); } - -Local UDPWrap::Instantiate(Environment* env, - AsyncWrap* parent, - UDPWrap::SocketType type) { - EscapableHandleScope scope(env->isolate()); +MaybeLocal UDPWrap::Instantiate(Environment* env, + AsyncWrap* parent, + UDPWrap::SocketType type) { AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(parent); // If this assert fires then Initialize hasn't been called yet. CHECK_EQ(env->udp_constructor_function().IsEmpty(), false); - Local instance = env->udp_constructor_function() - ->NewInstance(env->context()).ToLocalChecked(); - return scope.Escape(instance); + return env->udp_constructor_function()->NewInstance(env->context()); } diff --git a/src/udp_wrap.h b/src/udp_wrap.h index 6347cdea87b450..97d95b57d3dd84 100644 --- a/src/udp_wrap.h +++ b/src/udp_wrap.h @@ -61,9 +61,9 @@ class UDPWrap: public HandleWrap { static void SetTTL(const v8::FunctionCallbackInfo& args); static void BufferSize(const v8::FunctionCallbackInfo& args); - static v8::Local Instantiate(Environment* env, - AsyncWrap* parent, - SocketType type); + static v8::MaybeLocal Instantiate(Environment* env, + AsyncWrap* parent, + SocketType type); SET_NO_MEMORY_INFO() SET_MEMORY_INFO_NAME(UDPWrap) SET_SELF_SIZE(UDPWrap)