From ff0c45b4a069a84b131c8265d8d14c2f7d0566d7 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 13 Oct 2022 12:11:34 -0500 Subject: [PATCH] Minor changes for components related to wit-bindgen support (#5053) * Plumb type exports in components around more This commit adds some more plumbing for type exports to ensure that they show up in the final compiled representation of a component. For now they continued to be ignored for all purposes in the embedding API itself but I found this useful to explore in `wit-bindgen` based tooling which is leveraging the component parsing in Wasmtime. * Add a field to `ModuleTranslation` to store the original wasm This commit adds a field to be able to refer back to the original wasm binary for a `ModuleTranslation`. This field is used in the upcoming support for host generation in `wit-component` to "decompile" a component into core wasm modules to get instantiated. This is used to extract a core wasm module from the original component. * FIx a build warning --- crates/environ/src/component/dfg.rs | 2 ++ crates/environ/src/component/info.rs | 3 ++ .../environ/src/component/translate/inline.rs | 29 ++++++++++--------- crates/environ/src/component/types.rs | 2 +- crates/environ/src/module_environ.rs | 9 ++++++ crates/wasmtime/src/component/instance.rs | 2 +- 6 files changed, 32 insertions(+), 15 deletions(-) diff --git a/crates/environ/src/component/dfg.rs b/crates/environ/src/component/dfg.rs index ae986b55a2cd..bd7ee965851a 100644 --- a/crates/environ/src/component/dfg.rs +++ b/crates/environ/src/component/dfg.rs @@ -152,6 +152,7 @@ pub enum Export { ModuleStatic(StaticModuleIndex), ModuleImport(RuntimeImportIndex), Instance(IndexMap), + Type(TypeDef), } /// Same as `info::CoreDef`, except has an extra `Adapter` variant. @@ -430,6 +431,7 @@ impl LinearizeDfg<'_> { .map(|(name, export)| (name.clone(), self.export(export))) .collect(), ), + Export::Type(def) => info::Export::Type(*def), } } diff --git a/crates/environ/src/component/info.rs b/crates/environ/src/component/info.rs index d0c32bd0f1a8..6fc18afccd61 100644 --- a/crates/environ/src/component/info.rs +++ b/crates/environ/src/component/info.rs @@ -411,6 +411,9 @@ pub enum Export { /// A nested instance is being exported which has recursively defined /// `Export` items. Instance(IndexMap), + /// An exported type from a component or instance, currently only + /// informational. + Type(TypeDef), } /// Canonical ABI options associated with a lifted or lowered function. diff --git a/crates/environ/src/component/translate/inline.rs b/crates/environ/src/component/translate/inline.rs index bfb5590f3247..90938844b07f 100644 --- a/crates/environ/src/component/translate/inline.rs +++ b/crates/environ/src/component/translate/inline.rs @@ -224,10 +224,7 @@ enum ComponentItemDef<'a> { Instance(ComponentInstanceDef<'a>), Func(ComponentFuncDef<'a>), Module(ModuleDef<'a>), - // TODO: https://github.com/bytecodealliance/wasmtime/issues/4494 - // The entity is a type; currently unsupported but represented here - // so that type exports can be ignored for now. - Type, + Type(TypeDef), } #[derive(Clone)] @@ -381,7 +378,12 @@ impl<'a> Inliner<'a> { ComponentItemDef::Func(i) => { frame.component_funcs.push(i.clone()); } - ComponentItemDef::Type => {} + + // The type structure of a component does not change depending + // on how it is instantiated at this time so importing a type + // does not affect the component being instantiated so it's + // ignored. + ComponentItemDef::Type(_ty) => {} }, // Lowering a component function to a core wasm function is @@ -708,9 +710,13 @@ impl<'a> Inliner<'a> { let instance = i.clone(); frame.component_instances.push(instance); } - ComponentItemDef::Type => { - // Ignore type aliases for now - } + + // Like imports creation of types from an `alias`-ed + // export does not, at this time, modify what the type + // is or anything like that. The type structure of the + // component being instantiated is unchanged so types + // are ignored here. + ComponentItemDef::Type(_ty) => {} }, } } @@ -929,10 +935,7 @@ impl<'a> Inliner<'a> { bail!("exporting a component from the root component is not supported") } - ComponentItemDef::Type => { - // Ignore type exports for now - return Ok(()); - } + ComponentItemDef::Type(def) => dfg::Export::Type(def), }; map.insert(name.to_string(), export); @@ -979,7 +982,7 @@ impl<'a> InlinerFrame<'a> { ComponentItemDef::Instance(self.component_instances[i].clone()) } ComponentItem::Module(i) => ComponentItemDef::Module(self.modules[i].clone()), - ComponentItem::Type(_) => ComponentItemDef::Type, + ComponentItem::Type(t) => ComponentItemDef::Type(t), } } diff --git a/crates/environ/src/component/types.rs b/crates/environ/src/component/types.rs index 4c715b61efc2..ab29a25caf8b 100644 --- a/crates/environ/src/component/types.rs +++ b/crates/environ/src/component/types.rs @@ -469,7 +469,7 @@ impl ComponentTypesBuilder { self.core_outer_type(0, TypeIndex::from_u32(*ty)) } wasmparser::ComponentTypeRef::Func(ty) - | wasmparser::ComponentTypeRef::Type(_, ty) + | wasmparser::ComponentTypeRef::Type(wasmparser::TypeBounds::Eq, ty) | wasmparser::ComponentTypeRef::Instance(ty) | wasmparser::ComponentTypeRef::Component(ty) => { self.component_outer_type(0, ComponentTypeIndex::from_u32(*ty)) diff --git a/crates/environ/src/module_environ.rs b/crates/environ/src/module_environ.rs index c7af04e57e30..e8f1e8e296d6 100644 --- a/crates/environ/src/module_environ.rs +++ b/crates/environ/src/module_environ.rs @@ -40,6 +40,13 @@ pub struct ModuleTranslation<'data> { /// Module information. pub module: Module, + /// The input wasm binary. + /// + /// This can be useful, for example, when modules are parsed from a + /// component and the embedder wants access to the raw wasm modules + /// themselves. + pub wasm: &'data [u8], + /// References to the function bodies. pub function_body_inputs: PrimaryMap>, @@ -162,6 +169,8 @@ impl<'a, 'data> ModuleEnvironment<'a, 'data> { parser: Parser, data: &'data [u8], ) -> WasmResult> { + self.result.wasm = data; + for payload in parser.parse_all(data) { self.translate_payload(payload?)?; } diff --git a/crates/wasmtime/src/component/instance.rs b/crates/wasmtime/src/component/instance.rs index 0fcb62fbe619..39c566dc5d40 100644 --- a/crates/wasmtime/src/component/instance.rs +++ b/crates/wasmtime/src/component/instance.rs @@ -578,7 +578,7 @@ impl<'a, 'store> ExportInstance<'a, 'store> { func, options, )), - Export::Module(_) | Export::Instance(_) => None, + Export::Module(_) | Export::Instance(_) | Export::Type(_) => None, } }