diff options
author | Darshan Sen <darshan.sen@postman.com> | 2021-08-07 11:34:03 +0300 |
---|---|---|
committer | James M Snell <jasnell@gmail.com> | 2021-08-12 23:53:37 +0300 |
commit | 284f164962c8298ca4c24215a40b5c09d7ec572a (patch) | |
tree | f27434c4ffbbfeb257237341b9f42028a78be715 /src/api | |
parent | c3b4d3f0436239614d51ecfe365a60a9a2ea9095 (diff) |
src: return Maybe<bool> from InitializeContextRuntime()
Signed-off-by: Darshan Sen <darshan.sen@postman.com>
PR-URL: https://github.com/nodejs/node/pull/39695
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'src/api')
-rw-r--r-- | src/api/environment.cc | 121 |
1 files changed, 87 insertions, 34 deletions
diff --git a/src/api/environment.cc b/src/api/environment.cc index 2b9eab2bc0b..1ec1e8bc8c1 100644 --- a/src/api/environment.cc +++ b/src/api/environment.cc @@ -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) { @@ -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) { |