Implement support for outer core type aliases (#4385)

Fill in the gaps of the implementation left after #4380.
This commit is contained in:
Alex Crichton
2022-07-07 11:38:27 -05:00
committed by GitHub
parent 7000b0a4cf
commit 038383dc42
3 changed files with 62 additions and 27 deletions

View File

@@ -583,9 +583,15 @@ impl<'a, 'data> Translator<'a, 'data> {
let instance = ModuleInstanceIndex::from_u32(instance_index); let instance = ModuleInstanceIndex::from_u32(instance_index);
self.alias_module_instance_export(kind, instance, name) self.alias_module_instance_export(kind, instance, name)
} }
wasmparser::Alias::Outer { kind, count, index } => { wasmparser::Alias::Outer {
drop((kind, count, index)); kind: wasmparser::OuterAliasKind::Type,
unimplemented!("outer core index"); count,
index,
} => {
let index = TypeIndex::from_u32(index);
let ty = self.types.core_outer_type(count, index);
self.types.push_core_typedef(ty);
continue;
} }
}; };
self.result.initializers.push(init); self.result.initializers.push(init);

View File

@@ -408,42 +408,57 @@ impl ComponentTypesBuilder {
ty: &[wasmparser::ModuleTypeDeclaration<'_>], ty: &[wasmparser::ModuleTypeDeclaration<'_>],
) -> Result<TypeModuleIndex> { ) -> Result<TypeModuleIndex> {
let mut result = TypeModule::default(); let mut result = TypeModule::default();
let mut functypes: PrimaryMap<TypeIndex, SignatureIndex> = PrimaryMap::default(); self.push_type_scope();
for item in ty { for item in ty {
match item { match item {
wasmparser::ModuleTypeDeclaration::Type(wasmparser::Type::Func(f)) => { wasmparser::ModuleTypeDeclaration::Type(wasmparser::Type::Func(f)) => {
functypes.push(self.module_types.wasm_func_type(f.clone().try_into()?)); let ty =
TypeDef::CoreFunc(self.module_types.wasm_func_type(f.clone().try_into()?));
self.push_core_typedef(ty);
} }
wasmparser::ModuleTypeDeclaration::Export { name, ty } => { wasmparser::ModuleTypeDeclaration::Export { name, ty } => {
let prev = result let prev = result
.exports .exports
.insert(name.to_string(), type_ref(ty, &functypes)?); .insert(name.to_string(), self.entity_type(ty)?);
assert!(prev.is_none()); assert!(prev.is_none());
} }
wasmparser::ModuleTypeDeclaration::Import(import) => { wasmparser::ModuleTypeDeclaration::Import(import) => {
let prev = result.imports.insert( let prev = result.imports.insert(
(import.module.to_string(), import.name.to_string()), (import.module.to_string(), import.name.to_string()),
type_ref(&import.ty, &functypes)?, self.entity_type(&import.ty)?,
); );
assert!(prev.is_none()); assert!(prev.is_none());
} }
wasmparser::ModuleTypeDeclaration::Alias(alias) => { wasmparser::ModuleTypeDeclaration::Alias(alias) => match alias {
drop(alias); wasmparser::Alias::Outer {
unimplemented!("outer alias in module type"); kind: wasmparser::OuterAliasKind::Type,
count,
index,
} => {
let ty = self.core_outer_type(*count, TypeIndex::from_u32(*index));
self.push_core_typedef(ty);
} }
wasmparser::Alias::InstanceExport { .. } => {
unreachable!("invalid alias {alias:?}")
}
},
} }
} }
return Ok(self.component_types.modules.push(result)); self.pop_type_scope();
fn type_ref( Ok(self.component_types.modules.push(result))
ty: &wasmparser::TypeRef, }
functypes: &PrimaryMap<TypeIndex, SignatureIndex>,
) -> Result<EntityType> { fn entity_type(&self, ty: &wasmparser::TypeRef) -> Result<EntityType> {
Ok(match ty { Ok(match ty {
wasmparser::TypeRef::Func(idx) => { wasmparser::TypeRef::Func(idx) => {
EntityType::Function(functypes[TypeIndex::from_u32(*idx)]) let idx = TypeIndex::from_u32(*idx);
match self.core_outer_type(0, idx) {
TypeDef::CoreFunc(idx) => EntityType::Function(idx),
_ => unreachable!(), // not possible with valid components
}
} }
wasmparser::TypeRef::Table(ty) => EntityType::Table(ty.clone().try_into()?), wasmparser::TypeRef::Table(ty) => EntityType::Table(ty.clone().try_into()?),
wasmparser::TypeRef::Memory(ty) => EntityType::Memory(ty.clone().into()), wasmparser::TypeRef::Memory(ty) => EntityType::Memory(ty.clone().into()),
@@ -453,7 +468,6 @@ impl ComponentTypesBuilder {
wasmparser::TypeRef::Tag(_) => bail!("exceptions proposal not implemented"), wasmparser::TypeRef::Tag(_) => bail!("exceptions proposal not implemented"),
}) })
} }
}
fn component_type( fn component_type(
&mut self, &mut self,

View File

@@ -86,3 +86,18 @@
(export "3" (table 1 funcref)) (export "3" (table 1 funcref))
)) ))
) )
;; outer core aliases work
(component $C
(core type $f (func))
(core type $m (module))
(component $C2
(alias outer $C $f (core type $my_f))
(import "" (core module (type $m)))
(import "x" (core module
(alias outer $C2 $my_f (type $my_f))
(import "" "1" (func (type $my_f)))
))
)
)