How to make wasm project with lazyrand?
install crates:
# create project
cargo init --lib
# install wasm-pack
cargo install wasm-pack
cargo add wasm-bindgen
# install lazyrand
cargo add lazyrand
Add Cargo.toml to crate-type.
...
[lib]
crate-type = ["cdylib", "rlib"]
...
The function is defined as follows. The definition provided is for the case when the randint function is used.
use wasm_bindgen::prelude::*;
/// return random value
#[wasm_bindgen]
pub fn randint(min: isize, max: isize) -> i64 {
lazyrand::randint(min as i64, max as i64)
}
/// return random value
#[wasm_bindgen]
pub fn rand() -> u64 {
lazyrand::rand()
}
For other definitions, please refer to the actual lib.rs file.
wasm-pack build --target web --release
<script type="module">
// WASMを読み込む
import init, { randint, rand } from "./pkg/wasm_test.js";
init().then(() => {
console.log('randint=', randint(1, 6))
});
</script>
Check index.html in your browser. However, loading WASM must be done via http/https.