diff options
author | Fabio Alessandrelli <fabio.alessandrelli@gmail.com> | 2022-11-03 14:00:52 +0100 |
---|---|---|
committer | Fabio Alessandrelli <fabio.alessandrelli@gmail.com> | 2022-11-05 14:06:09 +0100 |
commit | cece83fdf26bcb0897b2ed785cd079a778878314 (patch) | |
tree | 5a4cbdc5fb3385e79e342c82040587619285aac6 /platform/web | |
parent | 6882890a34528e056f13020798f4a473046990bb (diff) |
[Web] Improve Godot shutdown and cleanup.
Diffstat (limited to 'platform/web')
-rw-r--r-- | platform/web/js/engine/config.js | 2 | ||||
-rw-r--r-- | platform/web/web_main.cpp | 36 |
2 files changed, 19 insertions, 19 deletions
diff --git a/platform/web/js/engine/config.js b/platform/web/js/engine/config.js index 41be7b2512..4560f12b49 100644 --- a/platform/web/js/engine/config.js +++ b/platform/web/js/engine/config.js @@ -275,7 +275,7 @@ const InternalConfig = function (initConfig) { // eslint-disable-line no-unused- 'print': this.onPrint, 'printErr': this.onPrintError, 'thisProgram': this.executable, - 'noExitRuntime': true, + 'noExitRuntime': false, 'dynamicLibraries': [`${loadPath}.side.wasm`], 'instantiateWasm': function (imports, onSuccess) { function done(result) { diff --git a/platform/web/web_main.cpp b/platform/web/web_main.cpp index a76b98f4e9..fce782b546 100644 --- a/platform/web/web_main.cpp +++ b/platform/web/web_main.cpp @@ -41,30 +41,25 @@ static OS_Web *os = nullptr; static uint64_t target_ticks = 0; +static bool main_started = false; +static bool shutdown_complete = false; void exit_callback() { - emscripten_cancel_main_loop(); // After this, we can exit! - Main::cleanup(); + if (!shutdown_complete) { + return; // Still waiting. + } + if (main_started) { + Main::cleanup(); + main_started = false; + } int exit_code = OS_Web::get_singleton()->get_exit_code(); memdelete(os); os = nullptr; - emscripten_force_exit(exit_code); // No matter that we call cancel_main_loop, regular "exit" will not work, forcing. + emscripten_force_exit(exit_code); // Exit runtime. } void cleanup_after_sync() { - emscripten_set_main_loop(exit_callback, -1, false); -} - -void early_cleanup() { - emscripten_cancel_main_loop(); // After this, we can exit! - int exit_code = OS_Web::get_singleton()->get_exit_code(); - memdelete(os); - os = nullptr; - emscripten_force_exit(exit_code); // No matter that we call cancel_main_loop, regular "exit" will not work, forcing. -} - -void early_cleanup_sync() { - emscripten_set_main_loop(early_cleanup, -1, false); + shutdown_complete = true; } void main_loop_callback() { @@ -87,7 +82,8 @@ void main_loop_callback() { target_ticks += (uint64_t)(1000000 / max_fps); } if (os->main_loop_iterate()) { - emscripten_cancel_main_loop(); // Cancel current loop and wait for cleanup_after_sync. + emscripten_cancel_main_loop(); // Cancel current loop and set the cleanup one. + emscripten_set_main_loop(exit_callback, -1, false); godot_js_os_finish_async(cleanup_after_sync); } } @@ -109,10 +105,14 @@ extern EMSCRIPTEN_KEEPALIVE int godot_web_main(int argc, char *argv[]) { } os->set_exit_code(exit_code); // Will only exit after sync. - godot_js_os_finish_async(early_cleanup_sync); + emscripten_set_main_loop(exit_callback, -1, false); + godot_js_os_finish_async(cleanup_after_sync); return exit_code; } + os->set_exit_code(0); + main_started = true; + // Ease up compatibility. ResourceLoader::set_abort_on_missing_resources(false); |