Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update wasm-tools for the latest component model proposal changes #703

Merged
merged 8 commits into from
Aug 10, 2022
62 changes: 43 additions & 19 deletions crates/wasm-compose/src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ impl<'a> TypeEncoder<'a> {

fn primitive(ty: wasmparser::PrimitiveValType) -> PrimitiveValType {
match ty {
wasmparser::PrimitiveValType::Unit => PrimitiveValType::Unit,
wasmparser::PrimitiveValType::Bool => PrimitiveValType::Bool,
wasmparser::PrimitiveValType::S8 => PrimitiveValType::S8,
wasmparser::PrimitiveValType::U8 => PrimitiveValType::U8,
Expand Down Expand Up @@ -334,18 +333,43 @@ impl<'a> TypeEncoder<'a> {
.as_component_func_type()
.unwrap();

let mut params = Vec::with_capacity(ty.params.len());
for (name, ty) in ty.params.iter() {
params.push((
name.as_deref(),
self.component_val_type(encodable, types, *ty),
));
}
let params = ty
.params
.iter()
.map(|(name, ty)| {
(
name.as_deref(),
self.component_val_type(encodable, types, *ty),
)
})
.collect::<Vec<_>>();

let result = self.component_val_type(encodable, types, ty.result);
let results = ty
.results
.iter()
.map(|(name, ty)| {
(
name.as_deref(),
self.component_val_type(encodable, types, *ty),
)
})
.collect::<Vec<_>>();

let index = encodable.type_count();
encodable.ty().function(params, result);
let mut f = encodable.ty().function();

if params.len() == 1 && params[0].0.is_none() {
f.param(params[0].1);
} else {
f.params(params.into_iter().map(|(name, ty)| (name.unwrap(), ty)));
}

if results.len() == 1 && results[0].0.is_none() {
f.result(results[0].1);
} else {
f.results(results.into_iter().map(|(name, ty)| (name.unwrap(), ty)));
}

types.insert(id, index);
index
}
Expand Down Expand Up @@ -425,8 +449,8 @@ impl<'a> TypeEncoder<'a> {
wasmparser::types::ComponentDefinedType::Option(ty) => {
self.option(encodable, types, *ty)
}
wasmparser::types::ComponentDefinedType::Expected(ok, err) => {
self.expected(encodable, types, *ok, *err)
wasmparser::types::ComponentDefinedType::Result { ok, err } => {
self.result(encodable, types, *ok, *err)
}
};

Expand Down Expand Up @@ -463,7 +487,7 @@ impl<'a> TypeEncoder<'a> {
.map(|(n, c)| {
(
n.as_str(),
self.component_val_type(encodable, types, c.ty),
c.ty.map(|ty| self.component_val_type(encodable, types, ty)),
c.refines
.as_deref()
.map(|r| variant.cases.iter().position(|(n, _)| n == r).unwrap() as u32),
Expand Down Expand Up @@ -551,18 +575,18 @@ impl<'a> TypeEncoder<'a> {
index
}

fn expected(
fn result(
&self,
encodable: &mut impl Encodable,
types: &mut HashMap<TypeId, u32>,
ok: wasmparser::types::ComponentValType,
err: wasmparser::types::ComponentValType,
ok: Option<wasmparser::types::ComponentValType>,
err: Option<wasmparser::types::ComponentValType>,
) -> u32 {
let ok = self.component_val_type(encodable, types, ok);
let err = self.component_val_type(encodable, types, err);
let ok = ok.map(|ty| self.component_val_type(encodable, types, ty));
let err = err.map(|ty| self.component_val_type(encodable, types, ty));

let index = encodable.type_count();
encodable.ty().defined_type().expected(ok, err);
encodable.ty().defined_type().result(ok, err);
index
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
(type (;26;) (option 4))
(type (;27;) (func (param "x" 26)))
(export "t" (func (type 27)))
(type (;28;) (expected 0 string))
(type (;28;) (result 0 (error string)))
(type (;29;) (func (result 28)))
(export "u" (func (type 29)))
)
Expand Down Expand Up @@ -117,7 +117,7 @@
(type (;26;) (option 25))
(type (;27;) (func (param "x" 26)))
(export "t" (func (type 27)))
(type (;28;) (expected 13 string))
(type (;28;) (result 13 (error string)))
(type (;29;) (func (result 28)))
(export "u" (func (type 29)))
)
Expand Down
2 changes: 1 addition & 1 deletion crates/wasm-compose/tests/compositions/complex/a.wat
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
(type (variant (case "a" s8) (case "b" u8) (case "c" s16) (case "d" u16) (case "e" s32) (case "f" u32) (case "g" s64) (case "h" u64) (case "i" float32) (case "j" float64) (case "k" bool) (case "l" string) (case "m" 13)))
(type (option 25))
(type (func (param "x" 26)))
(type (expected 13 string))
(type (result 13 (error string)))
(type (func (result 28)))
(export "record1" (type 13))
(export "flags1" (type 19))
Expand Down
2 changes: 1 addition & 1 deletion crates/wasm-compose/tests/compositions/complex/b.wat
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
(type (variant (case "a" s8) (case "b" u8) (case "c" s16) (case "d" u16) (case "e" s32) (case "f" u32) (case "g" s64) (case "h" u64) (case "i" float32) (case "j" float64) (case "k" bool) (case "l" string) (case "m" 13)))
(type (option 25))
(type (func (param "x" 26)))
(type (expected 13 string))
(type (result 13 (error string)))
(type (func (result 28)))
(type
(instance
Expand Down
4 changes: 2 additions & 2 deletions crates/wasm-compose/tests/compositions/complex/composed.wat
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
(type (;25;) (variant (case "a" s8) (case "b" u8) (case "c" s16) (case "d" u16) (case "e" s32) (case "f" u32) (case "g" s64) (case "h" u64) (case "i" float32) (case "j" float64) (case "k" bool) (case "l" string) (case "m" 13)))
(type (;26;) (option 25))
(type (;27;) (func (param "x" 26)))
(type (;28;) (expected 13 string))
(type (;28;) (result 13 (error string)))
(type (;29;) (func (result 28)))
(export "record1" (type 13))
(export "flags1" (type 19))
Expand Down Expand Up @@ -235,7 +235,7 @@
(type (;25;) (variant (case "a" s8) (case "b" u8) (case "c" s16) (case "d" u16) (case "e" s32) (case "f" u32) (case "g" s64) (case "h" u64) (case "i" float32) (case "j" float64) (case "k" bool) (case "l" string) (case "m" 13)))
(type (;26;) (option 25))
(type (;27;) (func (param "x" 26)))
(type (;28;) (expected 13 string))
(type (;28;) (result 13 (error string)))
(type (;29;) (func (result 28)))
(type (;30;)
(instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ failed to parse component `tests/compositions/component-not-valid/a.wat`

Caused by:
0: failed to validate WebAssembly component `tests/compositions/component-not-valid/a.wat`
1: unknown core function 0: function index out of bounds (at offset 0x11)
1: unknown core function 0: function index out of bounds (at offset 0x13)
16 changes: 9 additions & 7 deletions crates/wasm-encoder/src/component/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,15 @@ impl Encode for ComponentTypeRef {
/// let mut types = ComponentTypeSection::new();
///
/// // Define a function type of `[string, string] -> string`.
/// types.function(
/// [
/// (Some("a"), PrimitiveValType::String),
/// (Some("b"), PrimitiveValType::String)
/// ],
/// PrimitiveValType::String
/// );
/// types
/// .function()
/// .params(
/// [
/// ("a", PrimitiveValType::String),
/// ("b", PrimitiveValType::String)
/// ]
/// )
/// .result(PrimitiveValType::String);
///
/// // This imports a function named `f` with the type defined above
/// let mut imports = ComponentImportSection::new();
Expand Down
Loading