From 038383dc42830fdde454c6e7ed58894a2d4fbd14 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 7 Jul 2022 11:38:27 -0500 Subject: [PATCH] Implement support for outer core type aliases (#4385) Fill in the gaps of the implementation left after #4380. --- crates/environ/src/component/translate.rs | 12 +++- crates/environ/src/component/types.rs | 62 ++++++++++++------- .../misc_testsuite/component-model/types.wast | 15 +++++ 3 files changed, 62 insertions(+), 27 deletions(-) diff --git a/crates/environ/src/component/translate.rs b/crates/environ/src/component/translate.rs index 312eda2c60..51459d70b5 100644 --- a/crates/environ/src/component/translate.rs +++ b/crates/environ/src/component/translate.rs @@ -583,9 +583,15 @@ impl<'a, 'data> Translator<'a, 'data> { let instance = ModuleInstanceIndex::from_u32(instance_index); self.alias_module_instance_export(kind, instance, name) } - wasmparser::Alias::Outer { kind, count, index } => { - drop((kind, count, index)); - unimplemented!("outer core index"); + wasmparser::Alias::Outer { + kind: wasmparser::OuterAliasKind::Type, + 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); diff --git a/crates/environ/src/component/types.rs b/crates/environ/src/component/types.rs index f419375d2e..bdb3438def 100644 --- a/crates/environ/src/component/types.rs +++ b/crates/environ/src/component/types.rs @@ -408,51 +408,65 @@ impl ComponentTypesBuilder { ty: &[wasmparser::ModuleTypeDeclaration<'_>], ) -> Result { let mut result = TypeModule::default(); - let mut functypes: PrimaryMap = PrimaryMap::default(); + self.push_type_scope(); for item in ty { match item { 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 } => { let prev = result .exports - .insert(name.to_string(), type_ref(ty, &functypes)?); + .insert(name.to_string(), self.entity_type(ty)?); assert!(prev.is_none()); } wasmparser::ModuleTypeDeclaration::Import(import) => { let prev = result.imports.insert( (import.module.to_string(), import.name.to_string()), - type_ref(&import.ty, &functypes)?, + self.entity_type(&import.ty)?, ); assert!(prev.is_none()); } - wasmparser::ModuleTypeDeclaration::Alias(alias) => { - drop(alias); - unimplemented!("outer alias in module type"); - } + wasmparser::ModuleTypeDeclaration::Alias(alias) => match alias { + wasmparser::Alias::Outer { + 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( - ty: &wasmparser::TypeRef, - functypes: &PrimaryMap, - ) -> Result { - Ok(match ty { - wasmparser::TypeRef::Func(idx) => { - EntityType::Function(functypes[TypeIndex::from_u32(*idx)]) + Ok(self.component_types.modules.push(result)) + } + + fn entity_type(&self, ty: &wasmparser::TypeRef) -> Result { + Ok(match ty { + wasmparser::TypeRef::Func(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::Memory(ty) => EntityType::Memory(ty.clone().into()), - wasmparser::TypeRef::Global(ty) => { - EntityType::Global(Global::new(ty.clone(), GlobalInit::Import)?) - } - wasmparser::TypeRef::Tag(_) => bail!("exceptions proposal not implemented"), - }) - } + } + wasmparser::TypeRef::Table(ty) => EntityType::Table(ty.clone().try_into()?), + wasmparser::TypeRef::Memory(ty) => EntityType::Memory(ty.clone().into()), + wasmparser::TypeRef::Global(ty) => { + EntityType::Global(Global::new(ty.clone(), GlobalInit::Import)?) + } + wasmparser::TypeRef::Tag(_) => bail!("exceptions proposal not implemented"), + }) } fn component_type( diff --git a/tests/misc_testsuite/component-model/types.wast b/tests/misc_testsuite/component-model/types.wast index 5d5d4beccd..358ef35571 100644 --- a/tests/misc_testsuite/component-model/types.wast +++ b/tests/misc_testsuite/component-model/types.wast @@ -86,3 +86,18 @@ (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))) + )) + ) +)