Recursion limit reached while expanding wasm_bindgen

Tring out eframe and egui, so i copied an example file, wrote a little demo app and tried generating the wasm to run it in a browser with

RUSTFLAGS='--cfg=web_sys_unstable_apis --cfg getrandom_backend="wasm_js" -Zmacro-backtrace' cargo +nightly build -p "${CRATE_NAME}" --release --lib --target wasm32-unknown-unknown

+nightly and -Zmacro-backtrace to debug the macro expansion, but its not that useful.

In the lib file i have:

mod helper;
mod app;
mod app_logic;

#[cfg(target_arch = "wasm32")]
mod web;

#[cfg(target_arch = "wasm32")]
pub use web::*;

and in the web.rs file:

#![allow(clippy::mem_forget)] // False positives from #[wasm_bindgen] macro

use eframe::wasm_bindgen::{self, prelude::*};

use crate::app::RandomAppRunner;

#[derive(Clone)]
#[wasm_bindgen()]
pub struct WebHandle {
    runner: eframe::WebRunner,
}

#[wasm_bindgen()]
impl WebHandle {
    /// Installs a panic hook, then returns.
    #[allow(clippy::new_without_default, clippy::allow_attributes)]
    #[wasm_bindgen(constructor)]
    pub fn new() -> Self {
        // Redirect [`log`] message to `console.log` and friends:
        let log_level = if cfg!(debug_assertions) {
            log::LevelFilter::Trace
        } else {
            log::LevelFilter::Debug
        };
        eframe::WebLogger::init(log_level).ok();

        Self {
            runner: eframe::WebRunner::new(),
        }
    }

    /// Call this once from JavaScript to start your app.
    #[wasm_bindgen]
    pub async fn start(
        &self,
        canvas: web_sys::HtmlCanvasElement,
    ) -> Result<(), wasm_bindgen::JsValue> {
        self.runner
            .start(
                canvas,
                eframe::WebOptions::default(),
                Box::new(|cc| Ok(Box::new(RandomAppRunner::new(cc)))),
            )
            .await
    }

    #[wasm_bindgen]
    pub fn destroy(&self) {
        self.runner.destroy();
    }

    /// Example on how to call into your app from JavaScript.
    #[wasm_bindgen]
    pub fn example(&self) {
        if let Some(_app) = self.runner.app_mut::<WrapApp>() {
            // _app.example();
        }
    }

    /// The JavaScript can check whether or not your app has crashed:
    #[wasm_bindgen]
    pub fn has_panicked(&self) -> bool {
        self.runner.has_panicked()
    }

    #[wasm_bindgen]
    pub fn panic_message(&self) -> Option<String> {
        self.runner.panic_summary().map(|s| s.message())
    }

    #[wasm_bindgen]
    pub fn panic_callstack(&self) -> Option<String> {
        self.runner.panic_summary().map(|s| s.callstack())
    }
}

taken directly from the egui repo.
RandomAppRunner implements eframe::App in another file.

This is the error:

error: recursion limit reached while expanding `#[derive]`
  --> random_app/src/web.rs:9:1
   |
 9 | #[wasm_bindgen]
   | ^^^^^^^^^^^^^^^ in this attribute macro expansion
   |
  ::: ~/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/wasm-bindgen-macro-0.2.104/src/lib.rs:10:1
   |
10 | pub fn wasm_bindgen(attr: TokenStream, input: TokenStream) -> TokenStream {
   | ------------------------------------------------------------------------- in this expansion of `#[wasm_bindgen]`
   |
   = help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`reaction_systems_gui`)

error: could not compile `random_app` (lib) due to 1 previous error

I tried searching and this might be a similar issue but its marked as resolved and I'm using version 104, so I'm confused.

Runs native just fine, so its not the gui that is the problem.

Managed to get it working. But now I'm even more confused.
Added in Cargo.toml

[target.'cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))'.dependencies]
wasm-bindgen = "0.2"
wasm-bindgen-futures = "*"
getrandom = { version = "0.3", features = ["wasm_js"]}

taken from cardano

Dont know why this makes stuff work but im grateful the benevolent gods today smiled and let me have something work.

New problem: the canvas immediately tries to cover the surface of the sun by exponential search.

WebGL warning: clear: Requested size 9600x4800 was too large, but resize to 4800x2400 succeeded.
WebGL warning: drawElementsInstanced: Drawing to a destination rect smaller than the viewport rect. (This warning will only be given once)
WebGL warning: clear: Requested size 19200x9600 was too large, but resize to 4800x2400 succeeded.
WebGL warning: clear: Requested size 38400x19200 was too large, but resize to 4800x2400 succeeded.
WebGL warning: clear: Requested size 76800x38400 was too large, but resize to 4800x2400 succeeded.
WebGL warning: clear: Requested size 153600x76800 was too large, but resize to 4800x2400 succeeded.
WebGL warning: clear: Requested size 307200x153600 was too large, but resize to 4800x2400 succeeded.
WebGL warning: clear: Requested size 614400x307200 was too large, but resize to 4800x2400 succeeded.
WebGL warning: clear: Requested size 1228800x614400 was too large, but resize to 4800x2400 succeeded.
WebGL warning: clear: Requested size 2457600x1228800 was too large, but resize to 4800x2400 succeeded.
WebGL warning: clear: Requested size 4915200x2457600 was too large, but resize to 4800x2400 succeeded.
WebGL warning: clear: Requested size 9830400x4915200 was too large, but resize to 4800x2400 succeeded.
WebGL warning: clear: Requested size 19660800x9830400 was too large, but resize to 4800x2400 succeeded.
WebGL warning: clear: Requested size 35791396x19660800 was too large, but resize to 4369x2400 succeeded.
WebGL warning: clear: Requested size 35791396x35791396 was too large, but resize to 4369x4369 succeeded. 

At least this is a fun problem to solve :slight_smile:

1 Like