diff --git a/.gitignore b/.gitignore index a19894a8a..ada91db4c 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ debug/ target/ target-tarpaulin/ target-custom/ +venv/ # These are backup files generated by rustfmt **/*.rs.bk diff --git a/Cargo.toml b/Cargo.toml index da1b4bea9..547eded86 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,7 @@ # the part of this workspace [workspace] -exclude = ["sdk"] +exclude = ["sdk", "ws2riscvtest"] members = [ "circuits", "cli", @@ -32,6 +32,7 @@ resolver = "2" [profile.dev] lto = "thin" # We are running our tests with optimizations turned on to make them faster. + # Please turn optimizations off, when you want accurate stack traces for debugging. opt-level = 2 diff --git a/ws2riscvtest/Cargo.toml b/ws2riscvtest/Cargo.toml new file mode 100644 index 000000000..a992abd01 --- /dev/null +++ b/ws2riscvtest/Cargo.toml @@ -0,0 +1,10 @@ +[package] +edition = "2021" +name = "ws2riscvtest" +version = "0.1.0" + +[lib] +crate-type = ["cdylib"] + +[dependencies] +wasm-bindgen = "0.2" diff --git a/ws2riscvtest/README.md b/ws2riscvtest/README.md new file mode 100644 index 000000000..664faef72 --- /dev/null +++ b/ws2riscvtest/README.md @@ -0,0 +1,27 @@ +# What +This directory aims at testing if we can convert a WASM binary to RISC-V target and make it run well on `mozak-vm`. We simulate a naive implementation of Fibonacci for this. + +## Building and testing a WASM module +We need `wasm-pack` via +``` +cargo install wasm-pack +``` + +Inside the directory `wasmtest`, we build a WASM module via: +``` +wasm-pack build --target web +``` +This builds binary for the target `wasm-unknown-unknown` + +This would build artifacts accessible in the directory `./pkg`. Most importantly, these files would be generated: +1. `pkg/ws2riscvtest_bg.wasm`, the WASM binary file. +2. `pkg/ws2riscvtest.js`, the JS file required to run WASM code in browser + +Run a dummy testing server (on port 8000 by default) via: +``` +python3 -m http.server +``` +and load up `http://localhost:8000/` in the browser. The correct run should output: +``` +WASM TESTER! Function result: 34 +``` diff --git a/ws2riscvtest/index.html b/ws2riscvtest/index.html new file mode 100644 index 000000000..edd0402f2 --- /dev/null +++ b/ws2riscvtest/index.html @@ -0,0 +1,9 @@ + + + + + WASM Tester + + + + diff --git a/ws2riscvtest/index.js b/ws2riscvtest/index.js new file mode 100644 index 000000000..351a5b89c --- /dev/null +++ b/ws2riscvtest/index.js @@ -0,0 +1,15 @@ +// Import our outputted wasm ES6 module +// Which, export default's, an initialization function +import init from "./pkg/ws2riscvtest.js"; + +const runWasm = async () => { + // Instantiate our wasm module + const module = await init("./pkg/ws2riscvtest_bg.wasm"); + + // Call the function exported from wasm, save the result + const funcResult = module.fibonacci(BigInt("8")); + + // Set the result onto the body + document.body.textContent = `WASM TESTER! Function result: ${funcResult}`; +}; +runWasm(); diff --git a/ws2riscvtest/src/lib.rs b/ws2riscvtest/src/lib.rs new file mode 100644 index 000000000..563eaa58e --- /dev/null +++ b/ws2riscvtest/src/lib.rs @@ -0,0 +1,14 @@ +// The wasm-pack uses wasm-bindgen to build and generate JavaScript binding file. +// Import the wasm-bindgen crate. +use wasm_bindgen::prelude::*; + +// Our fibonacci function +// wasm-pack requires "exported" functions +// to include #[wasm_bindgen] +#[wasm_bindgen] +pub fn fibonacci(a: u64) -> u64 { + if a == 0 || a == 1 { + return 1; + } + return fibonacci(a-1) + fibonacci(a - 2); +}