wasm improvements

This commit is contained in:
Jorijn van der Graaf 2026-08-05 04:08:13 +02:00
commit 6aa0338c0f
9 changed files with 603 additions and 70 deletions

View file

@ -715,6 +715,17 @@ void Window::StartSync() {
#endif
}
void Window::StayAlive() {
// Native has no equivalent of the browser's "return from main but keep
// the instance alive" trick — the process simply ends. The event loop
// IS what keeps a native window alive, so the only correct native
// behaviour is StartSync's loop. Delegating keeps a single source that
// compiles identically on both targets: an app can call StayAlive
// unconditionally and get the cheap path in the browser without a
// #ifdef at the call site.
StartSync();
}
void Window::StartUpdate() {
lastFrameBegin = std::chrono::high_resolution_clock::now();
updating = true;
@ -1571,6 +1582,23 @@ void Window::StartSync() {
std::_Exit(0);
}
void Window::StayAlive() {
// As StartSync, minus the rAF loop.
//
// The _Exit(0) is the part that keeps the instance alive: it skips
// __wasm_call_dtors so every statically allocated object (event
// listeners, HtmlElementPtr handles, the Window itself) survives, and
// runtime.js catches the resulting __wasi_proc_exit via a sentinel.
// The frame loop is a separate concern, and a page that only renders
// DOM has no use for it — a rAF tick that runs forever to do nothing
// costs battery on every open tab.
//
// Use this for event-driven DOM pages; use StartSync when something
// actually animates or draws. As with StartSync, no caller code after
// this point ever runs.
std::_Exit(0);
}
void Window::StartUpdate() {
lastFrameBegin = std::chrono::high_resolution_clock::now();
updating = true;