wasmer-python
wasmer-python copied to clipboard
How to import shuffle or thread_rng from rand crate?
Purpose
I want to give an API of shuffling a vector/list from wasm. The example from wasmer-python readme.md
and py.test tests/test_wasi.py
work both, but the example shown below doesn't work. I'm confused about how to fix it. Is there anyone who can give a hand? TIA
Minimal reproduce
- Error msg:
Traceback (most recent call last):
File "demo.py", line 12, in <module>
instance = Instance(module)
RuntimeError: Error while importing "wasi_snapshot_preview1"."random_get": unknown import. Expected Function(FunctionType { params: [I32, I32], results: [I32] })
- API:
use rand::thread_rng;
#[no_mangle]
pub extern "C" fn add_random(a: i32) -> i32 {
let mut rng = thread_rng();
//let mut list = vec![1,2,3,4,5,6,7,8];
//list.shuffle(&mut rng);
//a + list[0]
}
- Cargo.toml
[lib]
crate-type = ['cdylib']
[dependencies]
rand = {version="0.7", features=["getrandom"]}
#rand = "0.8"
#getrandom = {version = "0.2", features = ["js"]}
- demo.py
from wasmer import engine, Store, Module, Instance
from wasmer_compiler_cranelift import Compiler
# Let's define the store, that holds the engine, that holds the compiler.
store = Store(engine.JIT(Compiler))
# Let's compile the module to be able to execute it!
module = Module(store, open('adder/target/wasm32-wasi/release/adder.wasm', 'rb').read())
# Now the module is compiled, we can instantiate it.
instance = Instance(module)
# Call the exported `sum` function.
result = instance.exports.add_random(5)
print(result)
Environments
- macOS, Intel x64
- Python 3.8.9
- Rustc 1.56.1
- wasmer==1.1.0
- wasmer_compiler_cranelift==1.1.0
Additional information
- After
cargo clean
, it cannot be compiled. seems a bug issue from getrandom and a pr from wasi - Wasmtime runtime and wasmtime-python work both. but wasmer and wasmer-python doesn't work.
wasi 0.10.3
has been yanked, so running cargo update
(or the equivalent) should fix your issue.