Files
wasmtime/tests/all/component_model/nested.rs
Alex Crichton 57dca934ad Upgrade wasm-tools crates, namely the component model (#4715)
* Upgrade wasm-tools crates, namely the component model

This commit pulls in the latest versions of all of the `wasm-tools`
family of crates. There were two major changes that happened in
`wasm-tools` in the meantime:

* bytecodealliance/wasm-tools#697 - this commit introduced a new API for
  more efficiently reading binary operators from a wasm binary. The old
  `Operator`-based reading was left in place, however, and continues to
  be what Wasmtime uses. I hope to update Wasmtime in a future PR to use
  this new API, but for now the biggest change is...

* bytecodealliance/wasm-tools#703 - this commit was a major update to
  the component model AST. This commit almost entirely deals with the
  fallout of this change.

The changes made to the component model were:

1. The `unit` type no longer exists. This was generally a simple change
   where the `Unit` case in a few different locations were all removed.
2. The `expected` type was renamed to `result`. This similarly was
   relatively lightweight and mostly just a renaming on the surface. I
   took this opportunity to rename `val::Result` to `val::ResultVal` and
   `types::Result` to `types::ResultType` to avoid clashing with the
   standard library types. The `Option`-based types were handled with
   this as well.
3. The payload type of `variant` and `result` types are now optional.
   This affected many locations that calculate flat type
   representations, ABI information, etc. The `#[derive(ComponentType)]`
   macro now specifically handles Rust-defined `enum` types which have
   no payload to the equivalent in the component model.
4. Functions can now return multiple parameters. This changed the
   signature of invoking component functions because the return value is
   now bound by `ComponentNamedList` (renamed from `ComponentParams`).
   This had a large effect in the tests, fuzz test case generation, etc.
5. Function types with 2-or-more parameters/results must uniquely name
   all parameters/results. This mostly affected the text format used
   throughout the tests.

I haven't added specifically new tests for multi-return but I changed a
number of tests to use it. Additionally I've updated the fuzzers to all
exercise multi-return as well so I think we should get some good
coverage with that.

* Update version numbers

* Use crates.io
2022-08-17 16:17:34 +00:00

174 lines
4.6 KiB
Rust

use super::REALLOC_AND_FREE;
use anyhow::Result;
use wasmtime::component::*;
use wasmtime::{Module, Store, StoreContextMut};
#[test]
fn top_level_instance_two_level() -> Result<()> {
let component = r#"
(component
(import "c" (instance $i
(export "c" (instance
(export "m" (core module
(export "g" (global i32))
))
))
))
(component $c1
(import "c" (instance $i
(export "c" (instance
(export "m" (core module
(export "g" (global i32))
))
))
))
(core module $verify
(import "" "g" (global i32))
(func $start
global.get 0
i32.const 101
i32.ne
if unreachable end
)
(start $start)
)
(core instance $m (instantiate (module $i "c" "m")))
(core instance (instantiate $verify (with "" (instance $m))))
)
(instance (instantiate $c1 (with "c" (instance $i))))
)
"#;
let module = r#"
(module
(global (export "g") i32 i32.const 101)
)
"#;
let engine = super::engine();
let module = Module::new(&engine, module)?;
let component = Component::new(&engine, component)?;
let mut store = Store::new(&engine, ());
let mut linker = Linker::new(&engine);
linker.instance("c")?.instance("c")?.module("m", &module)?;
linker.instantiate(&mut store, &component)?;
Ok(())
}
#[test]
fn nested_many_instantiations() -> Result<()> {
let component = r#"
(component
(import "count" (func $count))
(component $c1
(import "count" (func $count))
(core func $count_lower (canon lower (func $count)))
(core module $m
(import "" "" (func $count))
(start $count)
)
(core instance (instantiate $m (with "" (instance (export "" (func $count_lower))))))
(core instance (instantiate $m (with "" (instance (export "" (func $count_lower))))))
)
(component $c2
(import "count" (func $count))
(instance (instantiate $c1 (with "count" (func $count))))
(instance (instantiate $c1 (with "count" (func $count))))
)
(component $c3
(import "count" (func $count))
(instance (instantiate $c2 (with "count" (func $count))))
(instance (instantiate $c2 (with "count" (func $count))))
)
(component $c4
(import "count" (func $count))
(instance (instantiate $c3 (with "count" (func $count))))
(instance (instantiate $c3 (with "count" (func $count))))
)
(instance (instantiate $c4 (with "count" (func $count))))
)
"#;
let engine = super::engine();
let component = Component::new(&engine, component)?;
let mut store = Store::new(&engine, 0);
let mut linker = Linker::new(&engine);
linker
.root()
.func_wrap("count", |mut store: StoreContextMut<'_, u32>| {
*store.data_mut() += 1;
Ok(())
})?;
linker.instantiate(&mut store, &component)?;
assert_eq!(*store.data(), 16);
Ok(())
}
#[test]
fn thread_options_through_inner() -> Result<()> {
let component = format!(
r#"
(component
(import "hostfn" (func $host (param u32) (result string)))
(component $c
(import "hostfn" (func $host (param u32) (result string)))
(core module $libc
(memory (export "memory") 1)
{REALLOC_AND_FREE}
)
(core instance $libc (instantiate $libc))
(core func $host_lower
(canon lower
(func $host)
(memory $libc "memory")
(realloc (func $libc "realloc"))
)
)
(core module $m
(import "" "host" (func $host (param i32 i32)))
(import "libc" "memory" (memory 1))
(func (export "run") (param i32) (result i32)
i32.const 42
i32.const 100
call $host
i32.const 100
)
(export "memory" (memory 0))
)
(core instance $m (instantiate $m
(with "" (instance (export "host" (func $host_lower))))
(with "libc" (instance $libc))
))
(func (export "run") (param u32) (result string)
(canon lift
(core func $m "run")
(memory $m "memory")
)
)
)
(instance $c (instantiate $c (with "hostfn" (func $host))))
(export "run" (func $c "run"))
)
"#
);
let engine = super::engine();
let component = Component::new(&engine, component)?;
let mut store = Store::new(&engine, 0);
let mut linker = Linker::new(&engine);
linker
.root()
.func_wrap("hostfn", |param: u32| Ok((param.to_string(),)))?;
let instance = linker.instantiate(&mut store, &component)?;
let result = instance
.get_typed_func::<(u32,), (WasmStr,), _>(&mut store, "run")?
.call(&mut store, (43,))?
.0;
assert_eq!(result.to_str(&store)?, "42");
Ok(())
}