softdevteam / grmtools

Rust grammar tool libraries and binaries
Other
507 stars 31 forks source link

Panicked with 'called `Result::unwrap()` on an `Err` value: Custom("invalid value: integer `4984270261285729579`, expected usize")' after compiling to WebAssembly and running on nodejs #277

Closed JohnGouwar closed 2 years ago

JohnGouwar commented 2 years ago

OS: Ubuntu 20.04 64 bit

Steps to reproduce bug: 0) Follow Quickstart guide from grmtools book. 1) Edit the Cargo.toml in the following way

[lib]
crate-type = ["cdylib"]

[build-dependencies]
cfgrammar = "0.11"
lrlex = "0.11"
lrpar = "0.11"

[dependencies]
cfgrammar = "0.11"
lrlex = "0.11"
lrpar = "0.11"
wasm-bindgen = "0.2"
console_error_panic_hook = "*"

2) Edit parse function in the following way in src/lib.rs

#[wasm_bindgen]
pub fn parse(string: &str) -> u64 { 
    console_error_panic_hook::set_once();
    let lexerdef = calc_l::lexerdef();
    let lexer = lexerdef.lexer(string);
    // Pass the lexer to the parser and lex and parse the input.
    let (res, _) = calc_y::parse(&lexer);
    match res {
        Some(r) => r.unwrap(),
        _ => panic!("Failed to parse expression"),
    }
}

3) Compile using `wasm-pack build --target nodejs 4) Link the package to a node project 5) Run the following script using node

const calc = require("calc");
console.log(calc.parse("2 + 3"))

My initial conjecture is that there is a cast between u64 and usize somewhere, which does not work with the Wasm architecture.

ltratt commented 2 years ago

There isn't explicit support for wasm (if someone wants to add it, I'd accept a PR) but issue https://github.com/softdevteam/grmtools/issues/124 may give you a hack to get around the problem?

JohnGouwar commented 2 years ago

Ah, that's fair. I see that others have an issue from compiling on a 64 bit architecture, I'll try building with 32bit container. Thank you!