Skip to content

Commit 0a7f850

Browse files
RaisinTenjasnell
authored andcommitted
src: return Maybe from a couple of functions
Functions affected: * InitializeContext() * InitializeContextForSnapshot() * InitializePrimordials() Signed-off-by: Darshan Sen <[email protected]> PR-URL: #39603 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 26632c9 commit 0a7f850

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

src/api/environment.cc

+19-15
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;
@@ -501,7 +504,7 @@ MaybeLocal<Object> GetPerContextExports(Local<Context> context) {
501504

502505
Local<Object> exports = Object::New(isolate);
503506
if (context->Global()->SetPrivate(context, key, exports).IsNothing() ||
504-
!InitializePrimordials(context))
507+
InitializePrimordials(context).IsNothing())
505508
return MaybeLocal<Object>();
506509
return handle_scope.Escape(exports);
507510
}
@@ -514,7 +517,7 @@ Local<Context> NewContext(Isolate* isolate,
514517
auto context = Context::New(isolate, nullptr, object_template);
515518
if (context.IsEmpty()) return context;
516519

517-
if (!InitializeContext(context)) {
520+
if (InitializeContext(context).IsNothing()) {
518521
return Local<Context>();
519522
}
520523

@@ -581,16 +584,17 @@ void InitializeContextRuntime(Local<Context> context) {
581584
}
582585
}
583586

584-
bool InitializeContextForSnapshot(Local<Context> context) {
587+
Maybe<bool> InitializeContextForSnapshot(Local<Context> context) {
585588
Isolate* isolate = context->GetIsolate();
586589
HandleScope handle_scope(isolate);
587590

588591
context->SetEmbedderData(ContextEmbedderIndex::kAllowWasmCodeGeneration,
589592
True(isolate));
593+
590594
return InitializePrimordials(context);
591595
}
592596

593-
bool InitializePrimordials(Local<Context> context) {
597+
Maybe<bool> InitializePrimordials(Local<Context> context) {
594598
// Run per-context JS files.
595599
Isolate* isolate = context->GetIsolate();
596600
Context::Scope context_scope(context);
@@ -603,10 +607,10 @@ bool InitializePrimordials(Local<Context> context) {
603607

604608
// Create primordials first and make it available to per-context scripts.
605609
Local<Object> primordials = Object::New(isolate);
606-
if (!primordials->SetPrototype(context, Null(isolate)).FromJust() ||
610+
if (primordials->SetPrototype(context, Null(isolate)).IsNothing() ||
607611
!GetPerContextExports(context).ToLocal(&exports) ||
608-
!exports->Set(context, primordials_string, primordials).FromJust()) {
609-
return false;
612+
exports->Set(context, primordials_string, primordials).IsNothing()) {
613+
return Nothing<bool>();
610614
}
611615

612616
static const char* context_files[] = {"internal/per_context/primordials",
@@ -623,27 +627,27 @@ bool InitializePrimordials(Local<Context> context) {
623627
context, *module, &parameters, nullptr);
624628
Local<Function> fn;
625629
if (!maybe_fn.ToLocal(&fn)) {
626-
return false;
630+
return Nothing<bool>();
627631
}
628632
MaybeLocal<Value> result =
629633
fn->Call(context, Undefined(isolate), arraysize(arguments), arguments);
630634
// Execution failed during context creation.
631-
// TODO(joyeecheung): deprecate this signature and return a MaybeLocal.
632635
if (result.IsEmpty()) {
633-
return false;
636+
return Nothing<bool>();
634637
}
635638
}
636639

637-
return true;
640+
return Just(true);
638641
}
639642

640-
bool InitializeContext(Local<Context> context) {
641-
if (!InitializeContextForSnapshot(context)) {
642-
return false;
643+
Maybe<bool> InitializeContext(Local<Context> context) {
644+
if (InitializeContextForSnapshot(context).IsNothing()) {
645+
return Nothing<bool>();
643646
}
644647

645648
InitializeContextRuntime(context);
646-
return true;
649+
650+
return Just(true);
647651
}
648652

649653
uv_loop_t* GetCurrentEventLoop(Isolate* isolate) {

src/node.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ NODE_EXTERN v8::Local<v8::Context> NewContext(
367367

368368
// Runs Node.js-specific tweaks on an already constructed context
369369
// Return value indicates success of operation
370-
NODE_EXTERN bool InitializeContext(v8::Local<v8::Context> context);
370+
NODE_EXTERN v8::Maybe<bool> InitializeContext(v8::Local<v8::Context> context);
371371

372372
// If `platform` is passed, it will be used to register new Worker instances.
373373
// It can be `nullptr`, in which case creating new Workers inside of

src/node_internals.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,9 @@ 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>);
96-
bool InitializePrimordials(v8::Local<v8::Context> context);
95+
// TODO(RaisinTen): return a v8::Maybe<bool>.
96+
void InitializeContextRuntime(v8::Local<v8::Context> context);
97+
v8::Maybe<bool> InitializePrimordials(v8::Local<v8::Context> context);
9798

9899
class NodeArrayBufferAllocator : public ArrayBufferAllocator {
99100
public:

0 commit comments

Comments
 (0)