Skip to content

Commit c78fb7b

Browse files
committed
Refactoring
1 parent 137d073 commit c78fb7b

File tree

5 files changed

+83
-90
lines changed

5 files changed

+83
-90
lines changed

source/binary/mod.d

+60-70
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@ struct Module
2121
///
2222
immutable uint version_ = 1;
2323
///
24-
Memory[] memorySection;
24+
const(Memory)[] memorySection;
2525
///
26-
Data[] dataSection;
26+
const(Data)[] dataSection;
2727
///
28-
FuncType[] typeSection;
28+
const(FuncType)[] typeSection;
2929
///
30-
uint[] functionSection;
30+
const(uint)[] functionSection;
3131
///
3232
Function[] codeSection;
3333
///
34-
Export[] exportSection;
34+
const(Export)[] exportSection;
3535
///
36-
Import[] importSection;
36+
const(Import)[] importSection;
3737
}
3838

3939
///
@@ -44,20 +44,14 @@ Module decodeModule(ref const(ubyte)[] input)
4444
enforce('a' == input.read!ubyte());
4545
enforce('s' == input.read!ubyte());
4646
enforce('m' == input.read!ubyte());
47-
auto version_ = input.read!(uint, Endian.littleEndian)();
47+
const version_ = input.read!(uint, Endian.littleEndian)();
4848
enforce(version_ == 1);
4949

50-
Memory[] memorySection;
51-
Data[] dataSection;
52-
FuncType[] typeSection;
53-
uint[] functionSection;
54-
Function[] codeSection;
55-
Export[] exportSection;
56-
Import[] importSection;
50+
Module mod;
5751

5852
while (input.length)
5953
{
60-
auto sectionHeader = decodeSectionHeader(input);
54+
const sectionHeader = decodeSectionHeader(input);
6155
switch (sectionHeader[0])
6256
{
6357
case SectionCode.Custom:
@@ -66,66 +60,58 @@ Module decodeModule(ref const(ubyte)[] input)
6660
break;
6761
case SectionCode.Memory:
6862
const memory = decodeMemorySection(input);
69-
memorySection = [memory];
63+
mod.memorySection = [memory];
7064
break;
7165
case SectionCode.Data:
72-
dataSection = decodeDataSection(input);
66+
mod.dataSection = decodeDataSection(input);
7367
break;
7468
case SectionCode.Type:
75-
typeSection = decodeTypeSection(input);
69+
mod.typeSection = decodeTypeSection(input);
7670
break;
7771
case SectionCode.Function:
78-
functionSection = decodeFunctionSection(input);
72+
mod.functionSection = decodeFunctionSection(input);
7973
break;
8074
case SectionCode.Code:
81-
codeSection = decodeCodeSection(input);
75+
mod.codeSection = decodeCodeSection(input);
8276
break;
8377
case SectionCode.Export:
84-
exportSection = decodeExportSection(input);
78+
mod.exportSection = decodeExportSection(input);
8579
break;
8680
case SectionCode.Import:
87-
importSection = decodeImportSection(input);
81+
mod.importSection = decodeImportSection(input);
8882
break;
8983
default:
9084
assert(false);
9185
}
9286
}
9387

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;
10389
}
10490

10591
private:
10692

10793
///
10894
Tuple!(SectionCode, uint) decodeSectionHeader(ref const(ubyte)[] input)
10995
{
110-
auto code = cast(SectionCode) input.read!ubyte();
96+
auto code = input.read!SectionCode();
11197
auto size = input.leb128!uint();
11298
return tuple(code, size);
11399
}
114100

115101
///
116-
Memory decodeMemorySection(ref const(ubyte)[] input)
102+
const(Memory) decodeMemorySection(ref const(ubyte)[] input)
117103
{
118104
input.leb128!uint();
119-
auto limits = decodeLimits(input);
105+
const limits = decodeLimits(input);
120106
return Memory(limits: limits);
121107
}
122108

123109
///
124110
Limits decodeLimits(ref const(ubyte)[] input)
125111
{
126112
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();
129115
return Limits(min: min, max: max);
130116
}
131117

