diff options
author | Darshan Sen <darshan.sen@postman.com> | 2021-08-07 14:04:03 +0530 |
---|---|---|
committer | Michaƫl Zasso <targos@protonmail.com> | 2021-08-23 13:03:53 +0200 |
commit | 3a8399ee6192e296b8e493962f7a48c84db32204 (patch) | |
tree | 050912294b99f264746c439a4a1df1b560f4911e /src | |
parent | d82ee968613b5954a36a98c80e61eb0719f4b342 (diff) | |
download | node-new-3a8399ee6192e296b8e493962f7a48c84db32204.tar.gz |
src: return Maybe<bool> from InitializeContextRuntime()
Signed-off-by: Darshan Sen <darshan.sen@postman.com>
PR-URL: https://github.com/nodejs/node/pull/39695
Backport-PR-URL: https://github.com/nodejs/node/pull/39834
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/api/environment.cc | 123 | ||||
-rw-r--r-- | src/node_contextify.cc | 4 | ||||
-rw-r--r-- | src/node_internals.h | 2 | ||||
-rw-r--r-- | src/node_main_instance.cc | 2 |
4 files changed, 95 insertions, 36 deletions
diff --git a/src/api/environment.cc b/src/api/environment.cc index b7e213602b..0fb750c5ab 100644 --- a/src/api/environment.cc +++ b/src/api/environment.cc @@ -20,8 +20,11 @@ using v8::Function; using v8::FunctionCallbackInfo; using v8::HandleScope; using v8::Isolate; +using v8::Just; using v8::Local; +using v8::Maybe; using v8::MaybeLocal; +using v8::Nothing; using v8::Null; using v8::Object; using v8::ObjectTemplate; @@ -523,58 +526,113 @@ void ProtoThrower(const FunctionCallbackInfo<Value>& info) { // This runs at runtime, regardless of whether the context // is created from a snapshot. -void InitializeContextRuntime(Local<Context> context) { +Maybe<bool> InitializeContextRuntime(Local<Context> context) { Isolate* isolate = context->GetIsolate(); HandleScope handle_scope(isolate); // Delete `Intl.v8BreakIterator` // https://github.com/nodejs/node/issues/14909 - Local<String> intl_string = FIXED_ONE_BYTE_STRING(isolate, "Intl"); - Local<String> break_iter_string = - FIXED_ONE_BYTE_STRING(isolate, "v8BreakIterator"); - Local<Value> intl_v; - if (context->Global()->Get(context, intl_string).ToLocal(&intl_v) && - intl_v->IsObject()) { - Local<Object> intl = intl_v.As<Object>(); - intl->Delete(context, break_iter_string).Check(); + { + Local<String> intl_string = + FIXED_ONE_BYTE_STRING(isolate, "Intl"); + Local<String> break_iter_string = + FIXED_ONE_BYTE_STRING(isolate, "v8BreakIterator"); + + Local<Value> intl_v; + if (!context->Global() + ->Get(context, intl_string) + .ToLocal(&intl_v)) { + return Nothing<bool>(); + } + + if (intl_v->IsObject() && + intl_v.As<Object>() + ->Delete(context, break_iter_string) + .IsNothing()) { + return Nothing<bool>(); + } } // Delete `Atomics.wake` // https://github.com/nodejs/node/issues/21219 - Local<String> atomics_string = FIXED_ONE_BYTE_STRING(isolate, "Atomics"); - Local<String> wake_string = FIXED_ONE_BYTE_STRING(isolate, "wake"); - Local<Value> atomics_v; - if (context->Global()->Get(context, atomics_string).ToLocal(&atomics_v) && - atomics_v->IsObject()) { - Local<Object> atomics = atomics_v.As<Object>(); - atomics->Delete(context, wake_string).Check(); + { + Local<String> atomics_string = + FIXED_ONE_BYTE_STRING(isolate, "Atomics"); + Local<String> wake_string = + FIXED_ONE_BYTE_STRING(isolate, "wake"); + + Local<Value> atomics_v; + if (!context->Global() + ->Get(context, atomics_string) + .ToLocal(&atomics_v)) { + return Nothing<bool>(); + } + + if (atomics_v->IsObject() && + atomics_v.As<Object>() + ->Delete(context, wake_string) + .IsNothing()) { + return Nothing<bool>(); + } } // Remove __proto__ // https://github.com/nodejs/node/issues/31951 - Local<String> object_string = FIXED_ONE_BYTE_STRING(isolate, "Object"); - Local<String> prototype_string = FIXED_ONE_BYTE_STRING(isolate, "prototype"); - Local<Object> prototype = context->Global() - ->Get(context, object_string) - .ToLocalChecked() - .As<Object>() - ->Get(context, prototype_string) - .ToLocalChecked() - .As<Object>(); - Local<String> proto_string = FIXED_ONE_BYTE_STRING(isolate, "__proto__"); + Local<Object> prototype; + { + Local<String> object_string = + FIXED_ONE_BYTE_STRING(isolate, "Object"); + Local<String> prototype_string = + FIXED_ONE_BYTE_STRING(isolate, "prototype"); + + Local<Value> object_v; + if (!context->Global() + ->Get(context, object_string) + .ToLocal(&object_v)) { + return Nothing<bool>(); + } + + Local<Value> prototype_v; + if (!object_v.As<Object>() + ->Get(context, prototype_string) + .ToLocal(&prototype_v)) { + return Nothing<bool>(); + } + + prototype = prototype_v.As<Object>(); + } + + Local<String> 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<bool>(); + } } else if (per_process::cli_options->disable_proto == "throw") { - Local<Value> thrower = - Function::New(context, ProtoThrower).ToLocalChecked(); + Local<Value> thrower; + if (!Function::New(context, ProtoThrower) + .ToLocal(&thrower)) { + return Nothing<bool>(); + } + 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<bool>(); + } } 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); } bool InitializeContextForSnapshot(Local<Context> context) { @@ -638,8 +696,7 @@ bool InitializeContext(Local<Context> context) { return false; } - InitializeContextRuntime(context); - return true; + return InitializeContextRuntime(context).IsJust(); } uv_loop_t* GetCurrentEventLoop(Isolate* isolate) { diff --git a/src/node_contextify.cc b/src/node_contextify.cc index ae5fc2fbf1..482c9a7b51 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -210,7 +210,9 @@ MaybeLocal<Context> ContextifyContext::CreateV8Context( if (ctx.IsEmpty()) return MaybeLocal<Context>(); // Only partially initialize the context - the primordials are left out // and only initialized when necessary. - InitializeContextRuntime(ctx); + if (InitializeContextRuntime(ctx).IsNothing()) { + return MaybeLocal<Context>(); + } if (ctx.IsEmpty()) { return MaybeLocal<Context>(); diff --git a/src/node_internals.h b/src/node_internals.h index 8f7929994f..dec024bd79 100644 --- a/src/node_internals.h +++ b/src/node_internals.h @@ -92,7 +92,7 @@ void SignalExit(int signal, siginfo_t* info, void* ucontext); std::string GetProcessTitle(const char* default_title); std::string GetHumanReadableProcessName(); -void InitializeContextRuntime(v8::Local<v8::Context>); +v8::Maybe<bool> InitializeContextRuntime(v8::Local<v8::Context> context); bool InitializePrimordials(v8::Local<v8::Context> context); class NodeArrayBufferAllocator : public ArrayBufferAllocator { diff --git a/src/node_main_instance.cc b/src/node_main_instance.cc index f232cd6a89..ce319cca3e 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 |