Skip to content

Commit

Permalink
src: return MaybeLocal from a couple of functions
Browse files Browse the repository at this point in the history
Functions affected:
* InitializeContext()
* InitializeContextForSnapshot()
* InitializePrimordials()

Signed-off-by: Darshan Sen <darshan.sen@postman.com>
  • Loading branch information
RaisinTen committed Aug 1, 2021
1 parent 8be3b91 commit cbeeeba
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
33 changes: 19 additions & 14 deletions src/api/environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ MaybeLocal<Object> GetPerContextExports(Local<Context> context) {

Local<Object> exports = Object::New(isolate);
if (context->Global()->SetPrivate(context, key, exports).IsNothing() ||
!InitializePrimordials(context))
InitializePrimordials(context).IsEmpty())
return MaybeLocal<Object>();
return handle_scope.Escape(exports);
}
Expand All @@ -514,7 +514,7 @@ Local<Context> 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<Context>();
}

Expand Down Expand Up @@ -581,16 +581,21 @@ void InitializeContextRuntime(Local<Context> context) {
}
}

bool InitializeContextForSnapshot(Local<Context> context) {
MaybeLocal<Context> InitializeContextForSnapshot(Local<Context> context) {
Isolate* isolate = context->GetIsolate();
HandleScope handle_scope(isolate);

context->SetEmbedderData(ContextEmbedderIndex::kAllowWasmCodeGeneration,
True(isolate));
return InitializePrimordials(context);

if (InitializePrimordials(context).IsEmpty()) {
return MaybeLocal<Context>();
}

return context;
}

bool InitializePrimordials(Local<Context> context) {
MaybeLocal<Object> InitializePrimordials(Local<Context> context) {
// Run per-context JS files.
Isolate* isolate = context->GetIsolate();
Context::Scope context_scope(context);
Expand All @@ -606,7 +611,7 @@ bool InitializePrimordials(Local<Context> context) {
if (!primordials->SetPrototype(context, Null(isolate)).FromJust() ||
!GetPerContextExports(context).ToLocal(&exports) ||
!exports->Set(context, primordials_string, primordials).FromJust()) {
return false;
return MaybeLocal<Object>();
}

static const char* context_files[] = {"internal/per_context/primordials",
Expand All @@ -623,27 +628,27 @@ bool InitializePrimordials(Local<Context> context) {
context, *module, &parameters, nullptr);
Local<Function> fn;
if (!maybe_fn.ToLocal(&fn)) {
return false;
return MaybeLocal<Object>();
}
MaybeLocal<Value> 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<Object>();
}
}

return true;
return primordials;
}

bool InitializeContext(Local<Context> context) {
if (!InitializeContextForSnapshot(context)) {
return false;
MaybeLocal<Context> InitializeContext(Local<Context> context) {
if (InitializeContextForSnapshot(context).IsEmpty()) {
return MaybeLocal<Context>();
}

InitializeContextRuntime(context);
return true;

return context;
}

uv_loop_t* GetCurrentEventLoop(Isolate* isolate) {
Expand Down
3 changes: 2 additions & 1 deletion src/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,8 @@ NODE_EXTERN v8::Local<v8::Context> NewContext(

// Runs Node.js-specific tweaks on an already constructed context
// Return value indicates success of operation
NODE_EXTERN bool InitializeContext(v8::Local<v8::Context> context);
NODE_EXTERN v8::MaybeLocal<v8::Context> InitializeContext(
v8::Local<v8::Context> 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
Expand Down
6 changes: 4 additions & 2 deletions src/node_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<v8::Context>);
bool InitializePrimordials(v8::Local<v8::Context> context);
// TODO(RaisinTen): return a MaybeLocal.
void InitializeContextRuntime(v8::Local<v8::Context> context);
v8::MaybeLocal<v8::Object> InitializePrimordials(
v8::Local<v8::Context> context);

class NodeArrayBufferAllocator : public ArrayBufferAllocator {
public:
Expand Down

0 comments on commit cbeeeba

Please sign in to comment.