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
8 changes: 8 additions & 0 deletions builtins/web/timers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,12 @@ bool install(api::Engine *engine) {
return JS_DefineFunctions(engine->cx(), engine->global(), methods);
}

void reset() {
for (auto &[id, timer] : TIMERS_MAP->timers_) {
timer->cancel(ENGINE);
}
TIMERS_MAP->timers_.clear();
TIMERS_MAP->next_timer_id = 1;
}

} // namespace builtins::web::timers
1 change: 1 addition & 0 deletions builtins/web/timers.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ bool set_interval(JSContext *cx, HandleObject handler, JS::HandleValueVector arg
void clear_timeout_or_interval(int32_t timer_id);

bool install(api::Engine *engine);
void reset();

} // namespace builtins::web::timers

Expand Down
1 change: 1 addition & 0 deletions include/extension-api.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ class Engine {
void report_unhandled_promise_rejections();
static void clear_unhandled_promise_rejections();

void reset();
void abort(const char *reason);

static bool debug_logging_enabled();
Expand Down
9 changes: 9 additions & 0 deletions runtime/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "debugger.h"
#include "encode.h"
#include "event_loop.h"
#include "../builtins/web/timers.h"
#include "script_loader.h"

#include "js/CompilationAndEvaluation.h"
Expand Down Expand Up @@ -708,3 +709,11 @@ void Engine::report_unhandled_promise_rejections() {
void Engine::clear_unhandled_promise_rejections() {
JS::SetClear(CONTEXT, unhandledRejectedPromises);
}
void Engine::reset() {
clear_unhandled_promise_rejections();
core::EventLoop::reset(this);
builtins::web::timers::reset();
core::EventLoop::reset(this);
JS::PrepareForFullGC(cx());
JS::NonIncrementalGC(cx(), JS::GCOptions::Normal, JS::GCReason::API);
}
9 changes: 9 additions & 0 deletions runtime/event_loop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,13 @@ bool EventLoop::run_event_loop(api::Engine *engine, double total_compute) {

void EventLoop::init(JSContext *cx) { queue.init(cx); }

void EventLoop::reset(api::Engine *engine) {
auto &q = queue.get();
for (const auto& task : q.tasks) {
task->cancel(engine);
}
q.tasks.clear();
q.interest_cnt = 0;
}

} // namespace core
5 changes: 5 additions & 0 deletions runtime/event_loop.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ class EventLoop {
* Remove a queued async task.
*/
static bool cancel_async_task(api::Engine *engine, const RefPtr<api::AsyncTask>& task);

/**
* Reset the event loop state, cancelling all pending tasks and clearing interest count.
*/
static void reset(api::Engine *engine);
};

} // namespace core
Expand Down
Loading