From 5bd551acd82c94877b596035d15743ebce16d8c0 Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Sat, 7 Aug 2021 14:04:03 +0530 Subject: [PATCH] src: return Maybe from InitializeContextRuntime() Signed-off-by: Darshan Sen --- src/api/environment.cc | 121 +++++++++++++++++++++++++++----------- src/node_contextify.cc | 4 +- src/node_internals.h | 3 +- src/node_main_instance.cc | 2 +- 4 files changed, 92 insertions(+), 38 deletions(-) diff --git a/src/api/environment.cc b/src/api/environment.cc index 2b9eab2bc0b236..1ec1e8bc8c1896 100644 --- a/src/api/environment.cc +++ b/src/api/environment.cc @@ -530,58 +530,113 @@ void ProtoThrower(const FunctionCallbackInfo& info) { // This runs at runtime, regardless of whether the context // is created from a snapshot. -void InitializeContextRuntime(Local context) { +Maybe InitializeContextRuntime(Local context) { Isolate* isolate = context->GetIsolate(); HandleScope handle_scope(isolate); // Delete `Intl.v8BreakIterator` // https://github.com/nodejs/node/issues/14909 - Local intl_string = FIXED_ONE_BYTE_STRING(isolate, "Intl"); - Local break_iter_string = - FIXED_ONE_BYTE_STRING(isolate, "v8BreakIterator"); - Local intl_v; - if (context->Global()->Get(context, intl_string).ToLocal(&intl_v) && - intl_v->IsObject()) { - Local intl = intl_v.As(); - intl->Delete(context, break_iter_string).Check(); + { + Local intl_string = + FIXED_ONE_BYTE_STRING(isolate, "Intl"); + Local break_iter_string = + FIXED_ONE_BYTE_STRING(isolate, "v8BreakIterator"); + + Local intl_v; + if (!context->Global() + ->Get(context, intl_string) + .ToLocal(&intl_v)) { + return Nothing(); + } + + if (intl_v->IsObject() && + intl_v.As() + ->Delete(context, break_iter_string) + .IsNothing()) { + return Nothing(); + } } // Delete `Atomics.wake` // https://github.com/nodejs/node/issues/21219 - Local atomics_string = FIXED_ONE_BYTE_STRING(isolate, "Atomics"); - Local wake_string = FIXED_ONE_BYTE_STRING(isolate, "wake"); - Local atomics_v; - if (context->Global()->Get(context, atomics_string).ToLocal(&atomics_v) && - atomics_v->IsObject()) { - Local atomics = atomics_v.As(); - atomics->Delete(context, wake_string).Check(); + { + Local atomics_string = + FIXED_ONE_BYTE_STRING(isolate, "Atomics"); + Local wake_string = + FIXED_ONE_BYTE_STRING(isolate, "wake"); + + Local atomics_v; + if (!context->Global() + ->Get(context, atomics_string) + .ToLocal(&atomics_v)) { + return Nothing(); + } + + if (atomics_v->IsObject() && + atomics_v.As() + ->Delete(context, wake_string) + .IsNothing()) { + return Nothing(); + } } // Remove __proto__ // https://github.com/nodejs/node/issues/31951 - Local object_string = FIXED_ONE_BYTE_STRING(isolate, "Object"); - Local prototype_string = FIXED_ONE_BYTE_STRING(isolate, "prototype"); - Local prototype = context->Global() - ->Get(context, object_string) - .ToLocalChecked() - .As() - ->Get(context, prototype_string) - .ToLocalChecked() - .As(); - Local proto_string = FIXED_ONE_BYTE_STRING(isolate, "__proto__"); + Local prototype; + { + Local object_string = + FIXED_ONE_BYTE_STRING(isolate, "Object"); + Local prototype_string = + FIXED_ONE_BYTE_STRING(isolate, "prototype"); + + Local object_v; + if (!context->Global() + ->Get(context, object_string) + .ToLocal(&object_v)) { + return Nothing(); + } + + Local prototype_v; + if (!object_v.As() + ->Get(context, prototype_string) + .ToLocal(&prototype_v)) { + return Nothing(); + } + + prototype = prototype_v.As(); + } + + Local proto_string = + FIXED_ONE_BYTE_STRING(isolate, "__proto__"); + if (per_process::cli_options->disable_proto == "delete") { - prototype->Delete(context, proto_string).ToChecked(); + if (prototype + ->Delete(context, proto_string) + .IsNothing()) { + return Nothing(); + } } else if (per_process::cli_options->disable_proto == "throw") { - Local thrower = - Function::New(context, ProtoThrower).ToLocalChecked(); + Local thrower; + if (!Function::New(context, ProtoThrower) + .ToLocal(&thrower)) { + return Nothing(); + } + PropertyDescriptor descriptor(thrower, thrower); descriptor.set_enumerable(false); descriptor.set_configurable(true); - prototype->DefineProperty(context, proto_string, descriptor).ToChecked(); + if (prototype + ->DefineProperty(context, proto_string, descriptor) + .IsNothing()) { + return Nothing(); + } } else if (per_process::cli_options->disable_proto != "") { // Validated in ProcessGlobalArgs - FatalError("InitializeContextRuntime()", "invalid --disable-proto mode"); + FatalError("InitializeContextRuntime()", + "invalid --disable-proto mode"); } + + return Just(true); } Maybe InitializeContextForSnapshot(Local context) { @@ -645,9 +700,7 @@ Maybe InitializeContext(Local context) { return Nothing(); } - InitializeContextRuntime(context); - - return Just(true); + return InitializeContextRuntime(context); } uv_loop_t* GetCurrentEventLoop(Isolate* isolate) { diff --git a/src/node_contextify.cc b/src/node_contextify.cc index ae5fc2fbf1a862..482c9a7b51dbf0 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -210,7 +210,9 @@ MaybeLocal ContextifyContext::CreateV8Context( if (ctx.IsEmpty()) return MaybeLocal(); // Only partially initialize the context - the primordials are left out // and only initialized when necessary. - InitializeContextRuntime(ctx); + if (InitializeContextRuntime(ctx).IsNothing()) { + return MaybeLocal(); + } if (ctx.IsEmpty()) { return MaybeLocal(); diff --git a/src/node_internals.h b/src/node_internals.h index 06b2bdefb74a6e..c996723db5700c 100644 --- a/src/node_internals.h +++ b/src/node_internals.h @@ -92,8 +92,7 @@ void SignalExit(int signal, siginfo_t* info, void* ucontext); std::string GetProcessTitle(const char* default_title); std::string GetHumanReadableProcessName(); -// TODO(RaisinTen): return a v8::Maybe. -void InitializeContextRuntime(v8::Local context); +v8::Maybe InitializeContextRuntime(v8::Local context); v8::Maybe InitializePrimordials(v8::Local context); class NodeArrayBufferAllocator : public ArrayBufferAllocator { diff --git a/src/node_main_instance.cc b/src/node_main_instance.cc index f232cd6a89f26a..ce319cca3edca3 100644 --- a/src/node_main_instance.cc +++ b/src/node_main_instance.cc @@ -198,7 +198,7 @@ NodeMainInstance::CreateMainEnvironment(int* exit_code, CHECK(!context.IsEmpty()); Context::Scope context_scope(context); - InitializeContextRuntime(context); + CHECK(InitializeContextRuntime(context).IsJust()); SetIsolateErrorHandlers(isolate_, {}); env->InitializeMainContext(context, env_info); #if HAVE_INSPECTOR