wasmtime_wizer/component/
info.rs1use crate::ModuleContext;
2use std::collections::HashMap;
3
4#[derive(Default)]
9pub struct ComponentContext<'a> {
10 pub(crate) sections: Vec<RawSection<'a>>,
13
14 pub(crate) instances: u32,
19 pub(crate) funcs: u32,
20 pub(crate) types: u32,
21 pub(crate) core_instances: u32,
22 pub(crate) core_memories: u32,
23 pub(crate) core_funcs: u32,
24
25 pub(crate) core_instantiations: HashMap<u32, u32>,
28
29 pub(crate) accessors: Option<Vec<Accessor>>,
31}
32
33pub(crate) enum Accessor {
35 Global {
37 module_index: u32,
40
41 core_export_name: String,
43
44 accessor_export_name: String,
46
47 ty: wasmparser::ValType,
49 },
50
51 Memory {
54 module_index: u32,
57
58 core_export_name: String,
60
61 accessor_export_name: String,
63 },
64}
65
66pub(crate) enum RawSection<'a> {
68 Raw(wasm_encoder::RawSection<'a>),
70
71 Module(ModuleContext<'a>),
73}
74
75impl<'a> ComponentContext<'a> {
76 pub(crate) fn push_raw_section(&mut self, section: wasm_encoder::RawSection<'a>) {
77 self.sections.push(RawSection::Raw(section));
78 }
79
80 pub(crate) fn push_module_section(&mut self, module: ModuleContext<'a>) {
81 self.sections.push(RawSection::Module(module));
82 }
83
84 pub(crate) fn core_modules(&self) -> impl Iterator<Item = (u32, &ModuleContext<'a>)> + '_ {
85 let mut i = 0;
86 self.sections.iter().filter_map(move |s| match s {
87 RawSection::Module(m) => Some((inc(&mut i), m)),
88 RawSection::Raw(_) => None,
89 })
90 }
91
92 pub(crate) fn num_core_modules(&self) -> u32 {
93 u32::try_from(self.core_modules().count()).unwrap()
94 }
95
96 pub(crate) fn inc(&mut self, kind: wasmparser::ComponentExternalKind) {
97 match kind {
98 wasmparser::ComponentExternalKind::Type => {
99 self.inc_types();
100 }
101 wasmparser::ComponentExternalKind::Instance => {
102 self.inc_instances();
103 }
104 wasmparser::ComponentExternalKind::Func => {
105 self.inc_funcs();
106 }
107 wasmparser::ComponentExternalKind::Component
108 | wasmparser::ComponentExternalKind::Module
109 | wasmparser::ComponentExternalKind::Value => {}
110 }
111 }
112
113 pub(crate) fn inc_core(&mut self, kind: wasmparser::ExternalKind) {
114 match kind {
115 wasmparser::ExternalKind::Func => {
116 self.inc_core_funcs();
117 }
118 wasmparser::ExternalKind::Memory => {
119 self.inc_core_memories();
120 }
121 wasmparser::ExternalKind::Table
122 | wasmparser::ExternalKind::Global
123 | wasmparser::ExternalKind::Tag => {}
124 }
125 }
126
127 pub(crate) fn inc_instances(&mut self) -> u32 {
128 inc(&mut self.instances)
129 }
130
131 pub(crate) fn inc_funcs(&mut self) -> u32 {
132 inc(&mut self.funcs)
133 }
134
135 pub(crate) fn inc_core_memories(&mut self) -> u32 {
136 inc(&mut self.core_memories)
137 }
138
139 pub(crate) fn inc_types(&mut self) -> u32 {
140 inc(&mut self.types)
141 }
142
143 pub(crate) fn inc_core_instances(&mut self) -> u32 {
144 inc(&mut self.core_instances)
145 }
146
147 pub(crate) fn inc_core_funcs(&mut self) -> u32 {
148 inc(&mut self.core_funcs)
149 }
150}
151
152fn inc(count: &mut u32) -> u32 {
153 let current = *count;
154 *count += 1;
155 current
156}