From a160d2b500a105e26a9e6ac3f32d7a42437a1462 Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Sat, 31 Jul 2021 11:53:45 +0530 Subject: [PATCH] src: return MaybeLocal from a couple of functions Functions affected: * InitializeContext() * InitializeContextForSnapshot() * InitializePrimordials() Signed-off-by: Darshan Sen --- src/api/environment.cc | 33 +++++++++++++++++++-------------- src/node.h | 3 ++- src/node_internals.h | 6 ++++-- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/api/environment.cc b/src/api/environment.cc index bd9de560d08120..01c8d84ed495ab 100644 --- a/src/api/environment.cc +++ b/src/api/environment.cc @@ -501,7 +501,7 @@ MaybeLocal GetPerContextExports(Local context) { Local exports = Object::New(isolate); if (context->Global()->SetPrivate(context, key, exports).IsNothing() || - !InitializePrimordials(context)) + InitializePrimordials(context).IsEmpty()) return MaybeLocal(); return handle_scope.Escape(exports); } @@ -514,7 +514,7 @@ Local NewContext(Isolate* isolate, auto context = Context::New(isolate, nullptr, object_template); if (context.IsEmpty()) return context; - if (!InitializeContext(context)) { + if (InitializeContext(context).IsEmpty()) { return Local(); } @@ -581,16 +581,21 @@ void InitializeContextRuntime(Local context) { } } -bool InitializeContextForSnapshot(Local context) { +MaybeLocal InitializeContextForSnapshot(Local context) { Isolate* isolate = context->GetIsolate(); HandleScope handle_scope(isolate); context->SetEmbedderData(ContextEmbedderIndex::kAllowWasmCodeGeneration, True(isolate)); - return InitializePrimordials(context); + + if (InitializePrimordials(context).IsEmpty()) { + return MaybeLocal(); + } + + return context; } -bool InitializePrimordials(Local context) { +MaybeLocal InitializePrimordials(Local context) { // Run per-context JS files. Isolate* isolate = context->GetIsolate(); Context::Scope context_scope(context); @@ -606,7 +611,7 @@ bool InitializePrimordials(Local context) { if (!primordials->SetPrototype(context, Null(isolate)).FromJust() || !GetPerContextExports(context).ToLocal(&exports) || !exports->Set(context, primordials_string, primordials).FromJust()) { - return false; + return MaybeLocal(); } static const char* context_files[] = {"internal/per_context/primordials", @@ -623,27 +628,27 @@ bool InitializePrimordials(Local context) { context, *module, ¶meters, nullptr); Local fn; if (!maybe_fn.ToLocal(&fn)) { - return false; + return MaybeLocal(); } MaybeLocal result = fn->Call(context, Undefined(isolate), arraysize(arguments), arguments); // Execution failed during context creation. - // TODO(joyeecheung): deprecate this signature and return a MaybeLocal. if (result.IsEmpty()) { - return false; + return MaybeLocal(); } } - return true; + return primordials; } -bool InitializeContext(Local context) { - if (!InitializeContextForSnapshot(context)) { - return false; +MaybeLocal InitializeContext(Local context) { + if (InitializeContextForSnapshot(context).IsEmpty()) { + return MaybeLocal(); } InitializeContextRuntime(context); - return true; + + return context; } uv_loop_t* GetCurrentEventLoop(Isolate* isolate) { diff --git a/src/node.h b/src/node.h index 066c0eadfb6b05..75af41839ab1d3 100644 --- a/src/node.h +++ b/src/node.h @@ -367,7 +367,8 @@ NODE_EXTERN v8::Local NewContext( // Runs Node.js-specific tweaks on an already constructed context // Return value indicates success of operation -NODE_EXTERN bool InitializeContext(v8::Local context); +NODE_EXTERN v8::MaybeLocal InitializeContext( + v8::Local context); // If `platform` is passed, it will be used to register new Worker instances. // It can be `nullptr`, in which case creating new Workers inside of diff --git a/src/node_internals.h b/src/node_internals.h index 8f7929994f3243..9c0f34844b469b 100644 --- a/src/node_internals.h +++ b/src/node_internals.h @@ -92,8 +92,10 @@ void SignalExit(int signal, siginfo_t* info, void* ucontext); std::string GetProcessTitle(const char* default_title); std::string GetHumanReadableProcessName(); -void InitializeContextRuntime(v8::Local); -bool InitializePrimordials(v8::Local context); +// TODO(RaisinTen): return a v8::MaybeLocal. +void InitializeContextRuntime(v8::Local context); +v8::MaybeLocal InitializePrimordials( + v8::Local context); class NodeArrayBufferAllocator : public ArrayBufferAllocator { public: