stdweb icon indicating copy to clipboard operation
stdweb copied to clipboard

Panic on Firefox with Trunk

Open sotrh opened this issue 2 years ago • 0 comments

I was experimenting with using GilRs on WASM with Trunk using the following code:

use std::{cell::RefCell, rc::Rc};
use gilrs::Event;
use log::info;
use wasm_bindgen::{prelude::*, JsCast};

fn window() -> web_sys::Window {
    web_sys::window().expect("Platform does not have a global window.")
}

fn request_animation_frame(f: &Closure<dyn FnMut()>) {
    window()
        .request_animation_frame(f.as_ref().unchecked_ref())
        .expect("`requestAnimationFrame` should succeed");
}

fn game_loop(mut callback: impl FnMut() + 'static) {
    std::panic::set_hook(Box::new(console_error_panic_hook::hook));
    console_log::init_with_level(log::Level::Info).expect("Couldn't setup console logging");

    let f = Rc::new(RefCell::new(None));
    let g = f.clone();

    *g.borrow_mut() = Some(Closure::wrap(Box::new(move || {
        callback();
        request_animation_frame(f.borrow().as_ref().unwrap());
    }) as Box<dyn FnMut()>));

    info!("Starting game loop");
    request_animation_frame(g.borrow().as_ref().unwrap());
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut context = gilrs::Gilrs::new()?;

    game_loop(move || {
        info!("game_loop");
        while let Some(Event { id, event, time}) = context.next_event() {
            info!("Gamepad event");
            // info!("{:?}", id);
        }
    });

    Ok(())
}

The code runs fine as long as the connected gamepad doesn't have any inputs. As soon as the gamepad gets an input, the code panics with the following:

Panic location: /home/ben/.cargo/registry/src/github.com-1ecc6299db9ec823/stdweb-0.4.20/src/webcore/serialization.rs:447 inline279.js:1:108
    __cargo_web_snippet_72fc447820458c720c68d0d8e078ede631edd723 http://localhost:8080/snippets/stdweb-bb142200b065bd55/inline279.js:1
    __wbg_cargowebsnippet72fc447820458c720c68d0d8e078ede631edd723_ece3da0a4474dbeb http://localhost:8080/index-44e15f7f4b7059be.js:429
    hfc584dac876d521b http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:567993
    h11bcb30f712fdf5b http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:675817
    hd829486609ce1dfc http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:690973
    h98ec191c18086a09 http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:302453
    h606d7c7f7a423b98 http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:454051
    h9b985a293aac4ce1 http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:561075
    ha03abef02a8b70fd http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:746856
    rust_begin_unwind http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:715965
    h6314b5c91abe7349 http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:730725
    hec1fc057bd0baf0b http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:694808
    <stdweb::webcore::serialization::SerializedValue as core::convert::From<stdweb::webcore::serialization::SerializedUntaggedReference>>::from::hadfbae9d235f25d7 http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:357571
    <T as core::convert::Into<U>>::into::h5a573d61e2a7eb6e http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:568628
    <stdweb::webcore::value::Reference as stdweb::webcore::serialization::JsSerialize>::_into_js::hc4c09f414aa3eb25 http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:679509
    <&T as stdweb::webcore::serialization::JsSerialize>::_into_js::h85c64cc956c40e8d http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:707989
    <stdweb::webcore::newtype::Newtype<(stdweb::webcore::serialization::NonFunctionTag,()),T> as stdweb::webcore::serialization::JsSerializeOwned>::into_js_owned::h5d20b7dcb6d87257 http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:657841
    heaa01a07f9cd69d9 http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:304545
    h67e64ad48dc99139 http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:54876
    h91b9f80778aea007 http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:618855
    core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &mut F>::call_once::hd4a412cd0b519899 http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:555100
    ha00841e5932f2bc4 http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:344169
    <core::iter::adapters::map::Map<I,F> as core::iter::traits::iterator::Iterator>::next::h352aa1033bc5ab60 http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:576477
    <alloc::vec::Vec<T> as alloc::vec::spec_from_iter_nested::SpecFromIterNested<T,I>>::from_iter::h7a1b3705b436b9b7 http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:218444
    <alloc::vec::Vec<T> as alloc::vec::spec_from_iter::SpecFromIter<T,I>>::from_iter::h57e586fadfc94a89 http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:516655
    <alloc::vec::Vec<T> as core::iter::traits::collect::FromIterator<T>>::from_iter::h547de20f16ac3be1 http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:468799
    h099f28b8da9dd9e8 http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:516997
    hc5395bde68faa4b7 http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:49546
    h600223be596d5476 http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:717220
    h72b1789ec4d25374 http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:42919
    h0f2fad8861d4473a http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:187492
    h9806841f81972af8 http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:177300
    h8d4b07a2975bb5a0 http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:477376
    <dyn core::ops::function::FnMut<()>+Output = R as wasm_bindgen::closure::WasmClosure>::describe::invoke::hd01aec6e6501a09e http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:513139
    __wbg_adapter_20 http://localhost:8080/index-44e15f7f4b7059be.js:222
    real http://localhost:8080/index-44e15f7f4b7059be.js:207
    (Async: FrameRequestCallback)
    __wbg_requestAnimationFrame_8e3c7028c69ebaef http://localhost:8080/index-44e15f7f4b7059be.js:354
    handleError http://localhost:8080/index-44e15f7f4b7059be.js:257
    __wbg_requestAnimationFrame_8e3c7028c69ebaef http://localhost:8080/index-44e15f7f4b7059be.js:353
    hdcbe2455b4a75a5c http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:388341
    h152f11a925cb67a2 http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:467811
    h8d4b07a2975bb5a0 http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:477517
    <dyn core::ops::function::FnMut<()>+Output = R as wasm_bindgen::closure::WasmClosure>::describe::invoke::hd01aec6e6501a09e http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:513139
    __wbg_adapter_20 http://localhost:8080/index-44e15f7f4b7059be.js:222
    real http://localhost:8080/index-44e15f7f4b7059be.js:207
    (Async: FrameRequestCallback)
    __wbg_requestAnimationFrame_8e3c7028c69ebaef http://localhost:8080/index-44e15f7f4b7059be.js:354
    handleError http://localhost:8080/index-44e15f7f4b7059be.js:257
    __wbg_requestAnimationFrame_8e3c7028c69ebaef http://localhost:8080/index-44e15f7f4b7059be.js:353
    hdcbe2455b4a75a5c http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:388341
    h152f11a925cb67a2 http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:467811
    h8d4b07a2975bb5a0 http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:477517
    <dyn core::ops::function::FnMut<()>+Output = R as wasm_bindgen::closure::WasmClosure>::describe::invoke::hd01aec6e6501a09e http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:513139
    __wbg_adapter_20 http://localhost:8080/index-44e15f7f4b7059be.js:222
    real http://localhost:8080/index-44e15f7f4b7059be.js:207
    (Async: FrameRequestCallback)
    __wbg_requestAnimationFrame_8e3c7028c69ebaef http://localhost:8080/index-44e15f7f4b7059be.js:354
    handleError http://localhost:8080/index-44e15f7f4b7059be.js:257
    __wbg_requestAnimationFrame_8e3c7028c69ebaef http://localhost:8080/index-44e15f7f4b7059be.js:353
    hdcbe2455b4a75a5c http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:388341
    h152f11a925cb67a2 http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:467811
    h8d4b07a2975bb5a0 http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:477517
    <dyn core::ops::function::FnMut<()>+Output = R as wasm_bindgen::closure::WasmClosure>::describe::invoke::hd01aec6e6501a09e http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:513139
    __wbg_adapter_20 http://localhost:8080/index-44e15f7f4b7059be.js:222
    real http://localhost:8080/index-44e15f7f4b7059be.js:207
    (Async: FrameRequestCallback)
    __wbg_requestAnimationFrame_8e3c7028c69ebaef http://localhost:8080/index-44e15f7f4b7059be.js:354
    handleError http://localhost:8080/index-44e15f7f4b7059be.js:257
    __wbg_requestAnimationFrame_8e3c7028c69ebaef http://localhost:8080/index-44e15f7f4b7059be.js:353
    hdcbe2455b4a75a5c http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:388341
    h152f11a925cb67a2 http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:467811
    h8d4b07a2975bb5a0 http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:477517
    <dyn core::ops::function::FnMut<()>+Output = R as wasm_bindgen::closure::WasmClosure>::describe::invoke::hd01aec6e6501a09e http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:513139
    __wbg_adapter_20 http://localhost:8080/index-44e15f7f4b7059be.js:222
    real http://localhost:8080/index-44e15f7f4b7059be.js:207
    (Async: FrameRequestCallback)
    __wbg_requestAnimationFrame_8e3c7028c69ebaef http://localhost:8080/index-44e15f7f4b7059be.js:354
    handleError http://localhost:8080/index-44e15f7f4b7059be.js:257
    __wbg_requestAnimationFrame_8e3c7028c69ebaef http://localhost:8080/index-44e15f7f4b7059be.js:353
    hdcbe2455b4a75a5c http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:388341
    h152f11a925cb67a2 http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:467811
    h8d4b07a2975bb5a0 http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:477517
    <dyn core::ops::function::FnMut<()>+Output = R as wasm_bindgen::closure::WasmClosure>::describe::invoke::hd01aec6e6501a09e http://localhost:8080/index-44e15f7f4b7059be_bg.wasm:513139
    __wbg_adapter_20 http://localhost:8080/index-44e15f7f4b7059be.js:222
    real http://localhost:8080/index-44e15f7f4b7059be.js:207
    (Async: FrameRequestCallback)
    __wbg_requestAnimationFrame_8e3c7028c69ebaef http://localhost:8080/index-44e15f7f4b7059be.js:354
    handleError http://localhost:8080/index-44e15f7f4b7059be.js:257
    __wbg_requestAnimationFrame_8e3c7028c69ebaef http://localhost:8080/index-44e15f7f4b7059be.js:353

The error seems to due with webcore serializing something as per the first line in the error:

Panic location: /home/ben/.cargo/registry/src/github.com-1ecc6299db9ec823/stdweb-0.4.20/src/webcore/serialization.rs:447 inline279.js:1:108

You can view the full code here: https://github.com/sotrh/gamepads

I've already created an issue on the GilRs gitlab: https://gitlab.com/gilrs-project/gilrs/-/issues/118

sotrh avatar Apr 09 '22 20:04 sotrh