Fix Wasm from rust docs (#1719)

* Update Wasm from Rust docs

* oops

* oops x2
This commit is contained in:
Austin Abell
2020-05-18 07:58:21 -04:00
committed by GitHub
parent 463734b002
commit e40fd9d6ab

View File

@@ -43,7 +43,7 @@ dependency in `Cargo.toml`:
```toml ```toml
[dependencies] [dependencies]
wasmtime = "0.12.0" wasmtime = "0.16.0"
``` ```
Next up let's write the code that we need to execute this wasm file. The 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.
(module (module
(import "" "log" (func $log (param i32))) (import "" "log" (func $log (param i32)))
(import "" "double" (func $double (param i32) (result i32))) (import "" "double" (func $double (param i32) (result i32)))
(func (export "run") (result i32) (func (export "run")
i32.const 0 i32.const 0
call $log call $log
i32.const 1 i32.const 1
@@ -138,14 +138,16 @@ looks like this:
```rust,no_run ```rust,no_run
# extern crate wasmtime; # extern crate wasmtime;
# use std::error::Error; use std::error::Error;
# use wasmtime::*; use wasmtime::*;
# fn main() -> Result<(), Box<dyn Error>> {
# let store = Store::default(); fn main() -> Result<(), Box<dyn Error>> {
# let module = Module::new(&store, r#" let store = Store::default();
# (module # if false {
# (import "" "log" (func $log (param i32))) let module = Module::from_file(&store, "hello.wat")?;
# (import "" "double" (func $double (param i32) (result i32))))"#)?; # }
# 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))"#)?;
// First we can create our `log` function, which will simply print out the // First we can create our `log` function, which will simply print out the
// parameter it receives. // parameter it receives.
let log = Func::wrap(&store, |param: i32| { let log = Func::wrap(&store, |param: i32| {
@@ -159,8 +161,15 @@ let double = Func::wrap(&store, |param: i32| param * 2);
// instantiation process. This is the second slice argument, where each // instantiation process. This is the second slice argument, where each
// entry in the slice must line up with the imports in the module. // entry in the slice must line up with the imports in the module.
let instance = Instance::new(&module, &[log.into(), double.into()])?; let instance = Instance::new(&module, &[log.into(), double.into()])?;
# Ok(())
# } let run = instance
.get_func("run")
.expect("`run` was not an exported function");
let run = run.get0::<()>()?;
Ok(run()?)
}
``` ```
Note that there's a number of ways to define a `Func`, be sure to [consult its Note that there's a number of ways to define a `Func`, be sure to [consult its