Skip to content

Generated wasmBindings initialization doesn't expect exceptions #1019

@egor-ryashin

Description

@egor-ryashin

During the build process the following code is generated in html file:

<script type="module">
import init, * as bindings from '/bevy-demo-58cf2fdc11fbdbe3.js';
const wasm = await init({ module_or_path: '/bevy-demo-58cf2fdc11fbdbe3_bg.wasm' });


window.wasmBindings = bindings;


dispatchEvent(new CustomEvent("TrunkApplicationStarted", {detail: {wasm}}));

</script>

But the init method can throw exceptions which breaks wasmBindings initialization. It's common to think that an exception itself means a broken state that shouldn't be used further but actually some libraries think throwing exception does mean a successful initialization:

(index):136 Error stack: Error: Using exceptions for control flow, don't mind me. This isn't actually an error!
    at imports.wbg.__wbindgen_throw (http://127.0.0.1:7701/bevy-demo-aa55efd8f2bb1a32.js:1708:15)
    at bevy_demo.wasm.wasm_bindgen::throw_str::hf2c30ede6cf4503f (http://127.0.0.1:7701/bevy-demo-aa55efd8f2bb1a32_bg.wasm:wasm-function[86081]:0x197660f)
    at bevy_demo.wasm.winit::platform_impl::web::web_sys::throw::hb0947f5ee7e3478c (http://127.0.0.1:7701/bevy-demo-aa55efd8f2bb1a32_bg.wasm:wasm-function[84214]:0x196dae3)
    at bevy_demo.wasm.winit::platform_impl::web::event_loop::EventLoop<T>::run::ha55cc005230eefa7 (http://127.0.0.1:7701/bevy-demo-aa55efd8f2bb1a32_bg.wasm:wasm-function[35226]:0x1765908)
    at bevy_demo.wasm.bevy_winit::state::winit_runner::h0a8699fee68b727a (http://127.0.0.1:7701/bevy-demo-aa55efd8f2bb1a32_bg.wasm:wasm-function[2126]:0x80769e)
    at bevy_demo.wasm.core::ops::function::FnOnce::call_once{{vtable.shim}}::h0bf45ed5d82b2040 (http://127.0.0.1:7701/bevy-demo-aa55efd8f2bb1a32_bg.wasm:wasm-function[48332]:0x1891bd7)
    at bevy_demo.wasm.bevy_app::app::App::run::h54dc5d2aa39ddf98 (http://127.0.0.1:7701/bevy-demo-aa55efd8f2bb1a32_bg.wasm:wasm-function[10409]:0xffe335)
    at bevy_demo.wasm.bevy_demo::main::hfc3b979dc39b6105 (http://127.0.0.1:7701/bevy-demo-aa55efd8f2bb1a32_bg.wasm:wasm-function[1706]:0x709ea4)
    at bevy_demo.wasm.main (http://127.0.0.1:7701/bevy-demo-aa55efd8f2bb1a32_bg.wasm:wasm-function[45805]:0x186d0da)

Anyway, I think without catching that exception properly that library keeps wasmBindings uninitialized.

Updated. This code generates the exception: https://github.com/rust-windowing/winit/blob/2486f0f1a1d00ac9e5936a5222b2cfe90ceeca02/src/platform_impl/web/event_loop/mod.rs#L41

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions