Skip to content

Commit 3a8399e

Browse files
RaisinTentargos
authored andcommitted
src: return Maybe<bool> from InitializeContextRuntime()
Signed-off-by: Darshan Sen <darshan.sen@postman.com> PR-URL: #39695 Backport-PR-URL: #39834 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent d82ee96 commit 3a8399e

File tree

4 files changed

+95
-36
lines changed

4 files changed

+95
-36
lines changed

src/api/environment.cc

Lines changed: 90 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@ using v8::Function;
2020
using v8::FunctionCallbackInfo;
2121
using v8::HandleScope;
2222
using v8::Isolate;
23+
using v8::Just;
2324
using v8::Local;
25+
using v8::Maybe;
2426
using v8::MaybeLocal;
27+
using v8::Nothing;
2528
using v8::Null;
2629
using v8::Object;
2730
using v8::ObjectTemplate;
@@ -523,58 +526,113 @@ void ProtoThrower(const FunctionCallbackInfo<Value>& info) {
523526

524527
// This runs at runtime, regardless of whether the context
525528
// is created from a snapshot.
526-
void InitializeContextRuntime(Local<Context> context) {
529+
Maybe<bool> InitializeContextRuntime(Local<Context> context) {
527530
Isolate* isolate = context->GetIsolate();
528531
HandleScope handle_scope(isolate);
529532

530533
// Delete `Intl.v8BreakIterator`
531534
// https://github.com/nodejs/node/issues/14909
532-
Local<String> intl_string = FIXED_ONE_BYTE_STRING(isolate, "Intl");
533-
Local<String> break_iter_string =
534-
FIXED_ONE_BYTE_STRING(isolate, "v8BreakIterator");
535-
Local<Value> intl_v;
536-
if (context->Global()->Get(context, intl_string).ToLocal(&intl_v) &&
537-
intl_v->IsObject()) {
538-
Local<Object> intl = intl_v.As<Object>();
539-
intl->Delete(context, break_iter_string).Check();
535+
{
536+
Local<String> intl_string =
537+
FIXED_ONE_BYTE_STRING(isolate, "Intl");
538+
Local<String> break_iter_string =
539+
FIXED_ONE_BYTE_STRING(isolate, "v8BreakIterator");
540+
541+
Local<Value> intl_v;
542+
if (!context->Global()
543+
->Get(context, intl_string)
544+
.ToLocal(&intl_v)) {
545+
return Nothing<bool>();
546+
}
547+
548+
if (intl_v->IsObject() &&
549+
intl_v.As<Object>()
550+
->Delete(context, break_iter_string)
551+
.IsNothing()) {
552+
return Nothing<bool>();
553+
}
540554
}
541555

542556
// Delete `Atomics.wake`
543557
// https://github.com/nodejs/node/issues/21219
544-
Local<String> atomics_string = FIXED_ONE_BYTE_STRING(isolate, "Atomics");
545-
Local<String> wake_string = FIXED_ONE_BYTE_STRING(isolate, "wake");
546-
Local<Value> atomics_v;
547-
if (context->Global()->Get(context, atomics_string).ToLocal(&atomics_v) &&
548-
atomics_v->IsObject()) {
549-
Local<Object> atomics = atomics_v.As<Object>();
550-
atomics->Delete(context, wake_string).Check();
558+
{
559+
Local<String> atomics_string =
560+
FIXED_ONE_BYTE_STRING(isolate, "Atomics");
561+
Local<String> wake_string =
562+
FIXED_ONE_BYTE_STRING(isolate, "wake");
563+
564+
Local<Value> atomics_v;
565+
if (!context->Global()
566+
->Get(context, atomics_string)
567+
.ToLocal(&atomics_v)) {
568+
return Nothing<bool>();
569+
}
570+
571+
if (atomics_v->IsObject() &&
572+
atomics_v.As<Object>()
573+
->Delete(context, wake_string)
574+
.IsNothing()) {
575+
return Nothing<bool>();
576+
}
551577
}
552578

553579
// Remove __proto__
554580
// https://github.com/nodejs/node/issues/31951
555-
Local<String> object_string = FIXED_ONE_BYTE_STRING(isolate, "Object");
556-
Local<String> prototype_string = FIXED_ONE_BYTE_STRING(isolate, "prototype");
557-
Local<Object> prototype = context->Global()
558-
->Get(context, object_string)
559-
.ToLocalChecked()
560-
.As<Object>()
561-
->Get(context, prototype_string)
562-
.ToLocalChecked()
563-
.As<Object>();
564-
Local<String> proto_string = FIXED_ONE_BYTE_STRING(isolate, "__proto__");
581+
Local<Object> prototype;
582+
{
583+
Local<String> object_string =
584+
FIXED_ONE_BYTE_STRING(isolate, "Object");
585+
Local<String> prototype_string =
586+
FIXED_ONE_BYTE_STRING(isolate, "prototype");
587+
588+
Local<Value> object_v;
589+
if (!context->Global()
590+
->Get(context, object_string)
591+
.ToLocal(&object_v)) {
592+
return Nothing<bool>();
593+
}
594+
595+
Local<Value> prototype_v;
596+
if (!object_v.As<Object>()
597+
->Get(context, prototype_string)
598+
.ToLocal(&prototype_v)) {
599+
return Nothing<bool>();
600+
}
601+
602+
prototype = prototype_v.As<Object>();
603+
}
604+
605+
Local<String> proto_string =
606+
FIXED_ONE_BYTE_STRING(isolate, "__proto__");
607+
565608
if (per_process::cli_options->disable_proto == "delete") {
566-
prototype->Delete(context, proto_string).ToChecked();
609+
if (prototype
610+
->Delete(context, proto_string)
611+
.IsNothing()) {
612+
return Nothing<bool>();
613+
}
567614
} else if (per_process::cli_options->disable_proto == "throw") {
568-
Local<Value> thrower =
569-
Function::New(context, ProtoThrower).ToLocalChecked();
615+
Local<Value> thrower;
616+
if (!Function::New(context, ProtoThrower)
617+
.ToLocal(&thrower)) {
618+
return Nothing<bool>();
619+
}
620+
570621
PropertyDescriptor descriptor(thrower, thrower);
571622
descriptor.set_enumerable(false);
572623
descriptor.set_configurable(true);
573-
prototype->DefineProperty(context, proto_string, descriptor).ToChecked();
624+
if (prototype
625+
->DefineProperty(context, proto_string, descriptor)
626+
.IsNothing()) {
627+
return Nothing<bool>();
628+
}
574629
} else if (per_process::cli_options->disable_proto != "") {
575630
// Validated in ProcessGlobalArgs
576-
FatalError("InitializeContextRuntime()", "invalid --disable-proto mode");
631+
FatalError("InitializeContextRuntime()",
632+
"invalid --disable-proto mode");
577633
}
634+
635+
return Just(true);
578636
}
579637

580638
bool InitializeContextForSnapshot(Local<Context> context) {
@@ -638,8 +696,7 @@ bool InitializeContext(Local<Context> context) {
638696
return false;
639697
}
640698

641-
InitializeContextRuntime(context);
642-
return true;
699+
return InitializeContextRuntime(context).IsJust();
643700
}
644701

645702
uv_loop_t* GetCurrentEventLoop(Isolate* isolate) {

src/node_contextify.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,9 @@ MaybeLocal<Context> ContextifyContext::CreateV8Context(
210210
if (ctx.IsEmpty()) return MaybeLocal<Context>();
211211
// Only partially initialize the context - the primordials are left out
212212
// and only initialized when necessary.
213-
InitializeContextRuntime(ctx);
213+
if (InitializeContextRuntime(ctx).IsNothing()) {
214+
return MaybeLocal<Context>();
215+
}
214216

215217
if (ctx.IsEmpty()) {
216218
return MaybeLocal<Context>();

src/node_internals.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ void SignalExit(int signal, siginfo_t* info, void* ucontext);
9292
std::string GetProcessTitle(const char* default_title);
9393
std::string GetHumanReadableProcessName();
9494

95-
void InitializeContextRuntime(v8::Local<v8::Context>);
95+
v8::Maybe<bool> InitializeContextRuntime(v8::Local<v8::Context> context);
9696
bool InitializePrimordials(v8::Local<v8::Context> context);
9797

9898
class NodeArrayBufferAllocator : public ArrayBufferAllocator {

src/node_main_instance.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ NodeMainInstance::CreateMainEnvironment(int* exit_code,
198198

199199
CHECK(!context.IsEmpty());
200200
Context::Scope context_scope(context);
201-
InitializeContextRuntime(context);
201+
CHECK(InitializeContextRuntime(context).IsJust());
202202
SetIsolateErrorHandlers(isolate_, {});
203203
env->InitializeMainContext(context, env_info);
204204
#if HAVE_INSPECTOR

0 commit comments

Comments
 (0)