Skip to main content

synth_core/
component.rs

1//! WebAssembly Component Model data structures
2
3use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5
6/// A WebAssembly Component
7#[derive(Debug, Clone, Serialize, Deserialize)]
8pub struct Component {
9    /// Component name/ID
10    pub name: String,
11
12    /// Core modules contained in this component
13    pub modules: Vec<CoreModule>,
14
15    /// Nested components
16    pub components: Vec<Component>,
17
18    /// Component instances
19    pub instances: Vec<ComponentInstance>,
20
21    /// Interfaces defined by this component
22    pub interfaces: HashMap<String, WITInterface>,
23
24    /// Imports required by this component
25    pub imports: Vec<Import>,
26
27    /// Exports provided by this component
28    pub exports: Vec<Export>,
29}
30
31/// A core WebAssembly module
32#[derive(Debug, Clone, Serialize, Deserialize)]
33pub struct CoreModule {
34    /// Module ID
35    pub id: String,
36
37    /// Module binary data
38    pub binary: Vec<u8>,
39
40    /// Functions defined in this module
41    pub functions: Vec<Function>,
42
43    /// Linear memories
44    pub memories: Vec<Memory>,
45
46    /// Tables
47    pub tables: Vec<Table>,
48
49    /// Globals
50    pub globals: Vec<Global>,
51}
52
53/// A function in a core module
54#[derive(Debug, Clone, Serialize, Deserialize)]
55pub struct Function {
56    /// Function index
57    pub index: u32,
58
59    /// Function name (if available)
60    pub name: Option<String>,
61
62    /// Function type signature
63    pub signature: FunctionSignature,
64
65    /// Is this function exported?
66    pub exported: bool,
67
68    /// Is this function imported?
69    pub imported: bool,
70}
71
72/// Function signature (parameters and results)
73#[derive(Debug, Clone, Serialize, Deserialize)]
74pub struct FunctionSignature {
75    /// Parameter types
76    pub params: Vec<ValueType>,
77
78    /// Result types
79    pub results: Vec<ValueType>,
80}
81
82/// WebAssembly value types
83#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
84pub enum ValueType {
85    I32,
86    I64,
87    F32,
88    F64,
89    V128,
90    FuncRef,
91    ExternRef,
92}
93
94/// Linear memory
95#[derive(Debug, Clone, Serialize, Deserialize)]
96pub struct Memory {
97    /// Memory index
98    pub index: u32,
99
100    /// Initial size in pages (64KB each)
101    pub initial: u32,
102
103    /// Maximum size in pages (if limited)
104    pub maximum: Option<u32>,
105
106    /// Is this memory shared?
107    pub shared: bool,
108
109    /// Memory64 (64-bit addressing)
110    pub memory64: bool,
111}
112
113/// Table
114#[derive(Debug, Clone, Serialize, Deserialize)]
115pub struct Table {
116    /// Table index
117    pub index: u32,
118
119    /// Element type
120    pub element_type: ValueType,
121
122    /// Initial size
123    pub initial: u32,
124
125    /// Maximum size (if limited)
126    pub maximum: Option<u32>,
127}
128
129/// Global variable
130#[derive(Debug, Clone, Serialize, Deserialize)]
131pub struct Global {
132    /// Global index
133    pub index: u32,
134
135    /// Value type
136    pub value_type: ValueType,
137
138    /// Is this global mutable?
139    pub mutable: bool,
140}
141
142/// Component instance
143#[derive(Debug, Clone, Serialize, Deserialize)]
144pub struct ComponentInstance {
145    /// Instance ID
146    pub id: String,
147
148    /// Referenced component
149    pub component: String,
150
151    /// Allow recursive reentrance into this instance.
152    ///
153    /// The Component Model spec traps when `call_might_be_recursive` detects
154    /// that a component instance is already on the call stack (Concurrency.md).
155    /// After fusion (e.g. via meld), the caller and callee share the same
156    /// instance, making cross-component calls appear reentrant even though
157    /// the original components were distinct.
158    ///
159    /// When `true`, the AOT-generated canonical ABI entry sequence skips
160    /// the reentrancy guard for calls into this instance. This is an opt-in
161    /// extension ahead of the spec's planned `recursive` effect on function
162    /// types.
163    ///
164    /// Default: `false` (spec-compliant trapping behavior).
165    #[serde(default)]
166    pub recursive_reentrance: bool,
167}
168
169/// WIT (WebAssembly Interface Type) interface
170#[derive(Debug, Clone, Serialize, Deserialize)]
171pub struct WITInterface {
172    /// Interface name
173    pub name: String,
174
175    /// Functions in this interface
176    pub functions: Vec<WITFunction>,
177
178    /// Types defined in this interface
179    pub types: Vec<WITType>,
180}
181
182/// WIT function
183#[derive(Debug, Clone, Serialize, Deserialize)]
184pub struct WITFunction {
185    /// Function name
186    pub name: String,
187
188    /// Parameters
189    pub params: Vec<(String, WITType)>,
190
191    /// Results
192    pub results: Vec<WITType>,
193}
194
195/// WIT types (component model types)
196#[derive(Debug, Clone, Serialize, Deserialize)]
197pub enum WITType {
198    Bool,
199    U8,
200    U16,
201    U32,
202    U64,
203    S8,
204    S16,
205    S32,
206    S64,
207    F32,
208    F64,
209    String,
210    List(Box<WITType>),
211    Record(Vec<(String, WITType)>),
212    Variant(Vec<(String, Option<WITType>)>),
213    Enum(Vec<String>),
214    Option(Box<WITType>),
215    Result { ok: Box<WITType>, err: Box<WITType> },
216}
217
218/// Import declaration
219#[derive(Debug, Clone, Serialize, Deserialize)]
220pub struct Import {
221    /// Import name
222    pub name: String,
223
224    /// Import kind
225    pub kind: ImportKind,
226}
227
228/// Import kind
229#[derive(Debug, Clone, Serialize, Deserialize)]
230pub enum ImportKind {
231    Function(FunctionSignature),
232    Memory(Memory),
233    Table(Table),
234    Global(Global),
235}
236
237/// Export declaration
238#[derive(Debug, Clone, Serialize, Deserialize)]
239pub struct Export {
240    /// Export name
241    pub name: String,
242
243    /// Export kind
244    pub kind: ExportKind,
245}
246
247/// Export kind
248#[derive(Debug, Clone, Serialize, Deserialize)]
249pub enum ExportKind {
250    Function(u32),
251    Memory(u32),
252    Table(u32),
253    Global(u32),
254}
255
256impl Component {
257    /// Create a new component
258    pub fn new(name: String) -> Self {
259        Self {
260            name,
261            modules: Vec::new(),
262            components: Vec::new(),
263            instances: Vec::new(),
264            interfaces: HashMap::new(),
265            imports: Vec::new(),
266            exports: Vec::new(),
267        }
268    }
269
270    /// Get total number of linear memories across all modules
271    pub fn total_memories(&self) -> usize {
272        self.modules.iter().map(|m| m.memories.len()).sum()
273    }
274
275    /// Get total memory size required (in bytes)
276    pub fn total_memory_size(&self) -> u64 {
277        self.modules
278            .iter()
279            .flat_map(|m| &m.memories)
280            .map(|mem| mem.initial as u64 * 65536) // 64KB pages
281            .sum()
282    }
283}