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
This commit is contained in:
@@ -8,6 +8,7 @@ use wasmtime::Store;
|
||||
fn primitives() -> Result<()> {
|
||||
let engine = super::engine();
|
||||
let mut store = Store::new(&engine, ());
|
||||
let mut output = [Val::Bool(false)];
|
||||
|
||||
for (input, ty, param) in [
|
||||
(Val::Bool(true), "bool", Param(Type::U8, Some(0))),
|
||||
@@ -34,9 +35,9 @@ fn primitives() -> Result<()> {
|
||||
let component = Component::new(&engine, make_echo_component_with_params(ty, &[param]))?;
|
||||
let instance = Linker::new(&engine).instantiate(&mut store, &component)?;
|
||||
let func = instance.get_func(&mut store, "echo").unwrap();
|
||||
let output = func.call_and_post_return(&mut store, &[input.clone()])?;
|
||||
func.call_and_post_return(&mut store, &[input.clone()], &mut output)?;
|
||||
|
||||
assert_eq!(input, output);
|
||||
assert_eq!(input, output[0]);
|
||||
}
|
||||
|
||||
// Sad path: type mismatch
|
||||
@@ -48,7 +49,7 @@ fn primitives() -> Result<()> {
|
||||
let instance = Linker::new(&engine).instantiate(&mut store, &component)?;
|
||||
let func = instance.get_func(&mut store, "echo").unwrap();
|
||||
let err = func
|
||||
.call_and_post_return(&mut store, &[Val::U64(42)])
|
||||
.call_and_post_return(&mut store, &[Val::U64(42)], &mut output)
|
||||
.unwrap_err();
|
||||
|
||||
assert!(err.to_string().contains("type mismatch"), "{err}");
|
||||
@@ -62,6 +63,7 @@ fn primitives() -> Result<()> {
|
||||
Val::Float64(3.14159265_f64.to_bits()),
|
||||
Val::Float64(3.14159265_f64.to_bits()),
|
||||
],
|
||||
&mut output,
|
||||
)
|
||||
.unwrap_err();
|
||||
|
||||
@@ -72,13 +74,22 @@ fn primitives() -> Result<()> {
|
||||
|
||||
// Sad path: arity mismatch (too few)
|
||||
|
||||
let err = func.call_and_post_return(&mut store, &[]).unwrap_err();
|
||||
|
||||
let err = func
|
||||
.call_and_post_return(&mut store, &[], &mut output)
|
||||
.unwrap_err();
|
||||
assert!(
|
||||
err.to_string().contains("expected 1 argument(s), got 0"),
|
||||
"{err}"
|
||||
);
|
||||
|
||||
let err = func
|
||||
.call_and_post_return(&mut store, &output, &mut [])
|
||||
.unwrap_err();
|
||||
assert!(
|
||||
err.to_string().contains("expected 1 results(s), got 0"),
|
||||
"{err}"
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -91,9 +102,9 @@ fn strings() -> Result<()> {
|
||||
let instance = Linker::new(&engine).instantiate(&mut store, &component)?;
|
||||
let func = instance.get_func(&mut store, "echo").unwrap();
|
||||
let input = Val::String(Box::from("hello, component!"));
|
||||
let output = func.call_and_post_return(&mut store, &[input.clone()])?;
|
||||
|
||||
assert_eq!(input, output);
|
||||
let mut output = [Val::Bool(false)];
|
||||
func.call_and_post_return(&mut store, &[input.clone()], &mut output)?;
|
||||
assert_eq!(input, output[0]);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -112,9 +123,10 @@ fn lists() -> Result<()> {
|
||||
Val::U32(79023439),
|
||||
Val::U32(2084037802),
|
||||
]))?;
|
||||
let output = func.call_and_post_return(&mut store, &[input.clone()])?;
|
||||
let mut output = [Val::Bool(false)];
|
||||
func.call_and_post_return(&mut store, &[input.clone()], &mut output)?;
|
||||
|
||||
assert_eq!(input, output);
|
||||
assert_eq!(input, output[0]);
|
||||
|
||||
// Sad path: type mismatch
|
||||
|
||||
@@ -163,9 +175,10 @@ fn records() -> Result<()> {
|
||||
.new_val([("D", Val::Bool(false)), ("E", Val::U32(2084037802))])?,
|
||||
),
|
||||
])?;
|
||||
let output = func.call_and_post_return(&mut store, &[input.clone()])?;
|
||||
let mut output = [Val::Bool(false)];
|
||||
func.call_and_post_return(&mut store, &[input.clone()], &mut output)?;
|
||||
|
||||
assert_eq!(input, output);
|
||||
assert_eq!(input, output[0]);
|
||||
|
||||
// Sad path: type mismatch
|
||||
|
||||
@@ -198,7 +211,7 @@ fn records() -> Result<()> {
|
||||
.unwrap_record()
|
||||
.new_val([("D", Val::Bool(false)), ("E", Val::U32(2084037802))])?,
|
||||
),
|
||||
("F", Val::Unit),
|
||||
("F", Val::Bool(true)),
|
||||
])
|
||||
.unwrap_err();
|
||||
|
||||
@@ -246,54 +259,62 @@ fn variants() -> Result<()> {
|
||||
let ty = &func.params(&store)[0];
|
||||
let input = ty
|
||||
.unwrap_variant()
|
||||
.new_val("B", Val::Float64(3.14159265_f64.to_bits()))?;
|
||||
let output = func.call_and_post_return(&mut store, &[input.clone()])?;
|
||||
.new_val("B", Some(Val::Float64(3.14159265_f64.to_bits())))?;
|
||||
let mut output = [Val::Bool(false)];
|
||||
func.call_and_post_return(&mut store, &[input.clone()], &mut output)?;
|
||||
|
||||
assert_eq!(input, output);
|
||||
assert_eq!(input, output[0]);
|
||||
|
||||
// Do it again, this time using case "C"
|
||||
|
||||
let component = Component::new(
|
||||
&engine,
|
||||
dbg!(make_echo_component_with_params(
|
||||
make_echo_component_with_params(
|
||||
r#"(variant (case "A" u32) (case "B" float64) (case "C" (record (field "D" bool) (field "E" u32))))"#,
|
||||
&[
|
||||
Param(Type::U8, Some(0)),
|
||||
Param(Type::I64, Some(8)),
|
||||
Param(Type::I32, Some(12)),
|
||||
],
|
||||
)),
|
||||
),
|
||||
)?;
|
||||
let instance = Linker::new(&engine).instantiate(&mut store, &component)?;
|
||||
let func = instance.get_func(&mut store, "echo").unwrap();
|
||||
let ty = &func.params(&store)[0];
|
||||
let c_type = &ty.unwrap_variant().cases().nth(2).unwrap().ty;
|
||||
let c_type = &ty.unwrap_variant().cases().nth(2).unwrap().ty.unwrap();
|
||||
let input = ty.unwrap_variant().new_val(
|
||||
"C",
|
||||
c_type
|
||||
.unwrap_record()
|
||||
.new_val([("D", Val::Bool(true)), ("E", Val::U32(314159265))])?,
|
||||
Some(
|
||||
c_type
|
||||
.unwrap_record()
|
||||
.new_val([("D", Val::Bool(true)), ("E", Val::U32(314159265))])?,
|
||||
),
|
||||
)?;
|
||||
let output = func.call_and_post_return(&mut store, &[input.clone()])?;
|
||||
func.call_and_post_return(&mut store, &[input.clone()], &mut output)?;
|
||||
|
||||
assert_eq!(input, output);
|
||||
assert_eq!(input, output[0]);
|
||||
|
||||
// Sad path: type mismatch
|
||||
|
||||
let err = ty
|
||||
.unwrap_variant()
|
||||
.new_val("B", Val::U64(314159265))
|
||||
.new_val("B", Some(Val::U64(314159265)))
|
||||
.unwrap_err();
|
||||
|
||||
assert!(err.to_string().contains("type mismatch"), "{err}");
|
||||
let err = ty.unwrap_variant().new_val("B", None).unwrap_err();
|
||||
assert!(
|
||||
err.to_string().contains("expected a payload for case `B`"),
|
||||
"{err}"
|
||||
);
|
||||
|
||||
// Sad path: unknown case
|
||||
|
||||
let err = ty
|
||||
.unwrap_variant()
|
||||
.new_val("D", Val::U64(314159265))
|
||||
.new_val("D", Some(Val::U64(314159265)))
|
||||
.unwrap_err();
|
||||
|
||||
assert!(err.to_string().contains("unknown variant case"), "{err}");
|
||||
let err = ty.unwrap_variant().new_val("D", None).unwrap_err();
|
||||
assert!(err.to_string().contains("unknown variant case"), "{err}");
|
||||
|
||||
// Make sure we lift variants which have cases of different sizes with the correct alignment
|
||||
@@ -323,13 +344,15 @@ fn variants() -> Result<()> {
|
||||
let input = ty.unwrap_record().new_val([
|
||||
(
|
||||
"A",
|
||||
a_type.unwrap_variant().new_val("A", Val::U32(314159265))?,
|
||||
a_type
|
||||
.unwrap_variant()
|
||||
.new_val("A", Some(Val::U32(314159265)))?,
|
||||
),
|
||||
("B", Val::U32(628318530)),
|
||||
])?;
|
||||
let output = func.call_and_post_return(&mut store, &[input.clone()])?;
|
||||
func.call_and_post_return(&mut store, &[input.clone()], &mut output)?;
|
||||
|
||||
assert_eq!(input, output);
|
||||
assert_eq!(input, output[0]);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -350,9 +373,10 @@ fn flags() -> Result<()> {
|
||||
let func = instance.get_func(&mut store, "echo").unwrap();
|
||||
let ty = &func.params(&store)[0];
|
||||
let input = ty.unwrap_flags().new_val(&["B", "D"])?;
|
||||
let output = func.call_and_post_return(&mut store, &[input.clone()])?;
|
||||
let mut output = [Val::Bool(false)];
|
||||
func.call_and_post_return(&mut store, &[input.clone()], &mut output)?;
|
||||
|
||||
assert_eq!(input, output);
|
||||
assert_eq!(input, output[0]);
|
||||
|
||||
// Sad path: unknown flags
|
||||
|
||||
@@ -391,11 +415,10 @@ fn everything() -> Result<()> {
|
||||
(field "U" float64)
|
||||
(field "V" string)
|
||||
(field "W" char)
|
||||
(field "X" unit)
|
||||
(field "Y" (tuple u32 u32))
|
||||
(field "Z" (union u32 float64))
|
||||
(field "AA" (option u32))
|
||||
(field "BB" (expected string string))
|
||||
(field "BB" (result string (error string)))
|
||||
)"#,
|
||||
&[
|
||||
Param(Type::I32, Some(0)),
|
||||
@@ -436,7 +459,7 @@ fn everything() -> Result<()> {
|
||||
.map(|field| field.ty)
|
||||
.collect::<Box<[component::Type]>>();
|
||||
let (b_type, c_type, f_type, j_type, y_type, z_type, aa_type, bb_type) = (
|
||||
&types[1], &types[2], &types[3], &types[4], &types[14], &types[15], &types[16], &types[17],
|
||||
&types[1], &types[2], &types[3], &types[4], &types[13], &types[14], &types[15], &types[16],
|
||||
);
|
||||
let f_element_type = &f_type.unwrap_list().ty();
|
||||
let input = ty.unwrap_record().new_val([
|
||||
@@ -458,7 +481,7 @@ fn everything() -> Result<()> {
|
||||
"J",
|
||||
j_type
|
||||
.unwrap_variant()
|
||||
.new_val("L", Val::Float64(3.14159265_f64.to_bits()))?,
|
||||
.new_val("L", Some(Val::Float64(3.14159265_f64.to_bits())))?,
|
||||
),
|
||||
("P", Val::S8(42)),
|
||||
("Q", Val::S16(4242)),
|
||||
@@ -468,7 +491,6 @@ fn everything() -> Result<()> {
|
||||
("U", Val::Float64(3.14159265_f64.to_bits())),
|
||||
("V", Val::String(Box::from("wow, nice types"))),
|
||||
("W", Val::Char('🦀')),
|
||||
("X", Val::Unit),
|
||||
(
|
||||
"Y",
|
||||
y_type
|
||||
@@ -488,13 +510,14 @@ fn everything() -> Result<()> {
|
||||
(
|
||||
"BB",
|
||||
bb_type
|
||||
.unwrap_expected()
|
||||
.new_val(Ok(Val::String(Box::from("no problem"))))?,
|
||||
.unwrap_result()
|
||||
.new_val(Ok(Some(Val::String(Box::from("no problem")))))?,
|
||||
),
|
||||
])?;
|
||||
let output = func.call_and_post_return(&mut store, &[input.clone()])?;
|
||||
let mut output = [Val::Bool(false)];
|
||||
func.call_and_post_return(&mut store, &[input.clone()], &mut output)?;
|
||||
|
||||
assert_eq!(input, output);
|
||||
assert_eq!(input, output[0]);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user