From 0a7f850123f1b85010e0e91ce6ea1af69a20a010 Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Sat, 31 Jul 2021 11:53:45 +0530 Subject: [PATCH] src: return Maybe from a couple of functions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Functions affected: * InitializeContext() * InitializeContextForSnapshot() * InitializePrimordials() Signed-off-by: Darshan Sen PR-URL: https://github.com/nodejs/node/pull/39603 Reviewed-By: Anna Henningsen Reviewed-By: MichaĆ«l Zasso Reviewed-By: Joyee Cheung Reviewed-By: James M Snell --- src/api/environment.cc | 34 +++++++++++++++++++--------------- src/node.h | 2 +- src/node_internals.h | 5 +++-- 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/api/environment.cc b/src/api/environment.cc index bd9de560d08120..2b9eab2bc0b236 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; @@ -501,7 +504,7 @@ MaybeLocal GetPerContextExports(Local context) { Local exports = Object::New(isolate); if (context->Global()->SetPrivate(context, key, exports).IsNothing() || - !InitializePrimordials(context)) + InitializePrimordials(context).IsNothing()) return MaybeLocal(); return handle_scope.Escape(exports); } @@ -514,7 +517,7 @@ Local NewContext(Isolate* isolate, auto context = Context::New(isolate, nullptr, object_template); if (context.IsEmpty()) return context; - if (!InitializeContext(context)) { + if (InitializeContext(context).IsNothing()) { return Local(); } @@ -581,16 +584,17 @@ void InitializeContextRuntime(Local context) { } } -bool InitializeContextForSnapshot(Local context) { +Maybe InitializeContextForSnapshot(Local context) { Isolate* isolate = context->GetIsolate(); HandleScope handle_scope(isolate); context->SetEmbedderData(ContextEmbedderIndex::kAllowWasmCodeGeneration, True(isolate)); + return InitializePrimordials(context); } -bool InitializePrimordials(Local context) { +Maybe InitializePrimordials(Local context) { // Run per-context JS files. Isolate* isolate = context->GetIsolate(); Context::Scope context_scope(context); @@ -603,10 +607,10 @@ bool InitializePrimordials(Local context) { // Create primordials first and make it available to per-context scripts. Local primordials = Object::New(isolate); - if (!primordials->SetPrototype(context, Null(isolate)).FromJust() || + if (primordials->SetPrototype(context, Null(isolate)).IsNothing() || !GetPerContextExports(context).ToLocal(&exports) || - !exports->Set(context, primordials_string, primordials).FromJust()) { - return false; + exports->Set(context, primordials_string, primordials).IsNothing()) { + return Nothing(); } static const char* context_files[] = {"internal/per_context/primordials", @@ -623,27 +627,27 @@ bool InitializePrimordials(Local context) { context, *module, ¶meters, nullptr); Local fn; if (!maybe_fn.ToLocal(&fn)) { - return false; + return Nothing(); } 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 Nothing(); } } - return true; + return Just(true); } -bool InitializeContext(Local context) { - if (!InitializeContextForSnapshot(context)) { - return false; +Maybe InitializeContext(Local context) { + if (InitializeContextForSnapshot(context).IsNothing()) { + return Nothing(); } InitializeContextRuntime(context); - return true; + + return Just(true); } uv_loop_t* GetCurrentEventLoop(Isolate* isolate) { diff --git a/src/node.h b/src/node.h index 066c0eadfb6b05..1628a2147ea203 100644 --- a/src/node.h +++ b/src/node.h @@ -367,7 +367,7 @@ 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::Maybe 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..06b2bdefb74a6e 100644 --- a/src/node_internals.h +++ b/src/node_internals.h @@ -92,8 +92,9 @@ 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::Maybe. +void InitializeContextRuntime(v8::Local context); +v8::Maybe InitializePrimordials(v8::Local context); class NodeArrayBufferAllocator : public ArrayBufferAllocator { public: