-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathComponentDef.swift
More file actions
283 lines (242 loc) · 9.12 KB
/
Copy pathComponentDef.swift
File metadata and controls
283 lines (242 loc) · 9.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#if ComponentModel
import ComponentModel
import WasmParserCore
import WasmTypes
extension ComponentWatParser {
struct ComponentDefField {
let location: Location
let kind: NormalizedDefinition
}
struct ValueDef: NamedFieldDecl {
var id: Name?
}
struct ComponentInstanceDef: NamedFieldDecl {
var id: Name?
var componentRef: Parser.IndexOrId?
var arguments: [ComponentInstanceArgument]
}
struct ComponentInstanceArgument {
enum Kind {
case instance(Parser.IndexOrId)
}
let name: String
let kind: Kind
}
/// A core function definition created via canon lower.
struct CoreFuncDef: NamedFieldDecl {
var id: Name?
}
/// A core memory definition created via alias.
struct CoreMemoryAliasDef: NamedFieldDecl {
var id: Name?
}
/// https://github.com/WebAssembly/component-model/blob/main/design/mvp/Explainer.md#index-spaces
public struct ComponentDef: NamedFieldDecl {
var id: Name?
// Listed in the order of section indices in binary encoding.
var coreModulesMap: NameMapping<ModuleDef> = .init()
var coreInstancesMap: NameMapping<CoreInstanceDef> = .init()
var coreTypesMap: CoreTypesMap = .init()
var coreFunctionsMap: NameMapping<CoreFuncDef> = .init() // Core functions created by canon lower
var coreMemoriesMap: NameMapping<CoreMemoryAliasDef> = .init() // Core memories created by alias
var componentFunctions: NameMapping<ComponentFuncDef> = .init()
var valuesMap: NameMapping<ValueDef> = .init()
var componentTypes: NameMapping<ComponentTypeDef> = .init()
var componentInstancesMap: NameMapping<ComponentInstanceDef> = .init()
var componentsMap: NameMapping<ComponentDef> = .init()
/// As sections in CM binaries can stay disjoint and unmerged,
/// we should preserve disjoint sections together with their ordering.
var fields = [ComponentDefField]()
/// Mapping from a component type to its unique ID in `componentTypes` name mapping storage.
var typesMap = [ComponentTypeDef.Kind: Int]()
}
struct ModuleDef: NamedFieldDecl {
var id: Name?
var wat: Wat
}
struct CoreInstanceDef: NamedFieldDecl {
struct Argument {
enum Kind {
struct Export {
let name: String
let sort: CoreDefSort
let index: UInt32
}
case instance(Parser.IndexOrId)
case exports([Export])
}
let importName: String
let kind: Kind
}
var id: Name?
var moduleId: Parser.IndexOrId
var arguments: [Argument]
}
struct CoreTypeDef: NamedFieldDecl {
enum Kind {
case function(WatParser.FunctionType)
case module(CoreModuleTypeDef)
}
var id: Name?
let kind: Kind
}
struct CoreModuleTypeDef {
var declarations: [CoreModuleDecl]
var localTypeBindings: [String: Int] = [:]
func resolveTypeIndex(use: Parser.IndexOrId, globalResolver: CoreTypesMap) throws(WatParserError) -> Int {
switch use {
case .id(let id, _):
if let localDeclIndex = localTypeBindings[id.value] {
return localDeclIndex
}
return try globalResolver.resolveIndex(use: use)
case .index:
return try globalResolver.resolveIndex(use: use)
}
}
}
enum CoreModuleDecl {
case alias(CoreModuleAlias)
case type(TypeIndex)
case `import`(CoreModuleImport)
case export(CoreModuleExport)
}
struct CoreModuleAlias {
let sort: CoreDefSort
let target: CoreModuleAliasTarget
let bindingId: Name?
}
enum CoreModuleAliasTarget {
case outer(componentId: Parser.IndexOrId, index: Parser.IndexOrId, resolvedIndex: Int, outerCount: Int)
}
enum CoreAliasSort: String {
case `func`
case table
case memory
case global
case type
}
/// Component-level alias definition.
/// Syntax: (alias core export $instance "name" (core <sort> $bindingId))
/// (alias export $instance "name" (<sort> $bindingId))
/// (alias outer $component $idx (<sort> $bindingId))
///
/// This is separate from `ComponentModel.ComponentAlias` because the parser works with
/// unresolved symbolic references (`Parser.IndexOrId`) while the binary representation
/// uses resolved numeric indices (`UInt32`). The encoder resolves these during binary encoding.
struct ComponentAlias {
enum Target {
case export(isCore: Bool, instanceIndex: Parser.IndexOrId, exportName: String)
case outer(outerCount: Parser.IndexOrId, typeIndex: Parser.IndexOrId)
}
let sort: ComponentDefSort
let target: Target
let bindingId: Name?
}
struct CoreModuleImport {
let moduleName: String
let name: String
let descriptor: CoreImportDesc
}
enum CoreImportDesc {
case `func`(typeIndex: Parser.IndexOrId)
}
struct CoreModuleExport {
let name: String
let descriptor: CoreImportDesc
}
struct FuncIndex {
var instance: Parser.IndexOrId
var exportName: String
}
/// Reference to a core memory via instance export.
/// Used in canon options: (memory $instance "export")
struct MemoryRef {
var instance: Parser.IndexOrId
var exportName: String
}
/// Reference to a component function via instance export.
/// Used in canon lower: (func $instance "export")
struct ComponentFuncRef {
var instance: Parser.IndexOrId
var exportName: String
}
struct CanonDef {
enum Option {
case stringEncoding(ComponentStringEncoding)
case memory(MemoryRef)
case realloc(FuncIndex)
case postReturn(FuncIndex)
case `async`
case callback(FuncIndex)
}
enum Kind {
case lower(ComponentFuncRef)
case lift(FuncIndex)
}
let kind: Kind
let functionIndex: FuncIndex
let options: [Option]
}
enum ExternDesc {
case module(Parser.IndexOrId)
case function(ComponentFuncIndex)
case functionFromInstance(Parser.IndexOrId, String) // (instance, exportName)
case value(Parser.IndexOrId)
case type(Parser.IndexOrId)
case component(Parser.IndexOrId)
case instance(Parser.IndexOrId)
}
struct ExportDef {
let exportName: String
let descriptor: ExternDesc
}
struct ImportDef {
let importModuleName: String
let importName: String
let descriptor: ExternDesc
}
struct ComponentFuncDef: NamedFieldDecl {
var id: Name?
let paramNames: [String]
let type: ComponentTypeIndex
}
struct ComponentTypeDef: NamedFieldDecl {
enum Kind: Hashable {
case function(ComponentFuncType)
case value(ComponentValueType)
case instance(InstanceTypeDef)
case component(ComponentInnerTypeDef)
}
var id: Name?
let kind: Kind
}
struct InstanceTypeDef: Hashable {
struct TypeDecl: Hashable {
let id: Name?
let valueType: ComponentValueType
}
struct ExportDecl: Hashable {
let name: String
// Simplified - full implementation would handle type equality constraints
}
let typeDecls: [TypeDecl]
let exports: [ExportDecl]
}
struct ComponentInnerTypeDef: Hashable {
struct TypeDecl: Hashable {
let id: Name?
let valueType: ComponentValueType
}
struct ImportDecl: Hashable {
let name: String
}
struct ExportDecl: Hashable {
let name: String
}
let typeDecls: [TypeDecl]
let imports: [ImportDecl]
let exports: [ExportDecl]
}
}
#endif