@@ -21,19 +21,19 @@ struct Module
21
21
// /
22
22
immutable uint version_ = 1 ;
23
23
// /
24
- Memory[] memorySection;
24
+ const ( Memory) [] memorySection;
25
25
// /
26
- Data[] dataSection;
26
+ const ( Data) [] dataSection;
27
27
// /
28
- FuncType[] typeSection;
28
+ const ( FuncType) [] typeSection;
29
29
// /
30
- uint [] functionSection;
30
+ const ( uint ) [] functionSection;
31
31
// /
32
32
Function[] codeSection;
33
33
// /
34
- Export[] exportSection;
34
+ const ( Export) [] exportSection;
35
35
// /
36
- Import[] importSection;
36
+ const ( Import) [] importSection;
37
37
}
38
38
39
39
// /
@@ -44,20 +44,14 @@ Module decodeModule(ref const(ubyte)[] input)
44
44
enforce(' a' == input.read! ubyte ());
45
45
enforce(' s' == input.read! ubyte ());
46
46
enforce(' m' == input.read! ubyte ());
47
- auto version_ = input.read! (uint , Endian.littleEndian)();
47
+ const version_ = input.read! (uint , Endian.littleEndian)();
48
48
enforce(version_ == 1 );
49
49
50
- Memory[] memorySection;
51
- Data[] dataSection;
52
- FuncType[] typeSection;
53
- uint [] functionSection;
54
- Function[] codeSection;
55
- Export[] exportSection;
56
- Import[] importSection;
50
+ Module mod;
57
51
58
52
while (input.length)
59
53
{
60
- auto sectionHeader = decodeSectionHeader(input);
54
+ const sectionHeader = decodeSectionHeader(input);
61
55
switch (sectionHeader[0 ])
62
56
{
63
57
case SectionCode.Custom:
@@ -66,66 +60,58 @@ Module decodeModule(ref const(ubyte)[] input)
66
60
break ;
67
61
case SectionCode.Memory:
68
62
const memory = decodeMemorySection(input);
69
- memorySection = [memory];
63
+ mod. memorySection = [memory];
70
64
break ;
71
65
case SectionCode.Data:
72
- dataSection = decodeDataSection(input);
66
+ mod. dataSection = decodeDataSection(input);
73
67
break ;
74
68
case SectionCode.Type:
75
- typeSection = decodeTypeSection(input);
69
+ mod. typeSection = decodeTypeSection(input);
76
70
break ;
77
71
case SectionCode.Function:
78
- functionSection = decodeFunctionSection(input);
72
+ mod. functionSection = decodeFunctionSection(input);
79
73
break ;
80
74
case SectionCode.Code:
81
- codeSection = decodeCodeSection(input);
75
+ mod. codeSection = decodeCodeSection(input);
82
76
break ;
83
77
case SectionCode.Export:
84
- exportSection = decodeExportSection(input);
78
+ mod. exportSection = decodeExportSection(input);
85
79
break ;
86
80
case SectionCode.Import:
87
- importSection = decodeImportSection(input);
81
+ mod. importSection = decodeImportSection(input);
88
82
break ;
89
83
default :
90
84
assert (false );
91
85
}
92
86
}
93
87
94
- return Module (
95
- memorySection: memorySection,
96
- dataSection: dataSection,
97
- typeSection: typeSection,
98
- functionSection: functionSection,
99
- codeSection: codeSection,
100
- exportSection: exportSection,
101
- importSection: importSection
102
- );
88
+ return mod;
103
89
}
104
90
105
91
private :
106
92
107
93
// /
108
94
Tuple ! (SectionCode, uint ) decodeSectionHeader(ref const (ubyte )[] input)
109
95
{
110
- auto code = cast (SectionCode) input.read! ubyte ();
96
+ auto code = input.read! SectionCode ();
111
97
auto size = input.leb128! uint ();
112
98
return tuple (code, size);
113
99
}
114
100
115
101
// /
116
- Memory decodeMemorySection (ref const (ubyte )[] input)
102
+ const ( Memory) decodeMemorySection (ref const (ubyte )[] input)
117
103
{
118
104
input.leb128! uint ();
119
- auto limits = decodeLimits(input);
105
+ const limits = decodeLimits(input);
120
106
return Memory (limits: limits);
121
107
}
122
108
123
109
// /
124
110
Limits decodeLimits (ref const (ubyte )[] input)
125
111
{
126
112
const flags = input.leb128! uint ();
127
- auto min = input.leb128! uint ();
128
- auto max = flags == 0 ? uint .max : input.leb128! uint ();
113
+ const min = input.leb128! uint ();
114
+ const max = flags == 0 ? uint .max : input.leb128! uint ();
129
115
return Limits (min: min, max: max);
130
116
}
131
117
@@ -139,16 +125,16 @@ uint decodeExpr(ref const(ubyte)[] input)
139
125
}
140
126
141
127
// /
142
- Data[] decodeDataSection (ref const (ubyte )[] input)
128
+ const ( Data) [] decodeDataSection (ref const (ubyte )[] input)
143
129
{
144
130
const count = input.leb128! uint ();
145
- Data[] data;
131
+ const ( Data) [] data;
146
132
foreach (_; 0 .. count)
147
133
{
148
- auto memoryIndex = input.leb128! uint ();
149
- auto offset = input.decodeExpr();
150
- auto size = input.leb128! uint ();
151
- auto bytes = cast (ubyte []) input[0 .. size];
134
+ const memoryIndex = input.leb128! uint ();
135
+ const offset = input.decodeExpr();
136
+ const size = input.leb128! uint ();
137
+ const (ubyte )[] bytes = input[0 .. size];
152
138
input.popFrontExactly(size);
153
139
data ~= Data(
154
140
memoryIndex: memoryIndex,
@@ -162,32 +148,32 @@ Data[] decodeDataSection(ref const(ubyte)[] input)
162
148
// /
163
149
ValueType decodeValueSection (ref const (ubyte )[] input)
164
150
{
165
- return cast (ValueType) input.read! ubyte ();
151
+ return input.read! ValueType ();
166
152
}
167
153
168
154
// /
169
- FuncType[] decodeTypeSection (ref const (ubyte )[] input)
155
+ const ( FuncType) [] decodeTypeSection (ref const (ubyte )[] input)
170
156
{
171
- FuncType[] funcTypes;
157
+ const ( FuncType) [] funcTypes;
172
158
const count = input.leb128! uint ();
173
159
foreach (_; 0 .. count)
174
160
{
175
161
input.read! ubyte ();
176
162
uint size = input.leb128! uint ();
177
- auto params = cast (ValueType[]) input[0 .. size];
163
+ const params = cast (const ( ValueType[]) ) input[0 .. size];
178
164
input.popFrontExactly(size);
179
165
size = input.leb128! uint ();
180
- auto results = cast (ValueType[]) input[0 .. size];
166
+ const results = cast (const ( ValueType[]) ) input[0 .. size];
181
167
input.popFrontExactly(size);
182
- funcTypes ~= FuncType(params, results);
168
+ funcTypes ~= FuncType(params: params, results: results);
183
169
}
184
170
return funcTypes;
185
171
}
186
172
187
173
// /
188
- uint [] decodeFunctionSection (ref const (ubyte )[] input)
174
+ const ( uint ) [] decodeFunctionSection (ref const (ubyte )[] input)
189
175
{
190
- uint [] funcIdxList;
176
+ const ( uint ) [] funcIdxList;
191
177
const count = input.leb128! uint ();
192
178
foreach (_; 0 .. count)
193
179
{
@@ -227,7 +213,7 @@ Function decodeFunctionBody(ref const(ubyte)[] input, ref uint remaining)
227
213
228
214
while (remaining > 0 )
229
215
{
230
- auto instruction = input.decodeInstruction(remaining);
216
+ const instruction = input.decodeInstruction(remaining);
231
217
body .code ~= instruction;
232
218
}
233
219
return body ;
@@ -236,30 +222,30 @@ Function decodeFunctionBody(ref const(ubyte)[] input, ref uint remaining)
236
222
// /
237
223
Instruction decodeInstruction (ref const (ubyte )[] input, ref uint remaining)
238
224
{
239
- auto op = cast (OpCode) input.read! ubyte ();
225
+ const op = input.read! OpCode ();
240
226
remaining-- ;
241
227
switch (op)
242
228
{
243
229
case OpCode.If:
244
- auto block = input.decodeBlockSection(remaining);
230
+ const block = input.decodeBlockSection(remaining);
245
231
return Instruction (If(block));
246
232
case OpCode.Return:
247
233
return Instruction (Return());
248
234
case OpCode.LocalGet:
249
- auto idx = input.leb128! uint ();
235
+ const idx = input.leb128! uint ();
250
236
remaining-- ;
251
237
return Instruction (LocalGet(idx));
252
238
case OpCode.LocalSet:
253
- auto idx = input.leb128! uint ();
239
+ const idx = input.leb128! uint ();
254
240
remaining-- ;
255
241
return Instruction (LocalSet(idx));
256
242
case OpCode.I32Store:
257
- auto align_ = input.leb128! uint ();
258
- auto offset = input.leb128! uint ();
243
+ const align_ = input.leb128! uint ();
244
+ const offset = input.leb128! uint ();
259
245
remaining -= 2 ;
260
246
return Instruction (I32Store(align_, offset));
261
247
case OpCode.I32Const:
262
- auto value = input.leb128! int ();
248
+ const value = input.leb128! int ();
263
249
remaining-- ;
264
250
return Instruction (I32Const(value));
265
251
case OpCode.I32LtS:
@@ -271,7 +257,7 @@ Instruction decodeInstruction(ref const(ubyte)[] input, ref uint remaining)
271
257
case OpCode.End:
272
258
return Instruction (End());
273
259
case OpCode.Call:
274
- auto idx = input.leb128! uint ();
260
+ const idx = input.leb128! uint ();
275
261
remaining-- ;
276
262
return Instruction (Call(idx));
277
263
default :
@@ -298,36 +284,40 @@ Block decodeBlockSection(ref const(ubyte)[] input, ref uint remaining)
298
284
}
299
285
300
286
// /
301
- Export[] decodeExportSection (ref const (ubyte )[] input)
287
+ const ( Export) [] decodeExportSection (ref const (ubyte )[] input)
302
288
{
303
289
const count = input.leb128! uint ();
304
- Export[] exports;
290
+ const ( Export) [] exports;
305
291
foreach (_; 0 .. count)
306
292
{
307
- string name = input.decodeName();
293
+ const name = input.decodeName();
308
294
const exportKind = input.read! ubyte ();
309
295
enforce(exportKind == 0x0 , " unsupported export kind" );
310
- auto idx = input.leb128! uint ();
296
+ const idx = input.leb128! uint ();
311
297
ExportDesc desc = Func(idx);
312
- exports ~= Export(name, desc);
298
+ exports ~= Export(name: name, desc: desc);
313
299
}
314
300
return exports;
315
301
}
316
302
317
303
// /
318
- Import[] decodeImportSection (ref const (ubyte )[] input)
304
+ const ( Import) [] decodeImportSection (ref const (ubyte )[] input)
319
305
{
320
306
const count = input.leb128! uint ();
321
- Import[] imports;
307
+ const ( Import) [] imports;
322
308
foreach (_; 0 .. count)
323
309
{
324
- auto moduleName = input.decodeName();
325
- auto field = input.decodeName();
326
- auto importKind = input.read! ubyte ();
310
+ const moduleName = input.decodeName();
311
+ const field = input.decodeName();
312
+ const importKind = input.read! ubyte ();
327
313
enforce(importKind == 0x00 , " unsupported import kind" );
328
314
auto idx = input.leb128! uint ();
329
315
ImportDesc desc = Func(idx);
330
- imports ~= Import(moduleName, field, desc);
316
+ imports ~= Import(
317
+ moduleName: moduleName,
318
+ field: field,
319
+ desc: desc
320
+ );
331
321
}
332
322
return imports;
333
323
}
0 commit comments