-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
282 lines (265 loc) · 7.44 KB
/
index.js
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
// @ts-check
/**
* @typedef {import("./index.d.ts").ImportEntry } ImportEntry
* @typedef {import("./index.d.ts").FunctionType } FunctionType
* @typedef {import("./index.d.ts").TableType } TableType
* @typedef {import("./index.d.ts").MemoryType } MemoryType
* @typedef {import("./index.d.ts").ValueType } ValueType
*/
/**
* Parse a WebAssembly module bytes and return the imports entries.
*
* @param {BufferSource} moduleBytes - The WebAssembly module bytes.
* @returns {ImportEntry[]} - The import entries.
* @throws {Error} - If the module bytes are invalid.
*
* @example
* import { parseImports } from "wasm-imports-parser";
*
* function mockImports(imports) {
* let mock = {};
* for (const imp of imports) {
* let value;
* switch (imp.kind) {
* case "table":
* value = new WebAssembly.Table(imp.type);
* break;
* case "memory":
* value = new WebAssembly.Memory(imp.type);
* break;
* case "global":
* value = new WebAssembly.Global(imp.type, undefined);
* break;
* case "function":
* value = () => { throw "unimplemented" };
* break;
* }
* if (! (imp.module in mock)) mock[imp.module] = {};
* mock[imp.module][imp.name] = value;
* }
* return mock;
* }
*
* const imports = parseImports(moduleBytes);
* const importObject = mockImports(imports);
* const { instance } = await WebAssembly.instantiate(moduleBytes, importObject);
*/
export function parseImports(moduleBytes) {
if (moduleBytes instanceof Uint8Array) {
// no cast needed
} else if (moduleBytes instanceof ArrayBuffer) {
// cast ArrayBuffer to Uint8Array
moduleBytes = new Uint8Array(moduleBytes);
} else if (moduleBytes.buffer instanceof ArrayBuffer) {
// cast TypedArray or DataView to Uint8Array
moduleBytes = new Uint8Array(moduleBytes.buffer);
} else {
throw new Error("Argument must be a buffer source, like Uint8Array or ArrayBuffer");
}
const parseState = new ParseState(moduleBytes);
parseMagicNumber(parseState);
parseVersion(parseState);
/**
* @type {FunctionType[]}
*/
const types = [];
/**
* @type {ImportEntry[]}
*/
const imports = [];
while (parseState.hasMoreBytes()) {
const sectionId = parseState.readByte();
const sectionSize = parseState.readUnsignedLEB128();
switch (sectionId) {
case 1: {
// Type section
const typeCount = parseState.readUnsignedLEB128();
for (let i = 0; i < typeCount; i++) {
types.push(parseFunctionType(parseState));
}
break;
}
case 2: {
// Ok, found import section
const importCount = parseState.readUnsignedLEB128();
for (let i = 0; i < importCount; i++) {
const module = parseState.readName();
const name = parseState.readName();
const type = parseState.readByte();
switch (type) {
case 0x00:
const index = parseState.readUnsignedLEB128();
imports.push({ module, name, kind: "function", type: types[index] });
break;
case 0x01:
imports.push({ module, name, kind: "table", type: parseTableType(parseState) });
break;
case 0x02:
imports.push({ module, name, kind: "memory", type: parseLimits(parseState) });
break;
case 0x03:
imports.push({ module, name, kind: "global", type: parseGlobalType(parseState) });
break;
default:
throw new Error(`Unknown import descriptor type ${type}`);
}
}
// Skip the rest of the module
return imports;
}
default: {
parseState.skipBytes(sectionSize);
break;
}
}
}
return [];
}
class ParseState {
constructor(moduleBytes) {
this.moduleBytes = moduleBytes;
this.offset = 0;
this.textDecoder = new TextDecoder("utf-8");
}
hasMoreBytes() {
return this.offset < this.moduleBytes.length;
}
readByte() {
return this.moduleBytes[this.offset++];
}
skipBytes(count) {
this.offset += count;
}
/// Read unsigned LEB128 integer
readUnsignedLEB128() {
let result = 0;
let shift = 0;
let byte;
do {
byte = this.readByte();
result |= (byte & 0x7F) << shift;
shift += 7;
} while (byte & 0x80);
return result;
}
readName() {
const nameLength = this.readUnsignedLEB128();
const nameBytes = this.moduleBytes.slice(this.offset, this.offset + nameLength);
const name = this.textDecoder.decode(nameBytes);
this.offset += nameLength;
return name;
}
assertBytes(expected) {
const baseOffset = this.offset;
const expectedLength = expected.length;
for (let i = 0; i < expectedLength; i++) {
if (this.moduleBytes[baseOffset + i] !== expected[i]) {
throw new Error(`Expected ${expected} at offset ${baseOffset}`);
}
}
this.offset += expectedLength;
}
}
function parseMagicNumber(parseState) {
const expected = [0x00, 0x61, 0x73, 0x6D];
parseState.assertBytes(expected);
}
function parseVersion(parseState) {
const expected = [0x01, 0x00, 0x00, 0x00];
parseState.assertBytes(expected);
}
/**
* @returns {TableType}
*/
function parseTableType(parseState) {
const elementType = parseState.readByte();
/**
* @type {"funcref" | "externref"}
*/
let element;
switch (elementType) {
case 0x70:
element = "funcref";
break;
case 0x6F:
element = "externref";
break;
default:
throw new Error(`Unknown table element type ${elementType}`);
}
const { minimum, maximum } = parseLimits(parseState);
if (maximum) {
return { element, minimum, maximum };
} else {
return { element, minimum };
}
}
/**
* @returns {MemoryType}
*/
function parseLimits(parseState) {
const flags = parseState.readByte();
const minimum = parseState.readUnsignedLEB128();
const hasMaximum = flags & 1;
const shared = (flags & 2) !== 0;
const isMemory64 = (flags & 4) !== 0;
const index = isMemory64 ? "i64" : "i32";
if (hasMaximum) {
const maximum = parseState.readUnsignedLEB128();
return { minimum, shared, index, maximum };
} else {
return { minimum, shared, index };
}
}
function parseGlobalType(parseState) {
const value = parseValueType(parseState);
const mutable = parseState.readByte() === 1;
return { value, mutable };
}
/**
* @returns {ValueType}
*/
function parseValueType(parseState) {
const type = parseState.readByte();
switch (type) {
case 0x7F:
return "i32";
case 0x7E:
return "i64";
case 0x7D:
return "f32";
case 0x7C:
return "f64";
case 0x70:
return "funcref";
case 0x6f:
return "externref";
case 0x7B:
return "v128";
default:
throw new Error(`Unknown value type ${type}`);
}
}
function parseFunctionType(parseState) {
const form = parseState.readByte();
if (form !== 0x60) {
throw new Error(`Expected function type form 0x60, got ${form}`);
}
/**
* @type {ValueType[]}
*/
const parameters = [];
const parameterCount = parseState.readUnsignedLEB128();
for (let i = 0; i < parameterCount; i++) {
parameters.push(parseValueType(parseState));
}
/**
* @type {ValueType[]}
*/
const results = [];
const resultCount = parseState.readUnsignedLEB128();
for (let i = 0; i < resultCount; i++) {
results.push(parseValueType(parseState));
}
return { parameters, results };
}