Skip to content

Commit

Permalink
Update wasm-tools for the latest component model proposal changes (by…
Browse files Browse the repository at this point in the history
…tecodealliance#703)

* wasm-encoder: update for component model proposal changes.

This commit updates `wasm-encoder` for the following component model proposal
changes:

* Removed the `unit` type (this changed the encoding byte for the rest of the
  value types).
* Case types of variants are now optional.
* `expected` is now `result` and the ok and error types are optional.
* Function types can now have either a single unnamed type or a vector of named
  types for parameters/results.

* wasmparser: update to latest component model spec changes.

This commit updates `wasmparser` for the following component model proposal
changes:

* Removed the `unit` type (this changed the byte parsed for the rest of the
  value types).
* Case types of variants are now optional.
* `expected` is now `result` and the ok and error types are optional.
* Function types can now have either a single unnamed type or a vector of named
  types for parameters/results.
* Validation now requires named function parameters and results to be unique.

* wasm-smith: update to the latest component model proposal changes.

This commit updates `wasm-smith` for the following component model proposal
changes:

* Removed the `unit` type.
* Case types of variants are now optional.
* `expected` is now `result` and the ok and error types are optional.
* Function types can now have either a single unnamed type or a vector of named
  types for parameters/results.
* Removed `NamedType` and `OptionalNameType` as they were now used in singular
  locations; instead `RecordType` and `FuncType` now just store simple tuples.

* wasmprinter: update for latest component model proposal changes.

* Removed the `unit` type.
* Types in variant cases are now optional.
* Renamed `expected` to `result`.
* Types in `result` are now optional.
* Print multiple results in function types.

* wasm-compose: update for latest component model proposal changes.

This commit updates `wasm-compose` for the recent component model proposal
changes:

* Removed the `unit` type.
* Changed how component functions are encoded.
* `expected` is now `result`.
* Types are now optional in variant cases.
* Types are now optional in results.

* wast: update to latest component model proposal changes.

* Removed the `unit` type.
* Variant case types are now optional.
* Renamed `expected` to `result`.
* `result` types are now optional.
* Support for multi-return component functions.

* Update tests for component model proposal changes.

* wasm-smith: keep the `refines` name when encoding a variant.
  • Loading branch information
peterhuene authored Aug 10, 2022
1 parent 5cb7452 commit 63f8ab3
Show file tree
Hide file tree
Showing 44 changed files with 1,267 additions and 879 deletions.
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

0 comments on commit 63f8ab3

Please sign in to comment.