RuntimeError: unreachable in rustwasm

Viewed 3995

I am getting the error "RuntimeError: unreachable in rustwasm" for the code

error message:

Uncaught (in promise) RuntimeError: unreachable
    at __rust_start_panic (http://127.0.0.1:8080/wasm_bg.wasm:wasm-function[346]:0x274ec)
    at rust_panic (http://127.0.0.1:8080/wasm_bg.wasm:wasm-function[254]:0x26d0b)
    at std::panicking::rust_panic_with_hook::hb07b303a83b6d242 (http://127.0.0.1:8080/wasm_bg.wasm:wasm-function[168]:0x24a47)
    at rust_begin_unwind (http://127.0.0.1:8080/wasm_bg.wasm:wasm-function[228]:0x267ed)
    at core::panicking::panic_fmt::h62f7e605cdf65d83 
    at core::panicking::panic_bounds_check::ha440f41d0e4eca5f 
    at virtualdom::parseNode::h9ba47b7b20126d66 
    at run ()
    at init ()

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=ac403a9043e49b7a2fc3814a692b8d49

I have following questions ?

  1. is it coming from parseNode function?
  2. is it because of dm.children[i] overflowed? or some functions removed at runtime in the background in wasm?
  3. I tried to hook panic::set_hook(Box::new(console_error_panic_hook::hook)); but did not help much! How should I debug these kind of errors?
1 Answers

The issue is that dm.children.len() is 1 so immediately after the first iteration of the while loop, then i becomes 1 which triggers the panic as 1 is out of bounds.

The easiest way to fix this, is to replace the while loop with a for loop instead, and match the Node::Element variant inside the for loop.

for child in dm.children {
    if let Node::Element(x) = child {
        let nod = Node1 {
            tag: x.name,
            props: x.classes,
            children: x.children,
        };

        println!("{:?}", nod.tag);
        createElement(&nod);
    }
}

Note that you're not handling the result which createElement() returns.

Since this specific code you're having issues with doesn't really have any specific WASM/web related things. Then you could just compile that code as a regular Rust binary for testing, which would give you more helpful errors and simplify testing.

Related