我写了一个简单的 wasmer-wasi 示例来读取目录条目,但它总是失败。
#[no_mangle]
pub fn start() {
std::fs::read_dir("/").unwrap();
}
use wasmer::{Instance, Module, Store};
use wasmer_wasi::WasiState;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let wasm = std::fs::read("target/wasm32-wasi/debug/wasi_fs_example.wasm")?;
let store = Store::default();
let module = Module::new(&store, wasm)?;
let mut wasi_env = WasiState::new("wasi_fs_example")
.preopen_dir("target")?
.finalize()?;
let import_object = wasi_env.import_object(&module)?;
let instance = Instance::new(&module, &import_object)?;
let start = instance.exports.get_function("start")?;
start.call(&[])?;
Ok(())
}
这会在运行时产生错误:
cargo build -p wasi_fs_example --target wasm32-wasi
cargo run -p runner
thread '<unnamed>' panicked at 'called `Result::unwrap()` on an `Err` value: Custom { kind: Other, error: "failed to find a pre-opened file descriptor through which \"/\" could be opened" }', wasi_fs_example/src/lib.rs:6:10
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
该示例的完整源代码位于此处。为什么它不起作用?