Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 11 additions & 18 deletions src/module_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -757,8 +757,15 @@ void ModuleWrap::Evaluate(const FunctionCallbackInfo<Value>& args) {
bool timed_out = false;
bool received_signal = false;
MaybeLocal<Value> result;
auto run = [&]() {
MaybeLocal<Value> result = module->Evaluate(context);
{
auto wd = timeout != -1
? std::make_optional<Watchdog>(isolate, timeout, &timed_out)
: std::nullopt;
auto swd = break_on_sigint ? std::make_optional<SigintWatchdog>(
isolate, &received_signal)
: std::nullopt;

result = module->Evaluate(context);

Local<Value> res;
if (result.ToLocal(&res) && microtask_queue) {
Expand Down Expand Up @@ -792,29 +799,15 @@ void ModuleWrap::Evaluate(const FunctionCallbackInfo<Value>& args) {
Local<Context> outer_context = isolate->GetCurrentContext();
Local<Promise::Resolver> resolver;
if (!Promise::Resolver::New(outer_context).ToLocal(&resolver)) {
return MaybeLocal<Value>();
result = {};
}
if (resolver->Resolve(outer_context, res).IsNothing()) {
return MaybeLocal<Value>();
result = {};
}
result = resolver->GetPromise();

microtask_queue->PerformCheckpoint(isolate);
}
return result;
};
if (break_on_sigint && timeout != -1) {
Watchdog wd(isolate, timeout, &timed_out);
SigintWatchdog swd(isolate, &received_signal);
result = run();
} else if (break_on_sigint) {
SigintWatchdog swd(isolate, &received_signal);
result = run();
} else if (timeout != -1) {
Watchdog wd(isolate, timeout, &timed_out);
result = run();
} else {
result = run();
}

if (result.IsEmpty()) {
Expand Down
25 changes: 9 additions & 16 deletions src/node_contextify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1296,24 +1296,17 @@ bool ContextifyScript::EvalMachine(Local<Context> context,
MaybeLocal<Value> result;
bool timed_out = false;
bool received_signal = false;
auto run = [&]() {
MaybeLocal<Value> result = script->Run(context);
{
auto wd = timeout != -1 ? std::make_optional<Watchdog>(
env->isolate(), timeout, &timed_out)
: std::nullopt;
auto swd = break_on_sigint ? std::make_optional<SigintWatchdog>(
env->isolate(), &received_signal)
: std::nullopt;

result = script->Run(context);
if (!result.IsEmpty() && mtask_queue != nullptr)
mtask_queue->PerformCheckpoint(env->isolate());
return result;
};
if (break_on_sigint && timeout != -1) {
Watchdog wd(env->isolate(), timeout, &timed_out);
SigintWatchdog swd(env->isolate(), &received_signal);
result = run();
} else if (break_on_sigint) {
SigintWatchdog swd(env->isolate(), &received_signal);
result = run();
} else if (timeout != -1) {
Watchdog wd(env->isolate(), timeout, &timed_out);
result = run();
} else {
result = run();
}

// Convert the termination exception into a regular exception.
Expand Down
10 changes: 10 additions & 0 deletions src/node_watchdog.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ class Watchdog {
v8::Isolate* isolate() { return isolate_; }

private:
Watchdog(const Watchdog&) = delete;
Watchdog& operator=(const Watchdog&) = delete;
Watchdog(Watchdog&&) = delete;
Watchdog& operator=(Watchdog&&) = delete;

static void Run(void* arg);
static void Timer(uv_timer_t* timer);

Expand All @@ -77,6 +82,11 @@ class SigintWatchdog : public SigintWatchdogBase {
SignalPropagation HandleSigint() override;

private:
SigintWatchdog(const SigintWatchdog&) = delete;
SigintWatchdog& operator=(const SigintWatchdog&) = delete;
SigintWatchdog(SigintWatchdog&&) = delete;
SigintWatchdog& operator=(SigintWatchdog&&) = delete;

v8::Isolate* isolate_;
bool* received_signal_;
};
Expand Down
Loading