src: simplify watchdog instantiations via `std::optional` · nodejs/node@f14ed5a

Original file line numberDiff line numberDiff line change

@@ -757,8 +757,15 @@ void ModuleWrap::Evaluate(const FunctionCallbackInfo<Value>& args) {

757757

bool timed_out = false;

758758

bool received_signal = false;

759759

MaybeLocal<Value> result;

760-

auto run = [&]() {

761-

MaybeLocal<Value> result = module->Evaluate(context);

760+

{

761+

auto wd = timeout != -1

762+

? std::make_optional<Watchdog>(isolate, timeout, &timed_out)

763+

: std::nullopt;

764+

auto swd = break_on_sigint ? std::make_optional<SigintWatchdog>(

765+

isolate, &received_signal)

766+

: std::nullopt;

767+
768+

result = module->Evaluate(context);

762769
763770

Local<Value> res;

764771

if (result.ToLocal(&res) && microtask_queue) {

@@ -792,29 +799,15 @@ void ModuleWrap::Evaluate(const FunctionCallbackInfo<Value>& args) {

792799

Local<Context> outer_context = isolate->GetCurrentContext();

793800

Local<Promise::Resolver> resolver;

794801

if (!Promise::Resolver::New(outer_context).ToLocal(&resolver)) {

795-

return MaybeLocal<Value>();

802+

result = {};

796803

}

797804

if (resolver->Resolve(outer_context, res).IsNothing()) {

798-

return MaybeLocal<Value>();

805+

result = {};

799806

}

800807

result = resolver->GetPromise();

801808
802809

microtask_queue->PerformCheckpoint(isolate);

803810

}

804-

return result;

805-

};

806-

if (break_on_sigint && timeout != -1) {

807-

Watchdog wd(isolate, timeout, &timed_out);

808-

SigintWatchdog swd(isolate, &received_signal);

809-

result = run();

810-

} else if (break_on_sigint) {

811-

SigintWatchdog swd(isolate, &received_signal);

812-

result = run();

813-

} else if (timeout != -1) {

814-

Watchdog wd(isolate, timeout, &timed_out);

815-

result = run();

816-

} else {

817-

result = run();

818811

}

819812
820813

if (result.IsEmpty()) {