Skip to content

Commit e40fd9d

Browse files
authored
Fix Wasm from rust docs (bytecodealliance#1719)
* Update Wasm from Rust docs * oops * oops x2
1 parent 463734b commit e40fd9d

File tree

1 file changed

+33
-24
lines changed

1 file changed

+33
-24
lines changed

docs/lang-rust.md

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ dependency in `Cargo.toml`:
4343

4444
```toml
4545
[dependencies]
46-
wasmtime = "0.12.0"
46+
wasmtime = "0.16.0"
4747
```
4848

4949
Next up let's write the code that we need to execute this wasm file. The
@@ -120,7 +120,7 @@ some simple arithmetic from the environment.
120120
(module
121121
(import "" "log" (func $log (param i32)))
122122
(import "" "double" (func $double (param i32) (result i32)))
123-
(func (export "run") (result i32)
123+
(func (export "run")
124124
i32.const 0
125125
call $log
126126
i32.const 1
@@ -138,29 +138,38 @@ looks like this:
138138

139139
```rust,no_run
140140
# extern crate wasmtime;
141-
# use std::error::Error;
142-
# use wasmtime::*;
143-
# fn main() -> Result<(), Box<dyn Error>> {
144-
# let store = Store::default();
145-
# let module = Module::new(&store, r#"
146-
# (module
147-
# (import "" "log" (func $log (param i32)))
148-
# (import "" "double" (func $double (param i32) (result i32))))"#)?;
149-
// First we can create our `log` function, which will simply print out the
150-
// parameter it receives.
151-
let log = Func::wrap(&store, |param: i32| {
152-
println!("log: {}", param);
153-
});
154-
155-
// Next we can create our double function which doubles the input it receives.
156-
let double = Func::wrap(&store, |param: i32| param * 2);
157-
158-
// When instantiating the module we now need to provide the imports to the
159-
// instantiation process. This is the second slice argument, where each
160-
// entry in the slice must line up with the imports in the module.
161-
let instance = Instance::new(&module, &[log.into(), double.into()])?;
162-
# Ok(())
141+
use std::error::Error;
142+
use wasmtime::*;
143+
144+
fn main() -> Result<(), Box<dyn Error>> {
145+
let store = Store::default();
146+
# if false {
147+
let module = Module::from_file(&store, "hello.wat")?;
163148
# }
149+
# let module = Module::new(&store, r#"(module (import "" "log" (func $log (param i32))) (import "" "double" (func $double (param i32) (result i32))) (func (export "run") i32.const 0 call $log i32.const 1 call $log i32.const 2 call $double call $log))"#)?;
150+
151+
// First we can create our `log` function, which will simply print out the
152+
// parameter it receives.
153+
let log = Func::wrap(&store, |param: i32| {
154+
println!("log: {}", param);
155+
});
156+
157+
// Next we can create our double function which doubles the input it receives.
158+
let double = Func::wrap(&store, |param: i32| param * 2);
159+
160+
// When instantiating the module we now need to provide the imports to the
161+
// instantiation process. This is the second slice argument, where each
162+
// entry in the slice must line up with the imports in the module.
163+
let instance = Instance::new(&module, &[log.into(), double.into()])?;
164+
165+
let run = instance
166+
.get_func("run")
167+
.expect("`run` was not an exported function");
168+
169+
let run = run.get0::<()>()?;
170+
171+
Ok(run()?)
172+
}
164173
```
165174

166175
Note that there's a number of ways to define a `Func`, be sure to [consult its

0 commit comments

Comments
 (0)