Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: return Maybe<bool> from InitializeContextRuntime() #39695

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 87 additions & 34 deletions src/api/environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -530,58 +530,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);
}

Maybe<bool> InitializeContextForSnapshot(Local<Context> context) {
Expand Down Expand Up @@ -645,9 +700,7 @@ Maybe<bool> InitializeContext(Local<Context> context) {
return Nothing<bool>();
}

InitializeContextRuntime(context);

return Just(true);
return InitializeContextRuntime(context);
}

uv_loop_t* GetCurrentEventLoop(Isolate* isolate) {
Expand Down
4 changes: 3 additions & 1 deletion src/node_contextify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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>();
Expand Down
3 changes: 1 addition & 2 deletions src/node_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool>.
void InitializeContextRuntime(v8::Local<v8::Context> context);
v8::Maybe<bool> InitializeContextRuntime(v8::Local<v8::Context> context);
v8::Maybe<bool> InitializePrimordials(v8::Local<v8::Context> context);

class NodeArrayBufferAllocator : public ArrayBufferAllocator {
Expand Down
2 changes: 1 addition & 1 deletion src/node_main_instance.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down