index.js
9.51 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.moduleContextFromModuleAST = moduleContextFromModuleAST;
exports.ModuleContext = void 0;
var _ast = require("@webassemblyjs/ast");
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function moduleContextFromModuleAST(m) {
var moduleContext = new ModuleContext();
if (!(m.type === "Module")) {
throw new Error('m.type === "Module"' + " error: " + (undefined || "unknown"));
}
m.fields.forEach(function (field) {
switch (field.type) {
case "Start":
{
moduleContext.setStart(field.index);
break;
}
case "TypeInstruction":
{
moduleContext.addType(field);
break;
}
case "Func":
{
moduleContext.addFunction(field);
break;
}
case "Global":
{
moduleContext.defineGlobal(field);
break;
}
case "ModuleImport":
{
switch (field.descr.type) {
case "GlobalType":
{
moduleContext.importGlobal(field.descr.valtype, field.descr.mutability);
break;
}
case "Memory":
{
moduleContext.addMemory(field.descr.limits.min, field.descr.limits.max);
break;
}
case "FuncImportDescr":
{
moduleContext.importFunction(field.descr);
break;
}
case "Table":
{
// FIXME(sven): not implemented yet
break;
}
default:
throw new Error("Unsupported ModuleImport of type " + JSON.stringify(field.descr.type));
}
break;
}
case "Memory":
{
moduleContext.addMemory(field.limits.min, field.limits.max);
break;
}
}
});
return moduleContext;
}
/**
* Module context for type checking
*/
var ModuleContext =
/*#__PURE__*/
function () {
function ModuleContext() {
_classCallCheck(this, ModuleContext);
this.funcs = [];
this.funcsOffsetByIdentifier = [];
this.types = [];
this.globals = [];
this.globalsOffsetByIdentifier = [];
this.mems = []; // Current stack frame
this.locals = [];
this.labels = [];
this.return = [];
this.debugName = "unknown";
this.start = null;
}
/**
* Set start segment
*/
_createClass(ModuleContext, [{
key: "setStart",
value: function setStart(index) {
this.start = index.value;
}
/**
* Get start function
*/
}, {
key: "getStart",
value: function getStart() {
return this.start;
}
/**
* Reset the active stack frame
*/
}, {
key: "newContext",
value: function newContext(debugName, expectedResult) {
this.locals = [];
this.labels = [expectedResult];
this.return = expectedResult;
this.debugName = debugName;
}
/**
* Functions
*/
}, {
key: "addFunction",
value: function addFunction(func
/*: Func*/
) {
// eslint-disable-next-line prefer-const
var _ref = func.signature || {},
_ref$params = _ref.params,
args = _ref$params === void 0 ? [] : _ref$params,
_ref$results = _ref.results,
result = _ref$results === void 0 ? [] : _ref$results;
args = args.map(function (arg) {
return arg.valtype;
});
this.funcs.push({
args: args,
result: result
});
if (typeof func.name !== "undefined") {
this.funcsOffsetByIdentifier[func.name.value] = this.funcs.length - 1;
}
}
}, {
key: "importFunction",
value: function importFunction(funcimport) {
if ((0, _ast.isSignature)(funcimport.signature)) {
// eslint-disable-next-line prefer-const
var _funcimport$signature = funcimport.signature,
args = _funcimport$signature.params,
result = _funcimport$signature.results;
args = args.map(function (arg) {
return arg.valtype;
});
this.funcs.push({
args: args,
result: result
});
} else {
if (!(0, _ast.isNumberLiteral)(funcimport.signature)) {
throw new Error('isNumberLiteral(funcimport.signature)' + " error: " + (undefined || "unknown"));
}
var typeId = funcimport.signature.value;
if (!this.hasType(typeId)) {
throw new Error('this.hasType(typeId)' + " error: " + (undefined || "unknown"));
}
var signature = this.getType(typeId);
this.funcs.push({
args: signature.params.map(function (arg) {
return arg.valtype;
}),
result: signature.results
});
}
if (typeof funcimport.id !== "undefined") {
// imports are first, we can assume their index in the array
this.funcsOffsetByIdentifier[funcimport.id.value] = this.funcs.length - 1;
}
}
}, {
key: "hasFunction",
value: function hasFunction(index) {
return typeof this.getFunction(index) !== "undefined";
}
}, {
key: "getFunction",
value: function getFunction(index) {
if (typeof index !== "number") {
throw new Error("getFunction only supported for number index");
}
return this.funcs[index];
}
}, {
key: "getFunctionOffsetByIdentifier",
value: function getFunctionOffsetByIdentifier(name) {
if (!(typeof name === "string")) {
throw new Error('typeof name === "string"' + " error: " + (undefined || "unknown"));
}
return this.funcsOffsetByIdentifier[name];
}
/**
* Labels
*/
}, {
key: "addLabel",
value: function addLabel(result) {
this.labels.unshift(result);
}
}, {
key: "hasLabel",
value: function hasLabel(index) {
return this.labels.length > index && index >= 0;
}
}, {
key: "getLabel",
value: function getLabel(index) {
return this.labels[index];
}
}, {
key: "popLabel",
value: function popLabel() {
this.labels.shift();
}
/**
* Locals
*/
}, {
key: "hasLocal",
value: function hasLocal(index) {
return typeof this.getLocal(index) !== "undefined";
}
}, {
key: "getLocal",
value: function getLocal(index) {
return this.locals[index];
}
}, {
key: "addLocal",
value: function addLocal(type) {
this.locals.push(type);
}
/**
* Types
*/
}, {
key: "addType",
value: function addType(type) {
if (!(type.functype.type === "Signature")) {
throw new Error('type.functype.type === "Signature"' + " error: " + (undefined || "unknown"));
}
this.types.push(type.functype);
}
}, {
key: "hasType",
value: function hasType(index) {
return this.types[index] !== undefined;
}
}, {
key: "getType",
value: function getType(index) {
return this.types[index];
}
/**
* Globals
*/
}, {
key: "hasGlobal",
value: function hasGlobal(index) {
return this.globals.length > index && index >= 0;
}
}, {
key: "getGlobal",
value: function getGlobal(index) {
return this.globals[index].type;
}
}, {
key: "getGlobalOffsetByIdentifier",
value: function getGlobalOffsetByIdentifier(name) {
if (!(typeof name === "string")) {
throw new Error('typeof name === "string"' + " error: " + (undefined || "unknown"));
}
return this.globalsOffsetByIdentifier[name];
}
}, {
key: "defineGlobal",
value: function defineGlobal(global
/*: Global*/
) {
var type = global.globalType.valtype;
var mutability = global.globalType.mutability;
this.globals.push({
type: type,
mutability: mutability
});
if (typeof global.name !== "undefined") {
this.globalsOffsetByIdentifier[global.name.value] = this.globals.length - 1;
}
}
}, {
key: "importGlobal",
value: function importGlobal(type, mutability) {
this.globals.push({
type: type,
mutability: mutability
});
}
}, {
key: "isMutableGlobal",
value: function isMutableGlobal(index) {
return this.globals[index].mutability === "var";
}
}, {
key: "isImmutableGlobal",
value: function isImmutableGlobal(index) {
return this.globals[index].mutability === "const";
}
/**
* Memories
*/
}, {
key: "hasMemory",
value: function hasMemory(index) {
return this.mems.length > index && index >= 0;
}
}, {
key: "addMemory",
value: function addMemory(min, max) {
this.mems.push({
min: min,
max: max
});
}
}, {
key: "getMemory",
value: function getMemory(index) {
return this.mems[index];
}
}]);
return ModuleContext;
}();
exports.ModuleContext = ModuleContext;