* 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
382 lines
14 KiB
Rust
382 lines
14 KiB
Rust
use crate::core;
|
|
use anyhow::{anyhow, bail, Context, Result};
|
|
use std::collections::{BTreeSet, HashMap};
|
|
use std::fmt::Debug;
|
|
use wast::component::WastVal;
|
|
use wast::core::NanPattern;
|
|
|
|
pub use wasmtime::component::*;
|
|
|
|
pub fn val(v: &WastVal<'_>, ty: &Type) -> Result<Val> {
|
|
Ok(match v {
|
|
WastVal::Bool(b) => Val::Bool(*b),
|
|
WastVal::U8(b) => Val::U8(*b),
|
|
WastVal::S8(b) => Val::S8(*b),
|
|
WastVal::U16(b) => Val::U16(*b),
|
|
WastVal::S16(b) => Val::S16(*b),
|
|
WastVal::U32(b) => Val::U32(*b),
|
|
WastVal::S32(b) => Val::S32(*b),
|
|
WastVal::U64(b) => Val::U64(*b),
|
|
WastVal::S64(b) => Val::S64(*b),
|
|
WastVal::Float32(b) => Val::Float32(b.bits),
|
|
WastVal::Float64(b) => Val::Float64(b.bits),
|
|
WastVal::Char(b) => Val::Char(*b),
|
|
WastVal::String(s) => Val::String(s.to_string().into()),
|
|
WastVal::List(vals) => match ty {
|
|
Type::List(t) => {
|
|
let element = t.ty();
|
|
let vals = vals
|
|
.iter()
|
|
.map(|v| val(v, &element))
|
|
.collect::<Result<Vec<_>>>()?;
|
|
t.new_val(vals.into())?
|
|
}
|
|
_ => bail!("expected a list value"),
|
|
},
|
|
WastVal::Record(fields) => match ty {
|
|
Type::Record(t) => {
|
|
let mut fields_by_name = HashMap::new();
|
|
for (name, val) in fields {
|
|
let prev = fields_by_name.insert(*name, val);
|
|
if prev.is_some() {
|
|
bail!("field `{name}` specified twice");
|
|
}
|
|
}
|
|
let mut values = Vec::new();
|
|
for field in t.fields() {
|
|
let name = field.name;
|
|
let v = fields_by_name
|
|
.remove(name)
|
|
.ok_or_else(|| anyhow!("field `{name}` not specified"))?;
|
|
values.push((name, val(v, &field.ty)?));
|
|
}
|
|
if let Some((field, _)) = fields_by_name.iter().next() {
|
|
bail!("extra field `{field}` specified");
|
|
}
|
|
t.new_val(values)?
|
|
}
|
|
_ => bail!("expected a record value"),
|
|
},
|
|
WastVal::Tuple(vals) => match ty {
|
|
Type::Tuple(t) => {
|
|
if vals.len() != t.types().len() {
|
|
bail!("expected {} values got {}", t.types().len(), vals.len());
|
|
}
|
|
t.new_val(
|
|
vals.iter()
|
|
.zip(t.types())
|
|
.map(|(v, ty)| val(v, &ty))
|
|
.collect::<Result<Vec<_>>>()?
|
|
.into(),
|
|
)?
|
|
}
|
|
_ => bail!("expected a tuple value"),
|
|
},
|
|
WastVal::Enum(name) => match ty {
|
|
Type::Enum(t) => t.new_val(name)?,
|
|
_ => bail!("expected an enum value"),
|
|
},
|
|
WastVal::Variant(name, payload) => match ty {
|
|
Type::Variant(t) => {
|
|
let case = match t.cases().find(|c| c.name == *name) {
|
|
Some(case) => case,
|
|
None => bail!("no case named `{}", name),
|
|
};
|
|
let payload = payload_val(case.name, payload.as_deref(), case.ty.as_ref())?;
|
|
t.new_val(name, payload)?
|
|
}
|
|
_ => bail!("expected a variant value"),
|
|
},
|
|
WastVal::Union(idx, payload) => match ty {
|
|
Type::Union(t) => {
|
|
let case = match t.types().nth(*idx as usize) {
|
|
Some(case) => case,
|
|
None => bail!("case {idx} too large"),
|
|
};
|
|
let payload = val(payload, &case)?;
|
|
t.new_val(*idx, payload)?
|
|
}
|
|
_ => bail!("expected a union value"),
|
|
},
|
|
WastVal::Option(v) => match ty {
|
|
Type::Option(t) => {
|
|
let v = match v {
|
|
Some(v) => Some(val(v, &t.ty())?),
|
|
None => None,
|
|
};
|
|
t.new_val(v)?
|
|
}
|
|
_ => bail!("expected an option value"),
|
|
},
|
|
WastVal::Result(v) => match ty {
|
|
Type::Result(t) => {
|
|
let v = match v {
|
|
Ok(v) => Ok(payload_val("ok", v.as_deref(), t.ok().as_ref())?),
|
|
Err(v) => Err(payload_val("err", v.as_deref(), t.err().as_ref())?),
|
|
};
|
|
t.new_val(v)?
|
|
}
|
|
_ => bail!("expected an expected value"),
|
|
},
|
|
WastVal::Flags(v) => match ty {
|
|
Type::Flags(t) => t.new_val(v)?,
|
|
_ => bail!("expected a flags value"),
|
|
},
|
|
})
|
|
}
|
|
|
|
fn payload_val(name: &str, v: Option<&WastVal<'_>>, ty: Option<&Type>) -> Result<Option<Val>> {
|
|
match (v, ty) {
|
|
(Some(v), Some(ty)) => Ok(Some(val(v, ty)?)),
|
|
(None, None) => Ok(None),
|
|
(Some(_), None) => bail!("expected payload for case `{name}`"),
|
|
(None, Some(_)) => bail!("unexpected payload for case `{name}`"),
|
|
}
|
|
}
|
|
|
|
pub fn match_val(expected: &WastVal<'_>, actual: &Val) -> Result<()> {
|
|
match expected {
|
|
WastVal::Bool(e) => match actual {
|
|
Val::Bool(a) => match_debug(a, e),
|
|
_ => mismatch(expected, actual),
|
|
},
|
|
WastVal::U8(e) => match actual {
|
|
Val::U8(a) => core::match_int(a, e),
|
|
_ => mismatch(expected, actual),
|
|
},
|
|
WastVal::S8(e) => match actual {
|
|
Val::S8(a) => core::match_int(a, e),
|
|
_ => mismatch(expected, actual),
|
|
},
|
|
WastVal::U16(e) => match actual {
|
|
Val::U16(a) => core::match_int(a, e),
|
|
_ => mismatch(expected, actual),
|
|
},
|
|
WastVal::S16(e) => match actual {
|
|
Val::S16(a) => core::match_int(a, e),
|
|
_ => mismatch(expected, actual),
|
|
},
|
|
WastVal::U32(e) => match actual {
|
|
Val::U32(a) => core::match_int(a, e),
|
|
_ => mismatch(expected, actual),
|
|
},
|
|
WastVal::S32(e) => match actual {
|
|
Val::S32(a) => core::match_int(a, e),
|
|
_ => mismatch(expected, actual),
|
|
},
|
|
WastVal::U64(e) => match actual {
|
|
Val::U64(a) => core::match_int(a, e),
|
|
_ => mismatch(expected, actual),
|
|
},
|
|
WastVal::S64(e) => match actual {
|
|
Val::S64(a) => core::match_int(a, e),
|
|
_ => mismatch(expected, actual),
|
|
},
|
|
WastVal::Float32(e) => match actual {
|
|
Val::Float32(a) => core::match_f32(*a, &NanPattern::Value(*e)),
|
|
_ => mismatch(expected, actual),
|
|
},
|
|
WastVal::Float64(e) => match actual {
|
|
Val::Float64(a) => core::match_f64(*a, &NanPattern::Value(*e)),
|
|
_ => mismatch(expected, actual),
|
|
},
|
|
WastVal::Char(e) => match actual {
|
|
Val::Char(a) => match_debug(a, e),
|
|
_ => mismatch(expected, actual),
|
|
},
|
|
WastVal::String(e) => match actual {
|
|
Val::String(a) => match_debug(&a[..], *e),
|
|
_ => mismatch(expected, actual),
|
|
},
|
|
WastVal::List(e) => match actual {
|
|
Val::List(a) => {
|
|
if e.len() != a.len() {
|
|
bail!("expected {} values got {}", e.len(), a.len());
|
|
}
|
|
for (i, (expected, actual)) in e.iter().zip(a.iter()).enumerate() {
|
|
match_val(expected, actual)
|
|
.with_context(|| format!("failed to match list element {i}"))?;
|
|
}
|
|
Ok(())
|
|
}
|
|
_ => mismatch(expected, actual),
|
|
},
|
|
WastVal::Record(e) => match actual {
|
|
Val::Record(a) => {
|
|
let mut fields_by_name = HashMap::new();
|
|
for (name, val) in e {
|
|
let prev = fields_by_name.insert(*name, val);
|
|
if prev.is_some() {
|
|
bail!("field `{name}` specified twice");
|
|
}
|
|
}
|
|
for (name, actual) in a.fields() {
|
|
let expected = fields_by_name
|
|
.remove(name)
|
|
.ok_or_else(|| anyhow!("field `{name}` not specified"))?;
|
|
match_val(expected, actual)
|
|
.with_context(|| format!("failed to match field `{name}`"))?;
|
|
}
|
|
if let Some((field, _)) = fields_by_name.iter().next() {
|
|
bail!("extra field `{field}` specified");
|
|
}
|
|
Ok(())
|
|
}
|
|
_ => mismatch(expected, actual),
|
|
},
|
|
WastVal::Tuple(e) => match actual {
|
|
Val::Tuple(a) => {
|
|
if e.len() != a.values().len() {
|
|
bail!(
|
|
"expected {}-tuple, found {}-tuple",
|
|
e.len(),
|
|
a.values().len()
|
|
);
|
|
}
|
|
for (i, (expected, actual)) in e.iter().zip(a.values()).enumerate() {
|
|
match_val(expected, actual)
|
|
.with_context(|| format!("failed to match tuple element {i}"))?;
|
|
}
|
|
Ok(())
|
|
}
|
|
_ => mismatch(expected, actual),
|
|
},
|
|
WastVal::Variant(name, e) => match actual {
|
|
Val::Variant(a) => {
|
|
if a.discriminant() != *name {
|
|
bail!("expected discriminant `{name}` got `{}`", a.discriminant());
|
|
}
|
|
match_payload_val(name, e.as_deref(), a.payload())
|
|
}
|
|
_ => mismatch(expected, actual),
|
|
},
|
|
WastVal::Enum(name) => match actual {
|
|
Val::Enum(a) => {
|
|
if a.discriminant() != *name {
|
|
bail!("expected discriminant `{name}` got `{}`", a.discriminant());
|
|
} else {
|
|
Ok(())
|
|
}
|
|
}
|
|
_ => mismatch(expected, actual),
|
|
},
|
|
WastVal::Union(idx, e) => match actual {
|
|
Val::Union(a) => {
|
|
if a.discriminant() != *idx {
|
|
bail!("expected discriminant `{idx}` got `{}`", a.discriminant());
|
|
}
|
|
match_val(e, a.payload())
|
|
}
|
|
_ => mismatch(expected, actual),
|
|
},
|
|
WastVal::Option(e) => match actual {
|
|
Val::Option(a) => match (e, a.value()) {
|
|
(None, None) => Ok(()),
|
|
(Some(expected), Some(actual)) => match_val(expected, actual),
|
|
(None, Some(_)) => bail!("expected `none`, found `some`"),
|
|
(Some(_), None) => bail!("expected `some`, found `none`"),
|
|
},
|
|
_ => mismatch(expected, actual),
|
|
},
|
|
WastVal::Result(e) => match actual {
|
|
Val::Result(a) => match (e, a.value()) {
|
|
(Ok(_), Err(_)) => bail!("expected `ok`, found `err`"),
|
|
(Err(_), Ok(_)) => bail!("expected `err`, found `ok`"),
|
|
(Err(e), Err(a)) => match_payload_val("err", e.as_deref(), a),
|
|
(Ok(e), Ok(a)) => match_payload_val("ok", e.as_deref(), a),
|
|
},
|
|
_ => mismatch(expected, actual),
|
|
},
|
|
WastVal::Flags(e) => match actual {
|
|
Val::Flags(a) => {
|
|
let expected = e.iter().copied().collect::<BTreeSet<_>>();
|
|
let actual = a.flags().collect::<BTreeSet<_>>();
|
|
match_debug(&actual, &expected)
|
|
}
|
|
_ => mismatch(expected, actual),
|
|
},
|
|
}
|
|
}
|
|
|
|
fn match_payload_val(
|
|
name: &str,
|
|
expected: Option<&WastVal<'_>>,
|
|
actual: Option<&Val>,
|
|
) -> Result<()> {
|
|
match (expected, actual) {
|
|
(Some(e), Some(a)) => {
|
|
match_val(e, a).with_context(|| format!("failed to match case `{name}`"))
|
|
}
|
|
(None, None) => Ok(()),
|
|
(Some(_), None) => bail!("expected payload for case `{name}`"),
|
|
(None, Some(_)) => bail!("unexpected payload for case `{name}`"),
|
|
}
|
|
}
|
|
|
|
fn match_debug<T>(actual: &T, expected: &T) -> Result<()>
|
|
where
|
|
T: Eq + Debug + ?Sized,
|
|
{
|
|
if actual == expected {
|
|
Ok(())
|
|
} else {
|
|
bail!(
|
|
"
|
|
expected {expected:?}
|
|
actual {actual:?}"
|
|
)
|
|
}
|
|
}
|
|
|
|
fn mismatch(expected: &WastVal<'_>, actual: &Val) -> Result<()> {
|
|
let expected = match expected {
|
|
WastVal::Bool(..) => "bool",
|
|
WastVal::U8(..) => "u8",
|
|
WastVal::S8(..) => "s8",
|
|
WastVal::U16(..) => "u16",
|
|
WastVal::S16(..) => "s16",
|
|
WastVal::U32(..) => "u32",
|
|
WastVal::S32(..) => "s32",
|
|
WastVal::U64(..) => "u64",
|
|
WastVal::S64(..) => "s64",
|
|
WastVal::Float32(..) => "float32",
|
|
WastVal::Float64(..) => "float64",
|
|
WastVal::Char(..) => "char",
|
|
WastVal::String(..) => "string",
|
|
WastVal::List(..) => "list",
|
|
WastVal::Record(..) => "record",
|
|
WastVal::Tuple(..) => "tuple",
|
|
WastVal::Enum(..) => "enum",
|
|
WastVal::Variant(..) => "variant",
|
|
WastVal::Union(..) => "union",
|
|
WastVal::Option(..) => "option",
|
|
WastVal::Result(..) => "result",
|
|
WastVal::Flags(..) => "flags",
|
|
};
|
|
let actual = match actual {
|
|
Val::Bool(..) => "bool",
|
|
Val::U8(..) => "u8",
|
|
Val::S8(..) => "s8",
|
|
Val::U16(..) => "u16",
|
|
Val::S16(..) => "s16",
|
|
Val::U32(..) => "u32",
|
|
Val::S32(..) => "s32",
|
|
Val::U64(..) => "u64",
|
|
Val::S64(..) => "s64",
|
|
Val::Float32(..) => "float32",
|
|
Val::Float64(..) => "float64",
|
|
Val::Char(..) => "char",
|
|
Val::String(..) => "string",
|
|
Val::List(..) => "list",
|
|
Val::Record(..) => "record",
|
|
Val::Tuple(..) => "tuple",
|
|
Val::Enum(..) => "enum",
|
|
Val::Variant(..) => "variant",
|
|
Val::Union(..) => "union",
|
|
Val::Option(..) => "option",
|
|
Val::Result(..) => "result",
|
|
Val::Flags(..) => "flags",
|
|
};
|
|
bail!("expected `{expected}` got `{actual}`")
|
|
}
|