Support parsing the text format in wasmtime crate (#813)

* Support parsing the text format in `wasmtime` crate

This commit adds support to the `wasmtime::Module` type to parse the
text format. This is often quite convenient to support in testing or
tinkering with the runtime. Additionally the `wat` parser is pretty
lightweight and easy to add to builds, so it's relatively easy for us to
support as well!

The exact manner that this is now supported comes with a few updates to
the existing API:

* A new optional feature of the `wasmtime` crate, `wat`, has been added.
  This is enabled by default.
* The `Module::new` API now takes `impl AsRef<[u8]>` instead of just
  `&[u8]`, and when the `wat` feature is enabled it will attempt to
  interpret it either as a wasm binary or as the text format. Note that
  this check is quite cheap since you just check the first byte.
* A `Module::from_file` API was added as a convenience to parse a file
  from disk, allowing error messages for `*.wat` files on disk to be a
  bit nicer.
* APIs like `Module::new_unchecked` and `Module::validate` remain
  unchanged, they require the binary format to be called.

The intention here is to make this as convenient as possible for new
developers of the `wasmtime` crate. By changing the default behavior
though this has ramifications such as, for example, supporting the text
format implicitly through the C API now.

* Handle review comments

* Update more tests to avoid usage of `wat` crate

* Go back to unchecked for now in wasm_module_new

Looks like C# tests rely on this?
This commit is contained in:
Alex Crichton
2020-01-24 14:20:51 -06:00
committed by GitHub
parent 47d6db0be8
commit 16804673a2
15 changed files with 185 additions and 168 deletions

View File

@@ -36,9 +36,8 @@ const WAT: &str = r#"
fn main() -> anyhow::Result<()> {
// Load our WebAssembly (parsed WAT in our case), and then load it into a
// `Module` which is attached to a `Store` cache.
let wasm = wat::parse_str(WAT)?;
let store = Store::default();
let module = Module::new(&store, &wasm)?;
let module = Module::new(&store, WAT)?;
// Find index of the `gcd` export.
let gcd_index = module

View File

@@ -21,21 +21,15 @@ fn main() -> Result<()> {
println!("Initializing...");
let store = Store::default();
// Next upload the `*.wasm` binary file, which in this case we're going to
// be parsing an inline text format into a binary.
println!("Loading binary...");
let binary = wat::parse_str(
r#"
(module
(func $hello (import "" "hello"))
(func (export "run") (call $hello))
)
"#,
)?;
// Compiler the `*.wasm` binary into an in-memory instance of a `Module`.
// Compile the wasm binary into an in-memory instance of a `Module`.
println!("Compiling module...");
let module = Module::new(&store, &binary).context("> Error compiling module!")?;
let wat = r#"
(module
(func $hello (import "" "hello"))
(func (export "run") (call $hello))
)
"#;
let module = Module::new(&store, wat).context("> Error compiling module!")?;
// Here we handle the imports of the module, which in this case is our
// `HelloCallback` type and its associated implementation of `Callback.

View File

@@ -66,27 +66,25 @@ fn main() -> Result<(), Error> {
// Load binary.
println!("Loading binary...");
let binary = wat::parse_str(
r#"
(module
(memory (export "memory") 2 3)
let wat = r#"
(module
(memory (export "memory") 2 3)
(func (export "size") (result i32) (memory.size))
(func (export "load") (param i32) (result i32)
(i32.load8_s (local.get 0))
)
(func (export "store") (param i32 i32)
(i32.store8 (local.get 0) (local.get 1))
)
(func (export "size") (result i32) (memory.size))
(func (export "load") (param i32) (result i32)
(i32.load8_s (local.get 0))
)
(func (export "store") (param i32 i32)
(i32.store8 (local.get 0) (local.get 1))
)
(data (i32.const 0x1000) "\01\02\03\04")
)
"#,
)?;
(data (i32.const 0x1000) "\01\02\03\04")
)
"#;
// Compile.
println!("Compiling module...");
let module = Module::new(&store, &binary).context("> Error compiling module!")?;
let module = Module::new(&store, &wat).context("> Error compiling module!")?;
// Instantiate.
println!("Instantiating module...");

View File

@@ -48,13 +48,9 @@ fn main() -> Result<()> {
let engine = Engine::new(Config::new().wasm_multi_value(true));
let store = Store::new(&engine);
// Load binary.
println!("Loading binary...");
let binary = wat::parse_str(WAT)?;
// Compile.
println!("Compiling module...");
let module = Module::new(&store, &binary).context("Error compiling module!")?;
let module = Module::new(&store, WAT).context("Error compiling module!")?;
// Create external print functions.
println!("Creating callback...");