5

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());
  • Not all parts of the standard library are implemented for the standard WASM target. If the `rustengine` library is using those parts, then I wouldn't be surprised if you got behavior similar to what you're experiencing, so that might be the problem? – Frxstrem May 16 '20 at 11:04
  • I was complaining in some of my Discord posts about Go WASM being bloated and slow, but come on, compiling Go to WASM is using the standard framework of setting GOOS and GOARCH env variables and that's all, it compiles then, also at least it works, and it prints the stdout of the program to the console. while in Rust the stdout is not printed, you have to invoke complicated dependecy to be able to print to the console, but I could not find any source as to how to redirect stdout to the console – easychessanimations May 16 '20 at 11:09

0 Answers0