Implement other variant-like types in adapter fusion (#4547)

This commit fills out the adapter fusion compiler for the `union`,
`enum`, `option,` and `result` types. The preexisting support for
`variant` types was refactored slightly to be extensible to all of these
other types and they all now call into the same common translation code.
This commit is contained in:
Alex Crichton
2022-07-28 11:47:15 -05:00
committed by GitHub
parent e1148e43be
commit ce7bbef24d
3 changed files with 258 additions and 36 deletions

View File

@@ -588,7 +588,7 @@ impl ComponentTypesBuilder {
}
wasmparser::ComponentDefinedType::List(e) => {
let ty = self.valtype(e);
InterfaceType::List(self.intern_interface_type(ty))
InterfaceType::List(self.add_interface_type(ty))
}
wasmparser::ComponentDefinedType::Tuple(e) => InterfaceType::Tuple(self.tuple_type(e)),
wasmparser::ComponentDefinedType::Flags(e) => InterfaceType::Flags(self.flags_type(e)),
@@ -596,7 +596,7 @@ impl ComponentTypesBuilder {
wasmparser::ComponentDefinedType::Union(e) => InterfaceType::Union(self.union_type(e)),
wasmparser::ComponentDefinedType::Option(e) => {
let ty = self.valtype(e);
InterfaceType::Option(self.intern_interface_type(ty))
InterfaceType::Option(self.add_interface_type(ty))
}
wasmparser::ComponentDefinedType::Expected { ok, error } => {
InterfaceType::Expected(self.expected_type(ok, error))
@@ -618,14 +618,6 @@ impl ComponentTypesBuilder {
}
}
fn intern_interface_type(&mut self, ty: InterfaceType) -> TypeInterfaceIndex {
intern(
&mut self.interface_types,
&mut self.component_types.interface_types,
ty,
)
}
fn record_type(&mut self, record: &[(&str, wasmparser::ComponentValType)]) -> TypeRecordIndex {
let record = TypeRecord {
fields: record
@@ -675,14 +667,14 @@ impl ComponentTypesBuilder {
let e = TypeEnum {
names: variants.iter().map(|s| s.to_string()).collect(),
};
intern(&mut self.enums, &mut self.component_types.enums, e)
self.add_enum_type(e)
}
fn union_type(&mut self, types: &[wasmparser::ComponentValType]) -> TypeUnionIndex {
let union = TypeUnion {
types: types.iter().map(|ty| self.valtype(ty)).collect(),
};
intern(&mut self.unions, &mut self.component_types.unions, union)
self.add_union_type(union)
}
fn expected_type(
@@ -694,11 +686,7 @@ impl ComponentTypesBuilder {
ok: self.valtype(ok),
err: self.valtype(err),
};
intern(
&mut self.expecteds,
&mut self.component_types.expecteds,
expected,
)
self.add_expected_type(expected)
}
/// Interns a new function type within this type information.
@@ -720,6 +708,30 @@ impl ComponentTypesBuilder {
pub fn add_variant_type(&mut self, ty: TypeVariant) -> TypeVariantIndex {
intern(&mut self.variants, &mut self.component_types.variants, ty)
}
/// Interns a new union type within this type information.
pub fn add_union_type(&mut self, ty: TypeUnion) -> TypeUnionIndex {
intern(&mut self.unions, &mut self.component_types.unions, ty)
}
/// Interns a new enum type within this type information.
pub fn add_enum_type(&mut self, ty: TypeEnum) -> TypeEnumIndex {
intern(&mut self.enums, &mut self.component_types.enums, ty)
}
/// Interns a new expected type within this type information.
pub fn add_expected_type(&mut self, ty: TypeExpected) -> TypeExpectedIndex {
intern(&mut self.expecteds, &mut self.component_types.expecteds, ty)
}
/// Interns a new expected type within this type information.
pub fn add_interface_type(&mut self, ty: InterfaceType) -> TypeInterfaceIndex {
intern(
&mut self.interface_types,
&mut self.component_types.interface_types,
ty,
)
}
}
// Forward the indexing impl to the internal `TypeTables`