-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWasmReader.cs
151 lines (122 loc) · 5.23 KB
/
WasmReader.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using ModuleSaw;
using Wasm.Model;
namespace WasmStrip {
public class WasmReader {
public readonly BinaryReader Input;
public TypeSection Types;
public ImportSection Imports;
public FunctionSection Functions;
public TableSection Tables;
public GlobalSection Globals;
public ExportSection Exports;
public ElementSection Elements;
public CodeSection Code;
public Action<function_body, BinaryReader> FunctionBodyCallback = null;
public readonly List<SectionHeader> SectionHeaders = new List<SectionHeader>();
public readonly Dictionary<uint, string> FunctionNames = new Dictionary<uint, string>();
public WasmReader (BinaryReader input) {
Input = input;
}
private uint? _ImportedFunctionCount,
_ExportedFunctionCount;
public uint FunctionIndexOffset {
get {
return ImportedFunctionCount;
}
}
public uint ImportedFunctionCount {
get {
if (!_ImportedFunctionCount.HasValue)
_ImportedFunctionCount = (uint)Imports.entries.Count(e => e.kind == external_kind.Function);
return _ImportedFunctionCount.Value;
}
}
public uint ExportedFunctionCount {
get {
if (!_ExportedFunctionCount.HasValue)
_ExportedFunctionCount = (uint)Exports.entries.Count(e => e.kind == external_kind.Function);
return _ExportedFunctionCount.Value;
}
}
public void Read () {
var reader = new ModuleReader(Input);
Program.Assert(reader.ReadHeader(), "ReadHeader");
SectionHeader sh;
while (reader.ReadSectionHeader(out sh)) {
if (sh.StreamPayloadEnd > reader.Reader.BaseStream.Length)
throw new Exception("Invalid header");
SectionHeaders.Add(sh);
switch (sh.id) {
case SectionTypes.Type:
Program.Assert(reader.ReadTypeSection(out Types));
break;
case SectionTypes.Import:
Program.Assert(reader.ReadImportSection(out Imports));
break;
case SectionTypes.Function:
Program.Assert(reader.ReadFunctionSection(out Functions));
break;
case SectionTypes.Table:
// FIXME: Not tested
Program.Assert(reader.ReadTableSection(out Tables));
break;
case SectionTypes.Global:
Program.Assert(reader.ReadGlobalSection(out Globals));
break;
case SectionTypes.Export:
Program.Assert(reader.ReadExportSection(out Exports));
break;
case SectionTypes.Element:
Program.Assert(reader.ReadElementSection(out Elements));
break;
case SectionTypes.Code:
Program.Assert(reader.ReadCodeSection(out Code, FunctionBodyCallback));
break;
case SectionTypes.Data:
DataSection ds;
Program.Assert(reader.ReadDataSection(out ds));
Input.BaseStream.Seek(sh.StreamPayloadEnd, SeekOrigin.Begin);
break;
case SectionTypes.Custom:
if (sh.name == "name")
ReadNameSection(reader, Input, sh);
Input.BaseStream.Seek(sh.StreamPayloadEnd, SeekOrigin.Begin);
break;
default:
Input.BaseStream.Seek(sh.StreamPayloadEnd, SeekOrigin.Begin);
break;
}
}
}
private void ReadNameSection (ModuleReader reader, BinaryReader sr, SectionHeader nameSectionHeader) {
var bs = sr.BaseStream;
while ((bs.Position < nameSectionHeader.StreamPayloadEnd) && (bs.Position < bs.Length)) {
var id = reader.Reader.ReadByte();
var size = (uint)reader.Reader.ReadLEBUInt();
switch (id) {
// Function names
case 1:
reader.ReadList((i) => {
var idx = (uint)reader.Reader.ReadLEBUInt();
var name = reader.Reader.ReadPString();
FunctionNames.Add(idx, name);
return (object)null;
});
break;
// Module name
case 0:
// Local names
case 2:
default:
sr.BaseStream.Seek(size, SeekOrigin.Current);
break;
}
}
}
}
}