Why does a properly functioning Rust library fail with unreachable code error when compiled to WASM using wasm-bindgen?

Viewed 1020

Consider this library as a dependency:

[dependencies]
rustengine = "1.0.60"

Compile this tiny program against it:

use rustenginelib::uci::*;

fn main() {
    let mut uci = create_default_uci();

    uci.process_uci_command("perft 4".to_string());
}

This will instantiate the library's main object, call an expensive calculation, then print the stats of it. It outputs:

    Finished dev [unoptimized + debuginfo] target(s) in 0.02s
     Running `target\debug\rustengine.exe`
node(s) 320006 , time 0.47 sec(s) , nps 677.4099 kNode(s)/sec

Now use the same dependency in the official Rust WASM example

https://developer.mozilla.org/en-US/docs/WebAssembly/Rust_to_wasm

and modify the greet function to execute the same two commands against the same library dependency:

use rustenginelib::uci::*;

#[wasm_bindgen]
pub fn greet(name: &str) {
    let mut uci = create_default_uci();

    uci.process_uci_command("perft 4".to_string());

    alert(&format!("Hello, engine {}!", name));
}

This compiles without warning or error, the npm package is built, it is served up on localhost:8080, and the WASM executable runs and fails with error

Uncaught (in promise) RuntimeError: unreachable
    at wasm-function[117]:0x1459f
    at wasm-function[188]:0x1636d
    at wasm-function[232]:0x16792
    at wasm-function[9]:0x6692
    at wasm-function[52]:0xf068
    at Module.greet (webpack:///./node_modules/@easychessanimations/chessengine-wasm/chessengine_wasm_bg.js?:87:68)
    at eval (webpack:///./index.js?:3:6)

while the example works once you comment out the expensive calculation ( but not the initialization of the library object ):

    //uci.process_uci_command("perft 4".to_string());
0 Answers
Related