wasmtime_wizer/component/
info.rs1use crate::ModuleContext;
2use std::collections::HashMap;
3
4#[derive(Clone, 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
33#[derive(Clone)]
35pub(crate) enum Accessor {
36 Global {
38 module_index: u32,
41
42 core_export_name: String,
44
45 accessor_export_name: String,
47
48 ty: wasmparser::ValType,
50 },
51
52 Memory {
55 module_index: u32,
58
59 core_export_name: String,
61
62 accessor_export_name: String,
64 },
65}
66
67#[derive(Clone)]
69pub(crate) enum RawSection<'a> {
70 Raw(wasm_encoder::RawSection<'a>),
72
73 Module(ModuleContext<'a>),
75}
76
77impl<'a> ComponentContext<'a> {
78 pub(crate) fn push_raw_section(&mut self, section: wasm_encoder::RawSection<'a>) {
79 self.sections.push(RawSection::Raw(section));
80 }
81
82 pub(crate) fn push_module_section(&mut self, module: ModuleContext<'a>) {
83 self.sections.push(RawSection::Module(module));
84 }
85
86 pub(crate) fn core_modules(&self) -> impl Iterator<Item = (u32, &ModuleContext<'a>)> + '_ {
87 let mut i = 0;
88 self.sections.iter().filter_map(move |s| match s {
89 RawSection::Module(m) => Some((inc(&mut i), m)),
90 RawSection::Raw(_) => None,
91 })
92 }
93
94 pub(crate) fn num_core_modules(&self) -> u32 {
95 u32::try_from(self.core_modules().count()).unwrap()
96 }
97
98 pub(crate) fn inc(&mut self, kind: wasmparser::ComponentExternalKind) {
99 match kind {
100 wasmparser::ComponentExternalKind::Type => {
101 self.inc_types();
102 }
103 wasmparser::ComponentExternalKind::Instance => {
104 self.inc_instances();
105 }
106 wasmparser::ComponentExternalKind::Func => {
107 self.inc_funcs();
108 }
109 wasmparser::ComponentExternalKind::Component
110 | wasmparser::ComponentExternalKind::Module
111 | wasmparser::ComponentExternalKind::Value => {}
112 }
113 }
114
115 pub(crate) fn inc_core(&mut self, kind: wasmparser::ExternalKind) {
116 match kind {
117 wasmparser::ExternalKind::Func | wasmparser::ExternalKind::FuncExact => {
118 self.inc_core_funcs();
119 }
120 wasmparser::ExternalKind::Memory => {
121 self.inc_core_memories();
122 }
123 wasmparser::ExternalKind::Table
124 | wasmparser::ExternalKind::Global
125 | wasmparser::ExternalKind::Tag => {}
126 }
127 }
128
129 pub(crate) fn inc_instances(&mut self) -> u32 {
130 inc(&mut self.instances)
131 }
132
133 pub(crate) fn inc_funcs(&mut self) -> u32 {
134 inc(&mut self.funcs)
135 }
136
137 pub(crate) fn inc_core_memories(&mut self) -> u32 {
138 inc(&mut self.core_memories)
139 }
140
141 pub(crate) fn inc_types(&mut self) -> u32 {
142 inc(&mut self.types)
143 }
144
145 pub(crate) fn inc_core_instances(&mut self) -> u32 {
146 inc(&mut self.core_instances)
147 }
148
149 pub(crate) fn inc_core_funcs(&mut self) -> u32 {
150 inc(&mut self.core_funcs)
151 }
152}
153
154fn inc(count: &mut u32) -> u32 {
155 let current = *count;
156 *count += 1;
157 current
158}