@@ -139,16 +125,16 @@ uint decodeExpr(ref const(ubyte)[] input)
139125
}
140126

141127
///
142-
Data[] decodeDataSection(ref const(ubyte)[] input)
128+
const(Data)[] decodeDataSection(ref const(ubyte)[] input)
143129
{
144130
const count = input.leb128!uint();
145-
Data[] data;
131+
const(Data)[] data;
146132
foreach (_; 0..count)
147133
{
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];
152138
input.popFrontExactly(size);
153139
data ~= Data(
154140
memoryIndex: memoryIndex,
@@ -162,32 +148,32 @@ Data[] decodeDataSection(ref const(ubyte)[] input)
162148
///
163149
ValueType decodeValueSection(ref const(ubyte)[] input)
164150
{
165-
return cast(ValueType) input.read!ubyte();
151+
return input.read!ValueType();
166152
}
167153

168154
///
169-
FuncType[] decodeTypeSection(ref const(ubyte)[] input)
155+
const(FuncType)[] decodeTypeSection(ref const(ubyte)[] input)
170156
{
171-
FuncType[] funcTypes;
157+
const(FuncType)[] funcTypes;
172158
const count = input.leb128!uint();
173159
foreach (_; 0..count)
174160
{
175161
input.read!ubyte();
176162
uint size = input.leb128!uint();
177-
auto params = cast(ValueType[]) input[0..size];
163+
const params = cast(const(ValueType[])) input[0..size];
178164
input.popFrontExactly(size);
179165
size = input.leb128!uint();
180-
auto results = cast(ValueType[]) input[0..size];
166+
const results = cast(const(ValueType[])) input[0..size];
181167
input.popFrontExactly(size);
182-
funcTypes ~= FuncType(params, results);
168+
funcTypes ~= FuncType(params: params, results: results);
183169
}
184170
return funcTypes;
185171
}
186172

187173
///
188-
uint[] decodeFunctionSection(ref const(ubyte)[] input)
174+
const(uint)[] decodeFunctionSection(ref const(ubyte)[] input)
189175
{
190-
uint[] funcIdxList;
176+
const(uint)[] funcIdxList;
191177
const count = input.leb128!uint();
192178
foreach (_; 0..count)
193179
{
@@ -227,7 +213,7 @@ Function decodeFunctionBody(ref const(ubyte)[] input, ref uint remaining)
227213

228214
while (remaining > 0)
229215
{
230-
auto instruction = input.decodeInstruction(remaining);
216+
const instruction = input.decodeInstruction(remaining);
231217
body.code ~= instruction;
232218
}
233219
return body;
@@ -236,30 +222,30 @@ Function decodeFunctionBody(ref const(ubyte)[] input, ref uint remaining)
236222
///
237223
Instruction decodeInstruction(ref const(ubyte)[] input, ref uint remaining)
238224
{
239-
auto op = cast(OpCode) input.read!ubyte();
225+
const op = input.read!OpCode();
240226
remaining--;
241227
switch (op)
242228
{
243229
case OpCode.If:
244-
auto block = input.decodeBlockSection(remaining);
230+
const block = input.decodeBlockSection(remaining);
245231
return Instruction(If(block));
246232
case OpCode.Return:
247233
return Instruction(Return());
248234
case OpCode.LocalGet:
249-
auto idx = input.leb128!uint();
235+
const idx = input.leb128!uint();
250236
remaining--;
251237
return Instruction(LocalGet(idx));
252238
case OpCode.LocalSet:
253-
auto idx = input.leb128!uint();
239+
const idx = input.leb128!uint();
254240
remaining--;
255241
return Instruction(LocalSet(idx));
256242
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();
259245
remaining -= 2;
260246
return Instruction(I32Store(align_, offset));
261247
case OpCode.I32Const:
262-
auto value = input.leb128!int();
248+
const value = input.leb128!int();
263249
remaining--;
264250
return Instruction(I32Const(value));
265251
case OpCode.I32LtS:
@@ -271,7 +257,7 @@ Instruction decodeInstruction(ref const(ubyte)[] input, ref uint remaining)
271257
case OpCode.End:
272258
return Instruction(End());
273259
case OpCode.Call:
274-
auto idx = input.leb128!uint();
260+
const idx = input.leb128!uint();
275261
remaining--;
276262
return Instruction(Call(idx));
277263
default:
@@ -298,36 +284,40 @@ Block decodeBlockSection(ref const(ubyte)[] input, ref uint remaining)
298284
}
299285

300286
///
301-
Export[] decodeExportSection(ref const(ubyte)[] input)
287+
const(Export)[] decodeExportSection(ref const(ubyte)[] input)
302288
{
303289
const count = input.leb128!uint();
304-
Export[] exports;
290+
const(Export)[] exports;
305291
foreach (_; 0..count)
306292
{
307-
string name = input.decodeName();
293+
const name = input.decodeName();
308294
const exportKind = input.read!ubyte();
309295
enforce(exportKind == 0x0, "unsupported export kind");
310-
auto idx = input.leb128!uint();
296+
const idx = input.leb128!uint();
311297
ExportDesc desc = Func(idx);
312-
exports ~= Export(name, desc);
298+
exports ~= Export(name: name, desc: desc);
313299
}
314300
return exports;
315301
}
316302

317303
///
318-
Import[] decodeImportSection(ref const(ubyte)[] input)
304+
const(Import)[] decodeImportSection(ref const(ubyte)[] input)
319305
{
320306
const count = input.leb128!uint();
321-
Import[] imports;
307+
const(Import)[] imports;
322308
foreach (_; 0..count)
323309
{
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();
327313
enforce(importKind == 0x00, "unsupported import kind");
328314
auto idx = input.leb128!uint();
329315
ImportDesc desc = Func(idx);
330-
imports ~= Import(moduleName, field, desc);
316+
imports ~= Import(
317+
moduleName: moduleName,
318+
field: field,
319+
desc: desc
320+
);
331321
}
332322
return imports;
333323
}

source/binary/section.d

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ enum SectionCode : ubyte
2020
struct Function
2121
{
2222
///
23-
FunctionLocal[] locals;
23+
const(FunctionLocal)[] locals;
2424
///
25-
Instruction[] code;
25+
const(Instruction)[] code;
2626
}

source/binary/types.d

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import std.sumtype;
66
struct FuncType
77
{
88
///
9-
ValueType[] params;
9+
const(ValueType[]) params;
1010
///
11-
ValueType[] results;
11+
const(ValueType[]) results;
1212
}
1313

1414
///
@@ -82,7 +82,7 @@ struct Data
8282
///
8383
uint offset;
8484
///
85-
ubyte[] bytes;
85+
const(ubyte)[] bytes;
8686
}
8787

8888
///

source/execution/runtime.d

+5-5
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ private:
3434
{
3535
ptrdiff_t pc;
3636
size_t sp;
37-
Instruction[] insts;
37+
const(Instruction)[] insts;
3838
size_t arity;
3939
Label[] labels;
4040
Value[] locals;
@@ -277,12 +277,12 @@ public:
277277
///
278278
Nullable!Value call(string name, Value[] args)
279279
{
280-
ExportInst* p = name in this.store.moduleInst.exports;
280+
const ExportInst* p = name in this.store.moduleInst.exports;
281281
enforce(p !is null, "not found export function");
282-
auto idx = (*p).desc.match!(
282+
const idx = (*p).desc.match!(
283283
(binary.types.Func func) => func.idx
284284
);
285-
auto funcInst = this.store.funcs[idx];
285+
const funcInst = this.store.funcs[idx];
286286
foreach (arg; args)
287287
{
288288
this.stack ~= arg;
@@ -294,7 +294,7 @@ public:
294294
}
295295
}
296296

297-
private size_t getEndAddress(Instruction[] insts, size_t pc)
297+
private size_t getEndAddress(const(Instruction)[] insts, size_t pc)
298298
{
299299
uint depth = 0;
300300
while (true)

0 commit comments

Comments
 (0)