wasmtime_environ/component/translate/
inline.rs

1//! Implementation of "inlining" a component into a flat list of initializers.
2//!
3//! After the first phase of compiling a component we're left with a single
4//! root `Translation` for the original component along with a "static" list of
5//! child components. Each `Translation` has a list of `LocalInitializer` items
6//! inside of it which is a primitive representation of how the component
7//! should be constructed with effectively one initializer per item in the
8//! index space of a component. This "local initializer" list would be
9//! relatively inefficient to process at runtime and more importantly doesn't
10//! convey enough information to understand what trampolines need to be
11//! compiled or what fused adapters need to be generated. This consequently is
12//! the motivation for this file.
13//!
14//! The second phase of compilation, inlining here, will in a sense interpret
15//! the initializers, at compile time, into a new list of `GlobalInitializer` entries
16//! which are a sort of "global initializer". The generated `GlobalInitializer` is
17//! much more specific than the `LocalInitializer` and additionally far fewer
18//! `GlobalInitializer` structures are generated (in theory) than there are local
19//! initializers.
20//!
21//! The "inlining" portion of the name of this module indicates how the
22//! instantiation of a component is interpreted as calling a function. The
23//! function's arguments are the imports provided to the instantiation of a
24//! component, and further nested function calls happen on a stack when a
25//! nested component is instantiated. The inlining then refers to how this
26//! stack of instantiations is flattened to one list of `GlobalInitializer`
27//! entries to represent the process of instantiating a component graph,
28//! similar to how function inlining removes call instructions and creates one
29//! giant function for a call graph. Here there are no inlining heuristics or
30//! anything like that, we simply inline everything into the root component's
31//! list of initializers.
32//!
33//! Another primary task this module performs is a form of dataflow analysis
34//! to represent items in each index space with their definition rather than
35//! references of relative indices. These definitions (all the `*Def` types in
36//! this module) are not local to any one nested component and instead
37//! represent state available at runtime tracked in the final `Component`
38//! produced.
39//!
40//! With all this pieced together the general idea is relatively
41//! straightforward. All of a component's initializers are processed in sequence
42//! where instantiating a nested component pushes a "frame" onto a stack to
43//! start executing and we resume at the old one when we're done. Items are
44//! tracked where they come from and at the end after processing only the
45//! side-effectful initializers are emitted to the `GlobalInitializer` list in the
46//! final `Component`.
47
48use crate::component::translate::*;
49use crate::{EntityType, IndexType};
50use core::str::FromStr;
51use std::borrow::Cow;
52use wasmparser::component_types::{ComponentAnyTypeId, ComponentCoreModuleTypeId};
53
54pub(super) fn run(
55    types: &mut ComponentTypesBuilder,
56    result: &Translation<'_>,
57    nested_modules: &PrimaryMap<StaticModuleIndex, ModuleTranslation<'_>>,
58    nested_components: &PrimaryMap<StaticComponentIndex, Translation<'_>>,
59) -> Result<dfg::ComponentDfg> {
60    let mut inliner = Inliner {
61        nested_modules,
62        nested_components,
63        result: Default::default(),
64        import_path_interner: Default::default(),
65        runtime_instances: PrimaryMap::default(),
66    };
67
68    let index = RuntimeComponentInstanceIndex::from_u32(0);
69
70    // The initial arguments to the root component are all host imports. This
71    // means that they're all using the `ComponentItemDef::Host` variant. Here
72    // an `ImportIndex` is allocated for each item and then the argument is
73    // recorded.
74    //
75    // Note that this is represents the abstract state of a host import of an
76    // item since we don't know the precise structure of the host import.
77    let mut args = HashMap::with_capacity(result.exports.len());
78    let mut path = Vec::new();
79    types.resources_mut().set_current_instance(index);
80    let types_ref = result.types_ref();
81    for init in result.initializers.iter() {
82        let (name, ty) = match *init {
83            LocalInitializer::Import(name, ty) => (name, ty),
84            _ => continue,
85        };
86
87        // Before `convert_component_entity_type` below all resource types
88        // introduced by this import need to be registered and have indexes
89        // assigned to them. Any fresh new resource type referred to by imports
90        // is a brand new introduction of a resource which needs to have a type
91        // allocated to it, so new runtime imports are injected for each
92        // resource along with updating the `imported_resources` map.
93        let index = inliner.result.import_types.next_key();
94        types.resources_mut().register_component_entity_type(
95            &types_ref,
96            ty,
97            &mut path,
98            &mut |path| {
99                let index = inliner.runtime_import(&ImportPath {
100                    index,
101                    path: path.iter().copied().map(Into::into).collect(),
102                });
103                inliner.result.imported_resources.push(index)
104            },
105        );
106
107        // With resources all taken care of it's now possible to convert this
108        // into Wasmtime's type system.
109        let ty = types.convert_component_entity_type(types_ref, ty)?;
110
111        // Imports of types that aren't resources are not required to be
112        // specified by the host since it's just for type information within
113        // the component.
114        if let TypeDef::Interface(_) = ty {
115            continue;
116        }
117        let index = inliner.result.import_types.push((name.0.to_string(), ty));
118        let path = ImportPath::root(index);
119        args.insert(name.0, ComponentItemDef::from_import(path, ty)?);
120    }
121
122    // This will run the inliner to completion after being seeded with the
123    // initial frame. When the inliner finishes it will return the exports of
124    // the root frame which are then used for recording the exports of the
125    // component.
126    inliner.result.num_runtime_component_instances += 1;
127    let frame = InlinerFrame::new(index, result, ComponentClosure::default(), args, None);
128    let resources_snapshot = types.resources_mut().clone();
129    let mut frames = vec![(frame, resources_snapshot)];
130    let exports = inliner.run(types, &mut frames)?;
131    assert!(frames.is_empty());
132
133    let mut export_map = Default::default();
134    for (name, def) in exports {
135        inliner.record_export(name, def, types, &mut export_map)?;
136    }
137    inliner.result.exports = export_map;
138    inliner.result.num_future_tables = types.num_future_tables();
139    inliner.result.num_stream_tables = types.num_stream_tables();
140    inliner.result.num_error_context_tables = types.num_error_context_tables();
141
142    Ok(inliner.result)
143}
144
145struct Inliner<'a> {
146    /// The list of static modules that were found during initial translation of
147    /// the component.
148    ///
149    /// This is used during the instantiation of these modules to ahead-of-time
150    /// order the arguments precisely according to what the module is defined as
151    /// needing which avoids the need to do string lookups or permute arguments
152    /// at runtime.
153    nested_modules: &'a PrimaryMap<StaticModuleIndex, ModuleTranslation<'a>>,
154
155    /// The list of static components that were found during initial translation of
156    /// the component.
157    ///
158    /// This is used when instantiating nested components to push a new
159    /// `InlinerFrame` with the `Translation`s here.
160    nested_components: &'a PrimaryMap<StaticComponentIndex, Translation<'a>>,
161
162    /// The final `Component` that is being constructed and returned from this
163    /// inliner.
164    result: dfg::ComponentDfg,
165
166    // Maps used to "intern" various runtime items to only save them once at
167    // runtime instead of multiple times.
168    import_path_interner: HashMap<ImportPath<'a>, RuntimeImportIndex>,
169
170    /// Origin information about where each runtime instance came from
171    runtime_instances: PrimaryMap<dfg::InstanceId, InstanceModule>,
172}
173
174/// A "stack frame" as part of the inlining process, or the progress through
175/// instantiating a component.
176///
177/// All instantiations of a component will create an `InlinerFrame` and are
178/// incrementally processed via the `initializers` list here. Note that the
179/// inliner frames are stored on the heap to avoid recursion based on user
180/// input.
181struct InlinerFrame<'a> {
182    instance: RuntimeComponentInstanceIndex,
183
184    /// The remaining initializers to process when instantiating this component.
185    initializers: std::slice::Iter<'a, LocalInitializer<'a>>,
186
187    /// The component being instantiated.
188    translation: &'a Translation<'a>,
189
190    /// The "closure arguments" to this component, or otherwise the maps indexed
191    /// by `ModuleUpvarIndex` and `ComponentUpvarIndex`. This is created when
192    /// a component is created and stored as part of a component's state during
193    /// inlining.
194    closure: ComponentClosure<'a>,
195
196    /// The arguments to the creation of this component.
197    ///
198    /// At the root level these are all imports from the host and between
199    /// components this otherwise tracks how all the arguments are defined.
200    args: HashMap<&'a str, ComponentItemDef<'a>>,
201
202    // core wasm index spaces
203    funcs: PrimaryMap<FuncIndex, (ModuleInternedTypeIndex, dfg::CoreDef)>,
204    memories: PrimaryMap<MemoryIndex, dfg::CoreExport<EntityIndex>>,
205    tables: PrimaryMap<TableIndex, dfg::CoreExport<EntityIndex>>,
206    globals: PrimaryMap<GlobalIndex, dfg::CoreExport<EntityIndex>>,
207    tags: PrimaryMap<TagIndex, dfg::CoreExport<EntityIndex>>,
208    modules: PrimaryMap<ModuleIndex, ModuleDef<'a>>,
209
210    // component model index spaces
211    component_funcs: PrimaryMap<ComponentFuncIndex, ComponentFuncDef<'a>>,
212    module_instances: PrimaryMap<ModuleInstanceIndex, ModuleInstanceDef<'a>>,
213    component_instances: PrimaryMap<ComponentInstanceIndex, ComponentInstanceDef<'a>>,
214    components: PrimaryMap<ComponentIndex, ComponentDef<'a>>,
215
216    /// The type of instance produced by completing the instantiation of this
217    /// frame.
218    ///
219    /// This is a wasmparser-relative piece of type information which is used to
220    /// register resource types after instantiation has completed.
221    ///
222    /// This is `Some` for all subcomponents and `None` for the root component.
223    instance_ty: Option<ComponentInstanceTypeId>,
224}
225
226/// "Closure state" for a component which is resolved from the `ClosedOverVars`
227/// state that was calculated during translation.
228//
229// FIXME: this is cloned quite a lot and given the internal maps if this is a
230// perf issue we may want to `Rc` these fields. Note that this is only a perf
231// hit at compile-time though which we in general don't pay too much
232// attention to.
233#[derive(Default, Clone)]
234struct ComponentClosure<'a> {
235    modules: PrimaryMap<ModuleUpvarIndex, ModuleDef<'a>>,
236    components: PrimaryMap<ComponentUpvarIndex, ComponentDef<'a>>,
237}
238
239/// Representation of a "path" into an import.
240///
241/// Imports from the host at this time are one of three things:
242///
243/// * Functions
244/// * Core wasm modules
245/// * "Instances" of these three items
246///
247/// The "base" values are functions and core wasm modules, but the abstraction
248/// of an instance allows embedding functions/modules deeply within other
249/// instances. This "path" represents optionally walking through a host instance
250/// to get to the final desired item. At runtime instances are just maps of
251/// values and so this is used to ensure that we primarily only deal with
252/// individual functions and modules instead of synthetic instances.
253#[derive(Clone, PartialEq, Hash, Eq)]
254struct ImportPath<'a> {
255    index: ImportIndex,
256    path: Vec<Cow<'a, str>>,
257}
258
259/// Representation of all items which can be defined within a component.
260///
261/// This is the "value" of an item defined within a component and is used to
262/// represent both imports and exports.
263#[derive(Clone)]
264enum ComponentItemDef<'a> {
265    Component(ComponentDef<'a>),
266    Instance(ComponentInstanceDef<'a>),
267    Func(ComponentFuncDef<'a>),
268    Module(ModuleDef<'a>),
269    Type(TypeDef),
270}
271
272#[derive(Clone)]
273enum ModuleDef<'a> {
274    /// A core wasm module statically defined within the original component.
275    ///
276    /// The `StaticModuleIndex` indexes into the `static_modules` map in the
277    /// `Inliner`.
278    Static(StaticModuleIndex, ComponentCoreModuleTypeId),
279
280    /// A core wasm module that was imported from the host.
281    Import(ImportPath<'a>, TypeModuleIndex),
282}
283
284// Note that unlike all other `*Def` types which are not allowed to have local
285// indices this type does indeed have local indices. That is represented with
286// the lack of a `Clone` here where once this is created it's never moved across
287// components because module instances always stick within one component.
288enum ModuleInstanceDef<'a> {
289    /// A core wasm module instance was created through the instantiation of a
290    /// module.
291    ///
292    /// The `RuntimeInstanceIndex` was the index allocated as this was the
293    /// `n`th instantiation and the `ModuleIndex` points into an
294    /// `InlinerFrame`'s local index space.
295    Instantiated(dfg::InstanceId, ModuleIndex),
296
297    /// A "synthetic" core wasm module which is just a bag of named indices.
298    ///
299    /// Note that this can really only be used for passing as an argument to
300    /// another module's instantiation and is used to rename arguments locally.
301    Synthetic(&'a HashMap<&'a str, EntityIndex>),
302}
303
304#[derive(Clone)]
305enum ComponentFuncDef<'a> {
306    /// A compile-time builtin intrinsic.
307    UnsafeIntrinsic(UnsafeIntrinsic),
308
309    /// A host-imported component function.
310    Import(ImportPath<'a>),
311
312    /// A core wasm function was lifted into a component function.
313    Lifted {
314        /// The component function type.
315        ty: TypeFuncIndex,
316        /// The core Wasm function.
317        func: dfg::CoreDef,
318        /// Canonical options.
319        options: AdapterOptions,
320    },
321}
322
323#[derive(Clone)]
324enum ComponentInstanceDef<'a> {
325    /// The `__wasmtime_intrinsics` instance that exports all of our
326    /// compile-time builtin intrinsics.
327    Intrinsics,
328
329    /// A host-imported instance.
330    ///
331    /// This typically means that it's "just" a map of named values. It's not
332    /// actually supported to take a `wasmtime::component::Instance` and pass it
333    /// to another instance at this time.
334    Import(ImportPath<'a>, TypeComponentInstanceIndex),
335
336    /// A concrete map of values.
337    ///
338    /// This is used for both instantiated components as well as "synthetic"
339    /// components. This variant can be used for both because both are
340    /// represented by simply a bag of items within the entire component
341    /// instantiation process.
342    //
343    // FIXME: same as the issue on `ComponentClosure` where this is cloned a lot
344    // and may need `Rc`.
345    Items(
346        IndexMap<&'a str, ComponentItemDef<'a>>,
347        TypeComponentInstanceIndex,
348    ),
349}
350
351#[derive(Clone)]
352struct ComponentDef<'a> {
353    index: StaticComponentIndex,
354    closure: ComponentClosure<'a>,
355}
356
357impl<'a> Inliner<'a> {
358    /// Symbolically instantiates a component using the type information and
359    /// `frames` provided.
360    ///
361    /// The `types` provided is the type information for the entire component
362    /// translation process. This is a distinct output artifact separate from
363    /// the component metadata.
364    ///
365    /// The `frames` argument is storage to handle a "call stack" of components
366    /// instantiating one another. The youngest frame (last element) of the
367    /// frames list is a component that's currently having its initializers
368    /// processed. The second element of each frame is a snapshot of the
369    /// resource-related information just before the frame was translated. For
370    /// more information on this snapshotting see the documentation on
371    /// `ResourcesBuilder`.
372    fn run(
373        &mut self,
374        types: &mut ComponentTypesBuilder,
375        frames: &mut Vec<(InlinerFrame<'a>, ResourcesBuilder)>,
376    ) -> Result<IndexMap<&'a str, ComponentItemDef<'a>>> {
377        // This loop represents the execution of the instantiation of a
378        // component. This is an iterative process which is finished once all
379        // initializers are processed. Currently this is modeled as an infinite
380        // loop which drives the top-most iterator of the `frames` stack
381        // provided as an argument to this function.
382        loop {
383            let (frame, _) = frames.last_mut().unwrap();
384            types.resources_mut().set_current_instance(frame.instance);
385            match frame.initializers.next() {
386                // Process the initializer and if it started the instantiation
387                // of another component then we push that frame on the stack to
388                // continue onwards.
389                Some(init) => match self.initializer(frame, types, init)? {
390                    Some(new_frame) => {
391                        frames.push((new_frame, types.resources_mut().clone()));
392                    }
393                    None => {}
394                },
395
396                // If there are no more initializers for this frame then the
397                // component it represents has finished instantiation. The
398                // exports of the component are collected and then the entire
399                // frame is discarded. The exports are then either pushed in the
400                // parent frame, if any, as a new component instance or they're
401                // returned from this function for the root set of exports.
402                None => {
403                    let exports = frame
404                        .translation
405                        .exports
406                        .iter()
407                        .map(|(name, item)| Ok((*name, frame.item(*item, types)?)))
408                        .collect::<Result<_>>()?;
409                    let instance_ty = frame.instance_ty;
410                    let (_, snapshot) = frames.pop().unwrap();
411                    *types.resources_mut() = snapshot;
412                    match frames.last_mut() {
413                        Some((parent, _)) => {
414                            parent.finish_instantiate(exports, instance_ty.unwrap(), types)?;
415                        }
416                        None => break Ok(exports),
417                    }
418                }
419            }
420        }
421    }
422
423    fn initializer(
424        &mut self,
425        frame: &mut InlinerFrame<'a>,
426        types: &mut ComponentTypesBuilder,
427        initializer: &'a LocalInitializer,
428    ) -> Result<Option<InlinerFrame<'a>>> {
429        use LocalInitializer::*;
430
431        match initializer {
432            // When a component imports an item the actual definition of the
433            // item is looked up here (not at runtime) via its name. The
434            // arguments provided in our `InlinerFrame` describe how each
435            // argument was defined, so we simply move it from there into the
436            // correct index space.
437            //
438            // Note that for the root component this will add `*::Import` items
439            // but for sub-components this will do resolution to connect what
440            // was provided as an import at the instantiation-site to what was
441            // needed during the component's instantiation.
442            Import(name, ty) => {
443                let arg = match frame.args.get(name.0) {
444                    Some(arg) => arg,
445
446                    // Not all arguments need to be provided for instantiation,
447                    // namely the root component in Wasmtime doesn't require
448                    // structural type imports to be satisfied. These type
449                    // imports are relevant for bindings generators and such but
450                    // as a runtime there's not really a definition to fit in.
451                    //
452                    // If no argument was provided for `name` then it's asserted
453                    // that this is a type import and additionally it's not a
454                    // resource type import (which indeed must be provided). If
455                    // all that passes then this initializer is effectively
456                    // skipped.
457                    None => {
458                        match ty {
459                            ComponentEntityType::Type {
460                                created: ComponentAnyTypeId::Resource(_),
461                                ..
462                            } => unreachable!(),
463                            ComponentEntityType::Type { .. } => {}
464                            _ => unreachable!(),
465                        }
466                        return Ok(None);
467                    }
468                };
469
470                // Next resource types need to be handled. For example if a
471                // resource is imported into this component then it needs to be
472                // assigned a unique table to provide the isolation guarantees
473                // of resources (this component's table is shared with no
474                // others). Here `register_component_entity_type` will find
475                // imported resources and then `lookup_resource` will find the
476                // resource within `arg` as necessary to lookup the original
477                // true definition of this resource.
478                //
479                // This is what enables tracking true resource origins
480                // throughout component translation while simultaneously also
481                // tracking unique tables for each resource in each component.
482                let mut path = Vec::new();
483                let (resources, types) = types.resources_mut_and_types();
484                resources.register_component_entity_type(
485                    &frame.translation.types_ref(),
486                    *ty,
487                    &mut path,
488                    &mut |path| arg.lookup_resource(path, types),
489                );
490
491                // And now with all the type information out of the way the
492                // `arg` definition is moved into its corresponding index space.
493                frame.push_item(arg.clone());
494            }
495
496            IntrinsicsImport => {
497                frame
498                    .component_instances
499                    .push(ComponentInstanceDef::Intrinsics);
500            }
501
502            // Lowering a component function to a core wasm function is
503            // generally what "triggers compilation". Here various metadata is
504            // recorded and then the final component gets an initializer
505            // recording the lowering.
506            //
507            // NB: at this time only lowered imported functions are supported.
508            Lower {
509                func,
510                options,
511                lower_ty,
512            } => {
513                let lower_ty =
514                    types.convert_component_func_type(frame.translation.types_ref(), *lower_ty)?;
515                let options_lower = self.adapter_options(frame, types, options);
516                let lower_core_type = options_lower.core_type;
517                let func = match &frame.component_funcs[*func] {
518                    // If this component function was originally a host import
519                    // then this is a lowered host function which needs a
520                    // trampoline to enter WebAssembly. That's recorded here
521                    // with all relevant information.
522                    ComponentFuncDef::Import(path) => {
523                        let import = self.runtime_import(path);
524                        let options = self.canonical_options(options_lower);
525                        let index = self.result.trampolines.push((
526                            lower_core_type,
527                            dfg::Trampoline::LowerImport {
528                                import,
529                                options,
530                                lower_ty,
531                            },
532                        ));
533                        dfg::CoreDef::Trampoline(index)
534                    }
535
536                    // This case handles when a lifted function is later
537                    // lowered, and both the lowering and the lifting are
538                    // happening within the same component instance.
539                    //
540                    // In this situation if the `canon.lower`'d function is
541                    // called then it immediately sets `may_enter` to `false`.
542                    // When calling the callee, however, that's `canon.lift`
543                    // which immediately traps if `may_enter` is `false`. That
544                    // means that this pairing of functions creates a function
545                    // that always traps.
546                    //
547                    // When closely reading the spec though the precise trap
548                    // that comes out can be somewhat variable. Technically the
549                    // function yielded here is one that should validate the
550                    // arguments by lifting them, and then trap. This means that
551                    // the trap could be different depending on whether all
552                    // arguments are valid for now. This was discussed in
553                    // WebAssembly/component-model#51 somewhat and the
554                    // conclusion was that we can probably get away with "always
555                    // trap" here.
556                    //
557                    // The `CoreDef::AlwaysTrap` variant here is used to
558                    // indicate that this function is valid but if something
559                    // actually calls it then it just generates a trap
560                    // immediately.
561                    ComponentFuncDef::Lifted {
562                        options: options_lift,
563                        ..
564                    } if options_lift.instance == options_lower.instance => {
565                        let index = self
566                            .result
567                            .trampolines
568                            .push((lower_core_type, dfg::Trampoline::AlwaysTrap));
569                        dfg::CoreDef::Trampoline(index)
570                    }
571
572                    // Lowering a lifted function where the destination
573                    // component is different than the source component means
574                    // that a "fused adapter" was just identified.
575                    //
576                    // Metadata about this fused adapter is recorded in the
577                    // `Adapters` output of this compilation pass. Currently the
578                    // implementation of fused adapters is to generate a core
579                    // wasm module which is instantiated with relevant imports
580                    // and the exports are used as the fused adapters. At this
581                    // time we don't know when precisely the instance will be
582                    // created but we do know that the result of this will be an
583                    // export from a previously-created instance.
584                    //
585                    // To model this the result of this arm is a
586                    // `CoreDef::Export`. The actual indices listed within the
587                    // export are "fake indices" in the sense of they're not
588                    // resolved yet. This resolution will happen at a later
589                    // compilation phase. Any usages of the `CoreDef::Export`
590                    // here will be detected and rewritten to an actual runtime
591                    // instance created.
592                    //
593                    // The `instance` field of the `CoreExport` has a marker
594                    // which indicates that it's a fused adapter. The `item` is
595                    // a function where the function index corresponds to the
596                    // `adapter_idx` which contains the metadata about this
597                    // adapter being created. The metadata is used to learn
598                    // about the dependencies and when the adapter module can
599                    // be instantiated.
600                    ComponentFuncDef::Lifted {
601                        ty: lift_ty,
602                        func,
603                        options: options_lift,
604                    } => {
605                        let adapter_idx = self.result.adapters.push(Adapter {
606                            lift_ty: *lift_ty,
607                            lift_options: options_lift.clone(),
608                            lower_ty,
609                            lower_options: options_lower,
610                            func: func.clone(),
611                        });
612                        dfg::CoreDef::Adapter(adapter_idx)
613                    }
614
615                    ComponentFuncDef::UnsafeIntrinsic(intrinsic) => {
616                        dfg::CoreDef::UnsafeIntrinsic(options.core_type, *intrinsic)
617                    }
618                };
619                frame.funcs.push((lower_core_type, func));
620            }
621
622            // Lifting a core wasm function is relatively easy for now in that
623            // some metadata about the lifting is simply recorded. This'll get
624            // plumbed through to exports or a fused adapter later on.
625            Lift(ty, func, options) => {
626                let ty = types.convert_component_func_type(frame.translation.types_ref(), *ty)?;
627                let options = self.adapter_options(frame, types, options);
628                let func = frame.funcs[*func].1.clone();
629                frame
630                    .component_funcs
631                    .push(ComponentFuncDef::Lifted { ty, func, options });
632            }
633
634            // A new resource type is being introduced, so it's recorded as a
635            // brand new resource in the final `resources` array. Additionally
636            // for now resource introductions are considered side effects to
637            // know when to register their destructors so that's recorded as
638            // well.
639            //
640            // Note that this has the effect of when a component is instantiated
641            // twice it will produce unique types for the resources from each
642            // instantiation. That's the intended runtime semantics and
643            // implementation here, however.
644            Resource(ty, rep, dtor) => {
645                let idx = self.result.resources.push(dfg::Resource {
646                    rep: *rep,
647                    dtor: dtor.map(|i| frame.funcs[i].1.clone()),
648                    instance: frame.instance,
649                });
650                self.result
651                    .side_effects
652                    .push(dfg::SideEffect::Resource(idx));
653
654                // Register with type translation that all future references to
655                // `ty` will refer to `idx`.
656                //
657                // Note that this registration information is lost when this
658                // component finishes instantiation due to the snapshotting
659                // behavior in the frame processing loop above. This is also
660                // intended, though, since `ty` can't be referred to outside of
661                // this component.
662                let idx = self.result.resource_index(idx);
663                types.resources_mut().register_resource(ty.resource(), idx);
664            }
665
666            // Resource-related intrinsics are generally all the same.
667            // Wasmparser type information is converted to wasmtime type
668            // information and then new entries for each intrinsic are recorded.
669            ResourceNew(id, ty) => {
670                let id = types.resource_id(id.resource());
671                let index = self.result.trampolines.push((
672                    *ty,
673                    dfg::Trampoline::ResourceNew {
674                        instance: frame.instance,
675                        ty: id,
676                    },
677                ));
678                frame.funcs.push((*ty, dfg::CoreDef::Trampoline(index)));
679            }
680            ResourceRep(id, ty) => {
681                let id = types.resource_id(id.resource());
682                let index = self.result.trampolines.push((
683                    *ty,
684                    dfg::Trampoline::ResourceRep {
685                        instance: frame.instance,
686                        ty: id,
687                    },
688                ));
689                frame.funcs.push((*ty, dfg::CoreDef::Trampoline(index)));
690            }
691            ResourceDrop(id, ty) => {
692                let id = types.resource_id(id.resource());
693                let index = self.result.trampolines.push((
694                    *ty,
695                    dfg::Trampoline::ResourceDrop {
696                        instance: frame.instance,
697                        ty: id,
698                    },
699                ));
700                frame.funcs.push((*ty, dfg::CoreDef::Trampoline(index)));
701            }
702            BackpressureInc { func } => {
703                let index = self.result.trampolines.push((
704                    *func,
705                    dfg::Trampoline::BackpressureInc {
706                        instance: frame.instance,
707                    },
708                ));
709                frame.funcs.push((*func, dfg::CoreDef::Trampoline(index)));
710            }
711            BackpressureDec { func } => {
712                let index = self.result.trampolines.push((
713                    *func,
714                    dfg::Trampoline::BackpressureDec {
715                        instance: frame.instance,
716                    },
717                ));
718                frame.funcs.push((*func, dfg::CoreDef::Trampoline(index)));
719            }
720            TaskReturn { result, options } => {
721                let results = result
722                    .iter()
723                    .map(|ty| types.valtype(frame.translation.types_ref(), ty))
724                    .collect::<Result<_>>()?;
725                let results = types.new_tuple_type(results);
726                let func = options.core_type;
727                let options = self.adapter_options(frame, types, options);
728                let options = self.canonical_options(options);
729                let index = self.result.trampolines.push((
730                    func,
731                    dfg::Trampoline::TaskReturn {
732                        instance: frame.instance,
733                        results,
734                        options,
735                    },
736                ));
737                frame.funcs.push((func, dfg::CoreDef::Trampoline(index)));
738            }
739            TaskCancel { func } => {
740                let index = self.result.trampolines.push((
741                    *func,
742                    dfg::Trampoline::TaskCancel {
743                        instance: frame.instance,
744                    },
745                ));
746                frame.funcs.push((*func, dfg::CoreDef::Trampoline(index)));
747            }
748            WaitableSetNew { func } => {
749                let index = self.result.trampolines.push((
750                    *func,
751                    dfg::Trampoline::WaitableSetNew {
752                        instance: frame.instance,
753                    },
754                ));
755                frame.funcs.push((*func, dfg::CoreDef::Trampoline(index)));
756            }
757            WaitableSetWait { options } => {
758                let func = options.core_type;
759                let options = self.adapter_options(frame, types, options);
760                let options = self.canonical_options(options);
761                let index = self.result.trampolines.push((
762                    func,
763                    dfg::Trampoline::WaitableSetWait {
764                        instance: frame.instance,
765                        options,
766                    },
767                ));
768                frame.funcs.push((func, dfg::CoreDef::Trampoline(index)));
769            }
770            WaitableSetPoll { options } => {
771                let func = options.core_type;
772                let options = self.adapter_options(frame, types, options);
773                let options = self.canonical_options(options);
774                let index = self.result.trampolines.push((
775                    func,
776                    dfg::Trampoline::WaitableSetPoll {
777                        instance: frame.instance,
778                        options,
779                    },
780                ));
781                frame.funcs.push((func, dfg::CoreDef::Trampoline(index)));
782            }
783            WaitableSetDrop { func } => {
784                let index = self.result.trampolines.push((
785                    *func,
786                    dfg::Trampoline::WaitableSetDrop {
787                        instance: frame.instance,
788                    },
789                ));
790                frame.funcs.push((*func, dfg::CoreDef::Trampoline(index)));
791            }
792            WaitableJoin { func } => {
793                let index = self.result.trampolines.push((
794                    *func,
795                    dfg::Trampoline::WaitableJoin {
796                        instance: frame.instance,
797                    },
798                ));
799                frame.funcs.push((*func, dfg::CoreDef::Trampoline(index)));
800            }
801            ThreadYield { func, cancellable } => {
802                let index = self.result.trampolines.push((
803                    *func,
804                    dfg::Trampoline::ThreadYield {
805                        instance: frame.instance,
806                        cancellable: *cancellable,
807                    },
808                ));
809                frame.funcs.push((*func, dfg::CoreDef::Trampoline(index)));
810            }
811            SubtaskDrop { func } => {
812                let index = self.result.trampolines.push((
813                    *func,
814                    dfg::Trampoline::SubtaskDrop {
815                        instance: frame.instance,
816                    },
817                ));
818                frame.funcs.push((*func, dfg::CoreDef::Trampoline(index)));
819            }
820            SubtaskCancel { func, async_ } => {
821                let index = self.result.trampolines.push((
822                    *func,
823                    dfg::Trampoline::SubtaskCancel {
824                        instance: frame.instance,
825                        async_: *async_,
826                    },
827                ));
828                frame.funcs.push((*func, dfg::CoreDef::Trampoline(index)));
829            }
830            StreamNew { ty, func } => {
831                let InterfaceType::Stream(ty) =
832                    types.defined_type(frame.translation.types_ref(), *ty)?
833                else {
834                    unreachable!()
835                };
836                let index = self.result.trampolines.push((
837                    *func,
838                    dfg::Trampoline::StreamNew {
839                        instance: frame.instance,
840                        ty,
841                    },
842                ));
843                frame.funcs.push((*func, dfg::CoreDef::Trampoline(index)));
844            }
845            StreamRead { ty, options } => {
846                let InterfaceType::Stream(ty) =
847                    types.defined_type(frame.translation.types_ref(), *ty)?
848                else {
849                    unreachable!()
850                };
851                let func = options.core_type;
852                let options = self.adapter_options(frame, types, options);
853                let options = self.canonical_options(options);
854                let index = self.result.trampolines.push((
855                    func,
856                    dfg::Trampoline::StreamRead {
857                        instance: frame.instance,
858                        ty,
859                        options,
860                    },
861                ));
862                frame.funcs.push((func, dfg::CoreDef::Trampoline(index)));
863            }
864            StreamWrite { ty, options } => {
865                let InterfaceType::Stream(ty) =
866                    types.defined_type(frame.translation.types_ref(), *ty)?
867                else {
868                    unreachable!()
869                };
870                let func = options.core_type;
871                let options = self.adapter_options(frame, types, options);
872                let options = self.canonical_options(options);
873                let index = self.result.trampolines.push((
874                    func,
875                    dfg::Trampoline::StreamWrite {
876                        instance: frame.instance,
877                        ty,
878                        options,
879                    },
880                ));
881                frame.funcs.push((func, dfg::CoreDef::Trampoline(index)));
882            }
883            StreamCancelRead { ty, func, async_ } => {
884                let InterfaceType::Stream(ty) =
885                    types.defined_type(frame.translation.types_ref(), *ty)?
886                else {
887                    unreachable!()
888                };
889                let index = self.result.trampolines.push((
890                    *func,
891                    dfg::Trampoline::StreamCancelRead {
892                        instance: frame.instance,
893                        ty,
894                        async_: *async_,
895                    },
896                ));
897                frame.funcs.push((*func, dfg::CoreDef::Trampoline(index)));
898            }
899            StreamCancelWrite { ty, func, async_ } => {
900                let InterfaceType::Stream(ty) =
901                    types.defined_type(frame.translation.types_ref(), *ty)?
902                else {
903                    unreachable!()
904                };
905                let index = self.result.trampolines.push((
906                    *func,
907                    dfg::Trampoline::StreamCancelWrite {
908                        instance: frame.instance,
909                        ty,
910                        async_: *async_,
911                    },
912                ));
913                frame.funcs.push((*func, dfg::CoreDef::Trampoline(index)));
914            }
915            StreamDropReadable { ty, func } => {
916                let InterfaceType::Stream(ty) =
917                    types.defined_type(frame.translation.types_ref(), *ty)?
918                else {
919                    unreachable!()
920                };
921                let index = self.result.trampolines.push((
922                    *func,
923                    dfg::Trampoline::StreamDropReadable {
924                        instance: frame.instance,
925                        ty,
926                    },
927                ));
928                frame.funcs.push((*func, dfg::CoreDef::Trampoline(index)));
929            }
930            StreamDropWritable { ty, func } => {
931                let InterfaceType::Stream(ty) =
932                    types.defined_type(frame.translation.types_ref(), *ty)?
933                else {
934                    unreachable!()
935                };
936                let index = self.result.trampolines.push((
937                    *func,
938                    dfg::Trampoline::StreamDropWritable {
939                        instance: frame.instance,
940                        ty,
941                    },
942                ));
943                frame.funcs.push((*func, dfg::CoreDef::Trampoline(index)));
944            }
945            FutureNew { ty, func } => {
946                let InterfaceType::Future(ty) =
947                    types.defined_type(frame.translation.types_ref(), *ty)?
948                else {
949                    unreachable!()
950                };
951                let index = self.result.trampolines.push((
952                    *func,
953                    dfg::Trampoline::FutureNew {
954                        instance: frame.instance,
955                        ty,
956                    },
957                ));
958                frame.funcs.push((*func, dfg::CoreDef::Trampoline(index)));
959            }
960            FutureRead { ty, options } => {
961                let InterfaceType::Future(ty) =
962                    types.defined_type(frame.translation.types_ref(), *ty)?
963                else {
964                    unreachable!()
965                };
966                let func = options.core_type;
967                let options = self.adapter_options(frame, types, options);
968                let options = self.canonical_options(options);
969                let index = self.result.trampolines.push((
970                    func,
971                    dfg::Trampoline::FutureRead {
972                        instance: frame.instance,
973                        ty,
974                        options,
975                    },
976                ));
977                frame.funcs.push((func, dfg::CoreDef::Trampoline(index)));
978            }
979            FutureWrite { ty, options } => {
980                let InterfaceType::Future(ty) =
981                    types.defined_type(frame.translation.types_ref(), *ty)?
982                else {
983                    unreachable!()
984                };
985                let func = options.core_type;
986                let options = self.adapter_options(frame, types, options);
987                let options = self.canonical_options(options);
988                let index = self.result.trampolines.push((
989                    func,
990                    dfg::Trampoline::FutureWrite {
991                        instance: frame.instance,
992                        ty,
993                        options,
994                    },
995                ));
996                frame.funcs.push((func, dfg::CoreDef::Trampoline(index)));
997            }
998            FutureCancelRead { ty, func, async_ } => {
999                let InterfaceType::Future(ty) =
1000                    types.defined_type(frame.translation.types_ref(), *ty)?
1001                else {
1002                    unreachable!()
1003                };
1004                let index = self.result.trampolines.push((
1005                    *func,
1006                    dfg::Trampoline::FutureCancelRead {
1007                        instance: frame.instance,
1008                        ty,
1009                        async_: *async_,
1010                    },
1011                ));
1012                frame.funcs.push((*func, dfg::CoreDef::Trampoline(index)));
1013            }
1014            FutureCancelWrite { ty, func, async_ } => {
1015                let InterfaceType::Future(ty) =
1016                    types.defined_type(frame.translation.types_ref(), *ty)?
1017                else {
1018                    unreachable!()
1019                };
1020                let index = self.result.trampolines.push((
1021                    *func,
1022                    dfg::Trampoline::FutureCancelWrite {
1023                        instance: frame.instance,
1024                        ty,
1025                        async_: *async_,
1026                    },
1027                ));
1028                frame.funcs.push((*func, dfg::CoreDef::Trampoline(index)));
1029            }
1030            FutureDropReadable { ty, func } => {
1031                let InterfaceType::Future(ty) =
1032                    types.defined_type(frame.translation.types_ref(), *ty)?
1033                else {
1034                    unreachable!()
1035                };
1036                let index = self.result.trampolines.push((
1037                    *func,
1038                    dfg::Trampoline::FutureDropReadable {
1039                        instance: frame.instance,
1040                        ty,
1041                    },
1042                ));
1043                frame.funcs.push((*func, dfg::CoreDef::Trampoline(index)));
1044            }
1045            FutureDropWritable { ty, func } => {
1046                let InterfaceType::Future(ty) =
1047                    types.defined_type(frame.translation.types_ref(), *ty)?
1048                else {
1049                    unreachable!()
1050                };
1051                let index = self.result.trampolines.push((
1052                    *func,
1053                    dfg::Trampoline::FutureDropWritable {
1054                        instance: frame.instance,
1055                        ty,
1056                    },
1057                ));
1058                frame.funcs.push((*func, dfg::CoreDef::Trampoline(index)));
1059            }
1060            ErrorContextNew { options } => {
1061                let ty = types.error_context_table_type()?;
1062                let func = options.core_type;
1063                let options = self.adapter_options(frame, types, options);
1064                let options = self.canonical_options(options);
1065                let index = self.result.trampolines.push((
1066                    func,
1067                    dfg::Trampoline::ErrorContextNew {
1068                        instance: frame.instance,
1069                        ty,
1070                        options,
1071                    },
1072                ));
1073                frame.funcs.push((func, dfg::CoreDef::Trampoline(index)));
1074            }
1075            ErrorContextDebugMessage { options } => {
1076                let ty = types.error_context_table_type()?;
1077                let func = options.core_type;
1078                let options = self.adapter_options(frame, types, options);
1079                let options = self.canonical_options(options);
1080                let index = self.result.trampolines.push((
1081                    func,
1082                    dfg::Trampoline::ErrorContextDebugMessage {
1083                        instance: frame.instance,
1084                        ty,
1085                        options,
1086                    },
1087                ));
1088                frame.funcs.push((func, dfg::CoreDef::Trampoline(index)));
1089            }
1090            ErrorContextDrop { func } => {
1091                let ty = types.error_context_table_type()?;
1092                let index = self.result.trampolines.push((
1093                    *func,
1094                    dfg::Trampoline::ErrorContextDrop {
1095                        instance: frame.instance,
1096                        ty,
1097                    },
1098                ));
1099                frame.funcs.push((*func, dfg::CoreDef::Trampoline(index)));
1100            }
1101            ContextGet { func, i } => {
1102                let index = self.result.trampolines.push((
1103                    *func,
1104                    dfg::Trampoline::ContextGet {
1105                        instance: frame.instance,
1106                        slot: *i,
1107                    },
1108                ));
1109                frame.funcs.push((*func, dfg::CoreDef::Trampoline(index)));
1110            }
1111            ContextSet { func, i } => {
1112                let index = self.result.trampolines.push((
1113                    *func,
1114                    dfg::Trampoline::ContextSet {
1115                        instance: frame.instance,
1116                        slot: *i,
1117                    },
1118                ));
1119                frame.funcs.push((*func, dfg::CoreDef::Trampoline(index)));
1120            }
1121            ThreadIndex { func } => {
1122                let index = self
1123                    .result
1124                    .trampolines
1125                    .push((*func, dfg::Trampoline::ThreadIndex));
1126                frame.funcs.push((*func, dfg::CoreDef::Trampoline(index)));
1127            }
1128            ThreadNewIndirect {
1129                func,
1130                start_func_table_index,
1131                start_func_ty,
1132            } => {
1133                let table_export = frame.tables[*start_func_table_index]
1134                    .clone()
1135                    .map_index(|i| match i {
1136                        EntityIndex::Table(i) => i,
1137                        _ => unreachable!(),
1138                    });
1139
1140                let table_id = self.result.tables.push(table_export);
1141                let index = self.result.trampolines.push((
1142                    *func,
1143                    dfg::Trampoline::ThreadNewIndirect {
1144                        instance: frame.instance,
1145                        start_func_ty_idx: *start_func_ty,
1146                        start_func_table_id: table_id,
1147                    },
1148                ));
1149                frame.funcs.push((*func, dfg::CoreDef::Trampoline(index)));
1150            }
1151            ThreadSwitchTo { func, cancellable } => {
1152                let index = self.result.trampolines.push((
1153                    *func,
1154                    dfg::Trampoline::ThreadSwitchTo {
1155                        instance: frame.instance,
1156                        cancellable: *cancellable,
1157                    },
1158                ));
1159                frame.funcs.push((*func, dfg::CoreDef::Trampoline(index)));
1160            }
1161            ThreadSuspend { func, cancellable } => {
1162                let index = self.result.trampolines.push((
1163                    *func,
1164                    dfg::Trampoline::ThreadSuspend {
1165                        instance: frame.instance,
1166                        cancellable: *cancellable,
1167                    },
1168                ));
1169                frame.funcs.push((*func, dfg::CoreDef::Trampoline(index)));
1170            }
1171            ThreadResumeLater { func } => {
1172                let index = self.result.trampolines.push((
1173                    *func,
1174                    dfg::Trampoline::ThreadResumeLater {
1175                        instance: frame.instance,
1176                    },
1177                ));
1178                frame.funcs.push((*func, dfg::CoreDef::Trampoline(index)));
1179            }
1180            ThreadYieldTo { func, cancellable } => {
1181                let index = self.result.trampolines.push((
1182                    *func,
1183                    dfg::Trampoline::ThreadYieldTo {
1184                        instance: frame.instance,
1185                        cancellable: *cancellable,
1186                    },
1187                ));
1188                frame.funcs.push((*func, dfg::CoreDef::Trampoline(index)));
1189            }
1190            ModuleStatic(idx, ty) => {
1191                frame.modules.push(ModuleDef::Static(*idx, *ty));
1192            }
1193
1194            // Instantiation of a module is one of the meatier initializers that
1195            // we'll generate. The main magic here is that for a statically
1196            // known module we can order the imports as a list to exactly what
1197            // the static module needs to be instantiated. For imported modules,
1198            // however, the runtime string resolution must happen at runtime so
1199            // that is deferred here by organizing the arguments as a two-layer
1200            // `IndexMap` of what we're providing.
1201            //
1202            // In both cases though a new `RuntimeInstanceIndex` is allocated
1203            // and an initializer is recorded to indicate that it's being
1204            // instantiated.
1205            ModuleInstantiate(module, args) => {
1206                let (instance_module, init) = match &frame.modules[*module] {
1207                    ModuleDef::Static(idx, _ty) => {
1208                        let mut defs = Vec::new();
1209                        for (module, name, _ty) in self.nested_modules[*idx].module.imports() {
1210                            let instance = args[module];
1211                            defs.push(
1212                                self.core_def_of_module_instance_export(frame, instance, name),
1213                            );
1214                        }
1215                        (
1216                            InstanceModule::Static(*idx),
1217                            dfg::Instance::Static(*idx, defs.into()),
1218                        )
1219                    }
1220                    ModuleDef::Import(path, ty) => {
1221                        let mut defs = IndexMap::new();
1222                        for ((module, name), _) in types[*ty].imports.iter() {
1223                            let instance = args[module.as_str()];
1224                            let def =
1225                                self.core_def_of_module_instance_export(frame, instance, name);
1226                            defs.entry(module.to_string())
1227                                .or_insert(IndexMap::new())
1228                                .insert(name.to_string(), def);
1229                        }
1230                        let index = self.runtime_import(path);
1231                        (
1232                            InstanceModule::Import(*ty),
1233                            dfg::Instance::Import(index, defs),
1234                        )
1235                    }
1236                };
1237
1238                let instance = self.result.instances.push(init);
1239                let instance2 = self.runtime_instances.push(instance_module);
1240                assert_eq!(instance, instance2);
1241
1242                self.result
1243                    .side_effects
1244                    .push(dfg::SideEffect::Instance(instance));
1245
1246                frame
1247                    .module_instances
1248                    .push(ModuleInstanceDef::Instantiated(instance, *module));
1249            }
1250
1251            ModuleSynthetic(map) => {
1252                frame
1253                    .module_instances
1254                    .push(ModuleInstanceDef::Synthetic(map));
1255            }
1256
1257            // This is one of the stages of the "magic" of implementing outer
1258            // aliases to components and modules. For more information on this
1259            // see the documentation on `LexicalScope`. This stage of the
1260            // implementation of outer aliases is where the `ClosedOverVars` is
1261            // transformed into a `ComponentClosure` state using the current
1262            // `InlinerFrame`'s state. This will capture the "runtime" state of
1263            // outer components and upvars and such naturally as part of the
1264            // inlining process.
1265            ComponentStatic(index, vars) => {
1266                frame.components.push(ComponentDef {
1267                    index: *index,
1268                    closure: ComponentClosure {
1269                        modules: vars
1270                            .modules
1271                            .iter()
1272                            .map(|(_, m)| frame.closed_over_module(m))
1273                            .collect(),
1274                        components: vars
1275                            .components
1276                            .iter()
1277                            .map(|(_, m)| frame.closed_over_component(m))
1278                            .collect(),
1279                    },
1280                });
1281            }
1282
1283            // Like module instantiation is this is a "meaty" part, and don't be
1284            // fooled by the relative simplicity of this case. This is
1285            // implemented primarily by the `Inliner` structure and the design
1286            // of this entire module, so the "easy" step here is to simply
1287            // create a new inliner frame and return it to get pushed onto the
1288            // stack.
1289            ComponentInstantiate(component, args, ty) => {
1290                let component: &ComponentDef<'a> = &frame.components[*component];
1291                let index = RuntimeComponentInstanceIndex::from_u32(
1292                    self.result.num_runtime_component_instances,
1293                );
1294                self.result.num_runtime_component_instances += 1;
1295                let frame = InlinerFrame::new(
1296                    index,
1297                    &self.nested_components[component.index],
1298                    component.closure.clone(),
1299                    args.iter()
1300                        .map(|(name, item)| Ok((*name, frame.item(*item, types)?)))
1301                        .collect::<Result<_>>()?,
1302                    Some(*ty),
1303                );
1304                return Ok(Some(frame));
1305            }
1306
1307            ComponentSynthetic(map, ty) => {
1308                let items = map
1309                    .iter()
1310                    .map(|(name, index)| Ok((*name, frame.item(*index, types)?)))
1311                    .collect::<Result<_>>()?;
1312                let types_ref = frame.translation.types_ref();
1313                let ty = types.convert_instance(types_ref, *ty)?;
1314                frame
1315                    .component_instances
1316                    .push(ComponentInstanceDef::Items(items, ty));
1317            }
1318
1319            // Core wasm aliases, this and the cases below, are creating
1320            // `CoreExport` items primarily to insert into the index space so we
1321            // can create a unique identifier pointing to each core wasm export
1322            // with the instance and relevant index/name as necessary.
1323            AliasExportFunc(instance, name) => {
1324                let (ty, def) = match &frame.module_instances[*instance] {
1325                    ModuleInstanceDef::Instantiated(instance, module) => {
1326                        let (ty, item) = match &frame.modules[*module] {
1327                            ModuleDef::Static(idx, _ty) => {
1328                                let entity = self.nested_modules[*idx].module.exports[*name];
1329                                let ty = match entity {
1330                                    EntityIndex::Function(f) => {
1331                                        self.nested_modules[*idx].module.functions[f]
1332                                            .signature
1333                                            .unwrap_module_type_index()
1334                                    }
1335                                    _ => unreachable!(),
1336                                };
1337                                (ty, ExportItem::Index(entity))
1338                            }
1339                            ModuleDef::Import(_path, module_ty) => {
1340                                let module_ty = &types.component_types()[*module_ty];
1341                                let entity_ty = &module_ty.exports[&**name];
1342                                let ty = entity_ty.unwrap_func().unwrap_module_type_index();
1343                                (ty, ExportItem::Name((*name).to_string()))
1344                            }
1345                        };
1346                        let def = dfg::CoreExport {
1347                            instance: *instance,
1348                            item,
1349                        }
1350                        .into();
1351                        (ty, def)
1352                    }
1353                    ModuleInstanceDef::Synthetic(instance) => match instance[*name] {
1354                        EntityIndex::Function(i) => frame.funcs[i].clone(),
1355                        _ => unreachable!(),
1356                    },
1357                };
1358                frame.funcs.push((ty, def));
1359            }
1360
1361            AliasExportTable(instance, name) => {
1362                frame.tables.push(
1363                    match self.core_def_of_module_instance_export(frame, *instance, *name) {
1364                        dfg::CoreDef::Export(e) => e,
1365                        _ => unreachable!(),
1366                    },
1367                );
1368            }
1369
1370            AliasExportGlobal(instance, name) => {
1371                frame.globals.push(
1372                    match self.core_def_of_module_instance_export(frame, *instance, *name) {
1373                        dfg::CoreDef::Export(e) => e,
1374                        _ => unreachable!(),
1375                    },
1376                );
1377            }
1378
1379            AliasExportMemory(instance, name) => {
1380                frame.memories.push(
1381                    match self.core_def_of_module_instance_export(frame, *instance, *name) {
1382                        dfg::CoreDef::Export(e) => e,
1383                        _ => unreachable!(),
1384                    },
1385                );
1386            }
1387
1388            AliasExportTag(instance, name) => {
1389                frame.tags.push(
1390                    match self.core_def_of_module_instance_export(frame, *instance, *name) {
1391                        dfg::CoreDef::Export(e) => e,
1392                        _ => unreachable!(),
1393                    },
1394                );
1395            }
1396
1397            AliasComponentExport(instance, name) => {
1398                match &frame.component_instances[*instance] {
1399                    ComponentInstanceDef::Intrinsics => {
1400                        frame.push_item(ComponentItemDef::Func(ComponentFuncDef::UnsafeIntrinsic(
1401                            UnsafeIntrinsic::from_str(name)?,
1402                        )));
1403                    }
1404
1405                    // Aliasing an export from an imported instance means that
1406                    // we're extending the `ImportPath` by one name, represented
1407                    // with the clone + push here. Afterwards an appropriate
1408                    // item is then pushed in the relevant index space.
1409                    ComponentInstanceDef::Import(path, ty) => {
1410                        let path = path.push(*name);
1411                        let def = ComponentItemDef::from_import(path, types[*ty].exports[*name])?;
1412                        frame.push_item(def);
1413                    }
1414
1415                    // Given a component instance which was either created
1416                    // through instantiation of a component or through a
1417                    // synthetic renaming of items we just schlep around the
1418                    // definitions of various items here.
1419                    ComponentInstanceDef::Items(map, _) => frame.push_item(map[*name].clone()),
1420                }
1421            }
1422
1423            // For more information on these see `LexicalScope` but otherwise
1424            // this is just taking a closed over variable and inserting the
1425            // actual definition into the local index space since this
1426            // represents an outer alias to a module/component
1427            AliasModule(idx) => {
1428                frame.modules.push(frame.closed_over_module(idx));
1429            }
1430            AliasComponent(idx) => {
1431                frame.components.push(frame.closed_over_component(idx));
1432            }
1433
1434            Export(item) => match item {
1435                ComponentItem::Func(i) => {
1436                    frame
1437                        .component_funcs
1438                        .push(frame.component_funcs[*i].clone());
1439                }
1440                ComponentItem::Module(i) => {
1441                    frame.modules.push(frame.modules[*i].clone());
1442                }
1443                ComponentItem::Component(i) => {
1444                    frame.components.push(frame.components[*i].clone());
1445                }
1446                ComponentItem::ComponentInstance(i) => {
1447                    frame
1448                        .component_instances
1449                        .push(frame.component_instances[*i].clone());
1450                }
1451
1452                // Type index spaces aren't maintained during this inlining pass
1453                // so ignore this.
1454                ComponentItem::Type(_) => {}
1455            },
1456        }
1457
1458        Ok(None)
1459    }
1460
1461    /// "Commits" a path of an import to an actual index which is something that
1462    /// will be calculated at runtime.
1463    ///
1464    /// Note that the cost of calculating an item for a `RuntimeImportIndex` at
1465    /// runtime is amortized with an `InstancePre` which represents "all the
1466    /// runtime imports are lined up" and after that no more name resolution is
1467    /// necessary.
1468    fn runtime_import(&mut self, path: &ImportPath<'a>) -> RuntimeImportIndex {
1469        *self
1470            .import_path_interner
1471            .entry(path.clone())
1472            .or_insert_with(|| {
1473                self.result.imports.push((
1474                    path.index,
1475                    path.path.iter().map(|s| s.to_string()).collect(),
1476                ))
1477            })
1478    }
1479
1480    /// Returns the `CoreDef`, the canonical definition for a core wasm item,
1481    /// for the export `name` of `instance` within `frame`.
1482    fn core_def_of_module_instance_export(
1483        &self,
1484        frame: &InlinerFrame<'a>,
1485        instance: ModuleInstanceIndex,
1486        name: &'a str,
1487    ) -> dfg::CoreDef {
1488        match &frame.module_instances[instance] {
1489            // Instantiations of a statically known module means that we can
1490            // refer to the exported item by a precise index, skipping name
1491            // lookups at runtime.
1492            //
1493            // Instantiations of an imported module, however, must do name
1494            // lookups at runtime since we don't know the structure ahead of
1495            // time here.
1496            ModuleInstanceDef::Instantiated(instance, module) => {
1497                let item = match frame.modules[*module] {
1498                    ModuleDef::Static(idx, _ty) => {
1499                        let entity = self.nested_modules[idx].module.exports[name];
1500                        ExportItem::Index(entity)
1501                    }
1502                    ModuleDef::Import(..) => ExportItem::Name(name.to_string()),
1503                };
1504                dfg::CoreExport {
1505                    instance: *instance,
1506                    item,
1507                }
1508                .into()
1509            }
1510
1511            // This is a synthetic instance so the canonical definition of the
1512            // original item is returned.
1513            ModuleInstanceDef::Synthetic(instance) => match instance[name] {
1514                EntityIndex::Function(i) => frame.funcs[i].1.clone(),
1515                EntityIndex::Table(i) => frame.tables[i].clone().into(),
1516                EntityIndex::Global(i) => frame.globals[i].clone().into(),
1517                EntityIndex::Memory(i) => frame.memories[i].clone().into(),
1518                EntityIndex::Tag(i) => frame.tags[i].clone().into(),
1519            },
1520        }
1521    }
1522
1523    fn memory(
1524        &mut self,
1525        frame: &InlinerFrame<'a>,
1526        types: &ComponentTypesBuilder,
1527        memory: MemoryIndex,
1528    ) -> (dfg::CoreExport<MemoryIndex>, bool) {
1529        let memory = frame.memories[memory].clone().map_index(|i| match i {
1530            EntityIndex::Memory(i) => i,
1531            _ => unreachable!(),
1532        });
1533        let memory64 = match &self.runtime_instances[memory.instance] {
1534            InstanceModule::Static(idx) => match &memory.item {
1535                ExportItem::Index(i) => {
1536                    let ty = &self.nested_modules[*idx].module.memories[*i];
1537                    match ty.idx_type {
1538                        IndexType::I32 => false,
1539                        IndexType::I64 => true,
1540                    }
1541                }
1542                ExportItem::Name(_) => unreachable!(),
1543            },
1544            InstanceModule::Import(ty) => match &memory.item {
1545                ExportItem::Name(name) => match types[*ty].exports[name] {
1546                    EntityType::Memory(m) => match m.idx_type {
1547                        IndexType::I32 => false,
1548                        IndexType::I64 => true,
1549                    },
1550                    _ => unreachable!(),
1551                },
1552                ExportItem::Index(_) => unreachable!(),
1553            },
1554        };
1555        (memory, memory64)
1556    }
1557
1558    /// Translates a `LocalCanonicalOptions` which indexes into the `frame`
1559    /// specified into a runtime representation.
1560    fn adapter_options(
1561        &mut self,
1562        frame: &InlinerFrame<'a>,
1563        types: &ComponentTypesBuilder,
1564        options: &LocalCanonicalOptions,
1565    ) -> AdapterOptions {
1566        let data_model = match options.data_model {
1567            LocalDataModel::Gc {} => DataModel::Gc {},
1568            LocalDataModel::LinearMemory { memory, realloc } => {
1569                let (memory, memory64) = memory
1570                    .map(|i| {
1571                        let (memory, memory64) = self.memory(frame, types, i);
1572                        (Some(memory), memory64)
1573                    })
1574                    .unwrap_or((None, false));
1575                let realloc = realloc.map(|i| frame.funcs[i].1.clone());
1576                DataModel::LinearMemory {
1577                    memory,
1578                    memory64,
1579                    realloc,
1580                }
1581            }
1582        };
1583        let callback = options.callback.map(|i| frame.funcs[i].1.clone());
1584        let post_return = options.post_return.map(|i| frame.funcs[i].1.clone());
1585        AdapterOptions {
1586            instance: frame.instance,
1587            string_encoding: options.string_encoding,
1588            callback,
1589            post_return,
1590            async_: options.async_,
1591            cancellable: options.cancellable,
1592            core_type: options.core_type,
1593            data_model,
1594        }
1595    }
1596
1597    /// Translatees an `AdapterOptions` into a `CanonicalOptions` where
1598    /// memories/functions are inserted into the global initializer list for
1599    /// use at runtime. This is only used for lowered host functions and lifted
1600    /// functions exported to the host.
1601    fn canonical_options(&mut self, options: AdapterOptions) -> dfg::OptionsId {
1602        let data_model = match options.data_model {
1603            DataModel::Gc {} => dfg::CanonicalOptionsDataModel::Gc {},
1604            DataModel::LinearMemory {
1605                memory,
1606                memory64: _,
1607                realloc,
1608            } => dfg::CanonicalOptionsDataModel::LinearMemory {
1609                memory: memory.map(|export| self.result.memories.push(export)),
1610                realloc: realloc.map(|def| self.result.reallocs.push(def)),
1611            },
1612        };
1613        let callback = options.callback.map(|def| self.result.callbacks.push(def));
1614        let post_return = options
1615            .post_return
1616            .map(|def| self.result.post_returns.push(def));
1617        self.result.options.push(dfg::CanonicalOptions {
1618            instance: options.instance,
1619            string_encoding: options.string_encoding,
1620            callback,
1621            post_return,
1622            async_: options.async_,
1623            cancellable: options.cancellable,
1624            core_type: options.core_type,
1625            data_model,
1626        })
1627    }
1628
1629    fn record_export(
1630        &mut self,
1631        name: &str,
1632        def: ComponentItemDef<'a>,
1633        types: &'a ComponentTypesBuilder,
1634        map: &mut IndexMap<String, dfg::Export>,
1635    ) -> Result<()> {
1636        let export = match def {
1637            // Exported modules are currently saved in a `PrimaryMap`, at
1638            // runtime, so an index (`RuntimeModuleIndex`) is assigned here and
1639            // then an initializer is recorded about where the module comes
1640            // from.
1641            ComponentItemDef::Module(module) => match module {
1642                ModuleDef::Static(index, ty) => dfg::Export::ModuleStatic { ty, index },
1643                ModuleDef::Import(path, ty) => dfg::Export::ModuleImport {
1644                    ty,
1645                    import: self.runtime_import(&path),
1646                },
1647            },
1648
1649            ComponentItemDef::Func(func) => match func {
1650                // If this is a lifted function from something lowered in this
1651                // component then the configured options are plumbed through
1652                // here.
1653                ComponentFuncDef::Lifted { ty, func, options } => {
1654                    let options = self.canonical_options(options);
1655                    dfg::Export::LiftedFunction { ty, func, options }
1656                }
1657
1658                // Currently reexported functions from an import are not
1659                // supported. Being able to actually call these functions is
1660                // somewhat tricky and needs something like temporary scratch
1661                // space that isn't implemented.
1662                ComponentFuncDef::Import(_) => {
1663                    bail!(
1664                        "component export `{name}` is a reexport of an imported function which is not implemented"
1665                    )
1666                }
1667
1668                ComponentFuncDef::UnsafeIntrinsic(_) => {
1669                    bail!(
1670                        "component export `{name}` is a reexport of an intrinsic function which is not supported"
1671                    )
1672                }
1673            },
1674
1675            ComponentItemDef::Instance(instance) => {
1676                let mut exports = IndexMap::new();
1677                match instance {
1678                    ComponentInstanceDef::Intrinsics => {
1679                        bail!(
1680                            "component export `{name}` is a reexport of the intrinsics instance which is not supported"
1681                        )
1682                    }
1683
1684                    // If this instance is one that was originally imported by
1685                    // the component itself then the imports are translated here
1686                    // by converting to a `ComponentItemDef` and then
1687                    // recursively recording the export as a reexport.
1688                    //
1689                    // Note that for now this would only work with
1690                    // module-exporting instances.
1691                    ComponentInstanceDef::Import(path, ty) => {
1692                        for (name, ty) in types[ty].exports.iter() {
1693                            let path = path.push(name);
1694                            let def = ComponentItemDef::from_import(path, *ty)?;
1695                            self.record_export(name, def, types, &mut exports)?;
1696                        }
1697                        dfg::Export::Instance { ty, exports }
1698                    }
1699
1700                    // An exported instance which is itself a bag of items is
1701                    // translated recursively here to our `exports` map which is
1702                    // the bag of items we're exporting.
1703                    ComponentInstanceDef::Items(map, ty) => {
1704                        for (name, def) in map {
1705                            self.record_export(name, def, types, &mut exports)?;
1706                        }
1707                        dfg::Export::Instance { ty, exports }
1708                    }
1709                }
1710            }
1711
1712            // FIXME(#4283) should make an official decision on whether this is
1713            // the final treatment of this or not.
1714            ComponentItemDef::Component(_) => {
1715                bail!("exporting a component from the root component is not supported")
1716            }
1717
1718            ComponentItemDef::Type(def) => dfg::Export::Type(def),
1719        };
1720
1721        map.insert(name.to_string(), export);
1722        Ok(())
1723    }
1724}
1725
1726impl<'a> InlinerFrame<'a> {
1727    fn new(
1728        instance: RuntimeComponentInstanceIndex,
1729        translation: &'a Translation<'a>,
1730        closure: ComponentClosure<'a>,
1731        args: HashMap<&'a str, ComponentItemDef<'a>>,
1732        instance_ty: Option<ComponentInstanceTypeId>,
1733    ) -> Self {
1734        // FIXME: should iterate over the initializers of `translation` and
1735        // calculate the size of each index space to use `with_capacity` for
1736        // all the maps below. Given that doing such would be wordy and compile
1737        // time is otherwise not super crucial it's not done at this time.
1738        InlinerFrame {
1739            instance,
1740            translation,
1741            closure,
1742            args,
1743            instance_ty,
1744            initializers: translation.initializers.iter(),
1745
1746            funcs: Default::default(),
1747            memories: Default::default(),
1748            tables: Default::default(),
1749            globals: Default::default(),
1750            tags: Default::default(),
1751
1752            component_instances: Default::default(),
1753            component_funcs: Default::default(),
1754            module_instances: Default::default(),
1755            components: Default::default(),
1756            modules: Default::default(),
1757        }
1758    }
1759
1760    fn item(
1761        &self,
1762        index: ComponentItem,
1763        types: &mut ComponentTypesBuilder,
1764    ) -> Result<ComponentItemDef<'a>> {
1765        Ok(match index {
1766            ComponentItem::Func(i) => ComponentItemDef::Func(self.component_funcs[i].clone()),
1767            ComponentItem::Component(i) => ComponentItemDef::Component(self.components[i].clone()),
1768            ComponentItem::ComponentInstance(i) => {
1769                ComponentItemDef::Instance(self.component_instances[i].clone())
1770            }
1771            ComponentItem::Module(i) => ComponentItemDef::Module(self.modules[i].clone()),
1772            ComponentItem::Type(t) => {
1773                let types_ref = self.translation.types_ref();
1774                ComponentItemDef::Type(types.convert_type(types_ref, t)?)
1775            }
1776        })
1777    }
1778
1779    /// Pushes the component `item` definition provided into the appropriate
1780    /// index space within this component.
1781    fn push_item(&mut self, item: ComponentItemDef<'a>) {
1782        match item {
1783            ComponentItemDef::Func(i) => {
1784                self.component_funcs.push(i);
1785            }
1786            ComponentItemDef::Module(i) => {
1787                self.modules.push(i);
1788            }
1789            ComponentItemDef::Component(i) => {
1790                self.components.push(i);
1791            }
1792            ComponentItemDef::Instance(i) => {
1793                self.component_instances.push(i);
1794            }
1795
1796            // In short, type definitions aren't tracked here.
1797            //
1798            // The longer form explanation for this is that structural types
1799            // like lists and records don't need to be tracked at all and the
1800            // only significant type which needs tracking is resource types
1801            // themselves. Resource types, however, are tracked within the
1802            // `ResourcesBuilder` state rather than an `InlinerFrame` so they're
1803            // ignored here as well. The general reason for that is that type
1804            // information is everywhere and this `InlinerFrame` is not
1805            // everywhere so it seemed like it would make sense to split the
1806            // two.
1807            //
1808            // Note though that this case is actually frequently hit, so it
1809            // can't be `unreachable!()`. Instead callers are responsible for
1810            // handling this appropriately with respect to resources.
1811            ComponentItemDef::Type(_ty) => {}
1812        }
1813    }
1814
1815    fn closed_over_module(&self, index: &ClosedOverModule) -> ModuleDef<'a> {
1816        match *index {
1817            ClosedOverModule::Local(i) => self.modules[i].clone(),
1818            ClosedOverModule::Upvar(i) => self.closure.modules[i].clone(),
1819        }
1820    }
1821
1822    fn closed_over_component(&self, index: &ClosedOverComponent) -> ComponentDef<'a> {
1823        match *index {
1824            ClosedOverComponent::Local(i) => self.components[i].clone(),
1825            ClosedOverComponent::Upvar(i) => self.closure.components[i].clone(),
1826        }
1827    }
1828
1829    /// Completes the instantiation of a subcomponent and records type
1830    /// information for the instance that was produced.
1831    ///
1832    /// This method is invoked when an `InlinerFrame` finishes for a
1833    /// subcomponent. The `def` provided represents the instance that was
1834    /// produced from instantiation, and `ty` is the wasmparser-defined type of
1835    /// the instance produced.
1836    ///
1837    /// The purpose of this method is to record type information about resources
1838    /// in the instance produced. In the component model all instantiations of a
1839    /// component produce fresh new types for all resources which are unequal to
1840    /// all prior resources. This means that if wasmparser's `ty` type
1841    /// information references a unique resource within `def` that has never
1842    /// been registered before then that means it's a defined resource within
1843    /// the component that was just instantiated (as opposed to an imported
1844    /// resource which was reexported).
1845    ///
1846    /// Further type translation after this instantiation can refer to these
1847    /// resource types and a mapping from those types to the wasmtime-internal
1848    /// types is required, so this method builds up those mappings.
1849    ///
1850    /// Essentially what happens here is that the `ty` type is registered and
1851    /// any new unique resources are registered so new tables can be introduced
1852    /// along with origin information about the actual underlying resource type
1853    /// and which component instantiated it.
1854    fn finish_instantiate(
1855        &mut self,
1856        exports: IndexMap<&'a str, ComponentItemDef<'a>>,
1857        ty: ComponentInstanceTypeId,
1858        types: &mut ComponentTypesBuilder,
1859    ) -> Result<()> {
1860        let types_ref = self.translation.types_ref();
1861        {
1862            let (resources, types) = types.resources_mut_and_types();
1863            let mut path = Vec::new();
1864            resources.register_component_entity_type(
1865                &types_ref,
1866                ComponentEntityType::Instance(ty),
1867                &mut path,
1868                &mut |path| match path {
1869                    [] => unreachable!(),
1870                    [name, rest @ ..] => exports[name].lookup_resource(rest, types),
1871                },
1872            );
1873        }
1874        let ty = types.convert_instance(types_ref, ty)?;
1875        let def = ComponentInstanceDef::Items(exports, ty);
1876        let arg = ComponentItemDef::Instance(def);
1877        self.push_item(arg);
1878        Ok(())
1879    }
1880}
1881
1882impl<'a> ImportPath<'a> {
1883    fn root(index: ImportIndex) -> ImportPath<'a> {
1884        ImportPath {
1885            index,
1886            path: Vec::new(),
1887        }
1888    }
1889
1890    fn push(&self, s: impl Into<Cow<'a, str>>) -> ImportPath<'a> {
1891        let mut new = self.clone();
1892        new.path.push(s.into());
1893        new
1894    }
1895}
1896
1897impl<'a> ComponentItemDef<'a> {
1898    fn from_import(path: ImportPath<'a>, ty: TypeDef) -> Result<ComponentItemDef<'a>> {
1899        let item = match ty {
1900            TypeDef::Module(ty) => ComponentItemDef::Module(ModuleDef::Import(path, ty)),
1901            TypeDef::ComponentInstance(ty) => {
1902                ComponentItemDef::Instance(ComponentInstanceDef::Import(path, ty))
1903            }
1904            TypeDef::ComponentFunc(_ty) => ComponentItemDef::Func(ComponentFuncDef::Import(path)),
1905            // FIXME(#4283) should commit one way or another to how this
1906            // should be treated.
1907            TypeDef::Component(_ty) => bail!("root-level component imports are not supported"),
1908            TypeDef::Interface(_) | TypeDef::Resource(_) => ComponentItemDef::Type(ty),
1909            TypeDef::CoreFunc(_) => unreachable!(),
1910        };
1911        Ok(item)
1912    }
1913
1914    /// Walks the `path` within `self` to find a resource at that path.
1915    ///
1916    /// This method is used when resources are found within wasmparser's type
1917    /// information and they need to be correlated with actual concrete
1918    /// definitions from this inlining pass. The `path` here is a list of
1919    /// instance export names (or empty) to walk to reach down into the final
1920    /// definition which should refer to a resource itself.
1921    fn lookup_resource(&self, path: &[&str], types: &ComponentTypes) -> ResourceIndex {
1922        let mut cur = self.clone();
1923
1924        // Each element of `path` represents unwrapping a layer of an instance
1925        // type, so handle those here by updating `cur` iteratively.
1926        for element in path.iter().copied() {
1927            let instance = match cur {
1928                ComponentItemDef::Instance(def) => def,
1929                _ => unreachable!(),
1930            };
1931            cur = match instance {
1932                // If this instance is a "bag of things" then this is as easy as
1933                // looking up the name in the bag of names.
1934                ComponentInstanceDef::Items(names, _) => names[element].clone(),
1935
1936                // If, however, this instance is an imported instance then this
1937                // is a further projection within the import with one more path
1938                // element. The `types` type information is used to lookup the
1939                // type of `element` within the instance type, and that's used
1940                // in conjunction with a one-longer `path` to produce a new item
1941                // definition.
1942                ComponentInstanceDef::Import(path, ty) => {
1943                    ComponentItemDef::from_import(path.push(element), types[ty].exports[element])
1944                        .unwrap()
1945                }
1946                ComponentInstanceDef::Intrinsics => {
1947                    unreachable!("intrinsics do not define resources")
1948                }
1949            };
1950        }
1951
1952        // Once `path` has been iterated over it must be the case that the final
1953        // item is a resource type, in which case a lookup can be performed.
1954        match cur {
1955            ComponentItemDef::Type(TypeDef::Resource(idx)) => types[idx].unwrap_concrete_ty(),
1956            _ => unreachable!(),
1957        }
1958    }
1959}
1960
1961#[derive(Clone, Copy)]
1962enum InstanceModule {
1963    Static(StaticModuleIndex),
1964    Import(TypeModuleIndex),
1965}