1
|
"use strict";
|
2
|
|
3
|
Object.defineProperty(exports, "__esModule", {
|
4
|
value: true
|
5
|
});
|
6
|
exports.encodeVersion = encodeVersion;
|
7
|
exports.encodeHeader = encodeHeader;
|
8
|
exports.encodeU32 = encodeU32;
|
9
|
exports.encodeI32 = encodeI32;
|
10
|
exports.encodeI64 = encodeI64;
|
11
|
exports.encodeVec = encodeVec;
|
12
|
exports.encodeValtype = encodeValtype;
|
13
|
exports.encodeMutability = encodeMutability;
|
14
|
exports.encodeUTF8Vec = encodeUTF8Vec;
|
15
|
exports.encodeLimits = encodeLimits;
|
16
|
exports.encodeModuleImport = encodeModuleImport;
|
17
|
exports.encodeSectionMetadata = encodeSectionMetadata;
|
18
|
exports.encodeCallInstruction = encodeCallInstruction;
|
19
|
exports.encodeCallIndirectInstruction = encodeCallIndirectInstruction;
|
20
|
exports.encodeModuleExport = encodeModuleExport;
|
21
|
exports.encodeTypeInstruction = encodeTypeInstruction;
|
22
|
exports.encodeInstr = encodeInstr;
|
23
|
exports.encodeStringLiteral = encodeStringLiteral;
|
24
|
exports.encodeGlobal = encodeGlobal;
|
25
|
exports.encodeFuncBody = encodeFuncBody;
|
26
|
exports.encodeIndexInFuncSection = encodeIndexInFuncSection;
|
27
|
exports.encodeElem = encodeElem;
|
28
|
|
29
|
var leb = _interopRequireWildcard(require("@webassemblyjs/leb128"));
|
30
|
|
31
|
var ieee754 = _interopRequireWildcard(require("@webassemblyjs/ieee754"));
|
32
|
|
33
|
var utf8 = _interopRequireWildcard(require("@webassemblyjs/utf8"));
|
34
|
|
35
|
var _helperWasmBytecode = _interopRequireDefault(require("@webassemblyjs/helper-wasm-bytecode"));
|
36
|
|
37
|
var _index = require("../index");
|
38
|
|
39
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
40
|
|
41
|
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
|
42
|
|
43
|
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
|
44
|
|
45
|
function assertNotIdentifierNode(n) {
|
46
|
if (n.type === "Identifier") {
|
47
|
throw new Error("Unsupported node Identifier");
|
48
|
}
|
49
|
}
|
50
|
|
51
|
function encodeVersion(v) {
|
52
|
var bytes = _helperWasmBytecode.default.moduleVersion;
|
53
|
bytes[0] = v;
|
54
|
return bytes;
|
55
|
}
|
56
|
|
57
|
function encodeHeader() {
|
58
|
return _helperWasmBytecode.default.magicModuleHeader;
|
59
|
}
|
60
|
|
61
|
function encodeU32(v) {
|
62
|
var uint8view = new Uint8Array(leb.encodeU32(v));
|
63
|
|
64
|
var array = _toConsumableArray(uint8view);
|
65
|
|
66
|
return array;
|
67
|
}
|
68
|
|
69
|
function encodeI32(v) {
|
70
|
var uint8view = new Uint8Array(leb.encodeI32(v));
|
71
|
|
72
|
var array = _toConsumableArray(uint8view);
|
73
|
|
74
|
return array;
|
75
|
}
|
76
|
|
77
|
function encodeI64(v) {
|
78
|
var uint8view = new Uint8Array(leb.encodeI64(v));
|
79
|
|
80
|
var array = _toConsumableArray(uint8view);
|
81
|
|
82
|
return array;
|
83
|
}
|
84
|
|
85
|
function encodeVec(elements) {
|
86
|
var size = encodeU32(elements.length);
|
87
|
return _toConsumableArray(size).concat(_toConsumableArray(elements));
|
88
|
}
|
89
|
|
90
|
function encodeValtype(v) {
|
91
|
var byte = _helperWasmBytecode.default.valtypesByString[v];
|
92
|
|
93
|
if (typeof byte === "undefined") {
|
94
|
throw new Error("Unknown valtype: " + v);
|
95
|
}
|
96
|
|
97
|
return parseInt(byte, 10);
|
98
|
}
|
99
|
|
100
|
function encodeMutability(v) {
|
101
|
var byte = _helperWasmBytecode.default.globalTypesByString[v];
|
102
|
|
103
|
if (typeof byte === "undefined") {
|
104
|
throw new Error("Unknown mutability: " + v);
|
105
|
}
|
106
|
|
107
|
return parseInt(byte, 10);
|
108
|
}
|
109
|
|
110
|
function encodeUTF8Vec(str) {
|
111
|
return encodeVec(utf8.encode(str));
|
112
|
}
|
113
|
|
114
|
function encodeLimits(n) {
|
115
|
var out = [];
|
116
|
|
117
|
if (typeof n.max === "number") {
|
118
|
out.push(0x01);
|
119
|
out.push.apply(out, _toConsumableArray(encodeU32(n.min))); // $FlowIgnore: ensured by the typeof
|
120
|
|
121
|
out.push.apply(out, _toConsumableArray(encodeU32(n.max)));
|
122
|
} else {
|
123
|
out.push(0x00);
|
124
|
out.push.apply(out, _toConsumableArray(encodeU32(n.min)));
|
125
|
}
|
126
|
|
127
|
return out;
|
128
|
}
|
129
|
|
130
|
function encodeModuleImport(n) {
|
131
|
var out = [];
|
132
|
out.push.apply(out, _toConsumableArray(encodeUTF8Vec(n.module)));
|
133
|
out.push.apply(out, _toConsumableArray(encodeUTF8Vec(n.name)));
|
134
|
|
135
|
switch (n.descr.type) {
|
136
|
case "GlobalType":
|
137
|
{
|
138
|
out.push(0x03); // $FlowIgnore: GlobalType ensure that these props exists
|
139
|
|
140
|
out.push(encodeValtype(n.descr.valtype)); // $FlowIgnore: GlobalType ensure that these props exists
|
141
|
|
142
|
out.push(encodeMutability(n.descr.mutability));
|
143
|
break;
|
144
|
}
|
145
|
|
146
|
case "Memory":
|
147
|
{
|
148
|
out.push(0x02); // $FlowIgnore
|
149
|
|
150
|
out.push.apply(out, _toConsumableArray(encodeLimits(n.descr.limits)));
|
151
|
break;
|
152
|
}
|
153
|
|
154
|
case "Table":
|
155
|
{
|
156
|
out.push(0x01);
|
157
|
out.push(0x70); // element type
|
158
|
// $FlowIgnore
|
159
|
|
160
|
out.push.apply(out, _toConsumableArray(encodeLimits(n.descr.limits)));
|
161
|
break;
|
162
|
}
|
163
|
|
164
|
case "FuncImportDescr":
|
165
|
{
|
166
|
out.push(0x00); // $FlowIgnore
|
167
|
|
168
|
assertNotIdentifierNode(n.descr.id); // $FlowIgnore
|
169
|
|
170
|
out.push.apply(out, _toConsumableArray(encodeU32(n.descr.id.value)));
|
171
|
break;
|
172
|
}
|
173
|
|
174
|
default:
|
175
|
throw new Error("Unsupport operation: encode module import of type: " + n.descr.type);
|
176
|
}
|
177
|
|
178
|
return out;
|
179
|
}
|
180
|
|
181
|
function encodeSectionMetadata(n) {
|
182
|
var out = [];
|
183
|
var sectionId = _helperWasmBytecode.default.sections[n.section];
|
184
|
|
185
|
if (typeof sectionId === "undefined") {
|
186
|
throw new Error("Unknown section: " + n.section);
|
187
|
}
|
188
|
|
189
|
if (n.section === "start") {
|
190
|
/**
|
191
|
* This is not implemented yet because it's a special case which
|
192
|
* doesn't have a vector in its section.
|
193
|
*/
|
194
|
throw new Error("Unsupported section encoding of type start");
|
195
|
}
|
196
|
|
197
|
out.push(sectionId);
|
198
|
out.push.apply(out, _toConsumableArray(encodeU32(n.size.value)));
|
199
|
out.push.apply(out, _toConsumableArray(encodeU32(n.vectorOfSize.value)));
|
200
|
return out;
|
201
|
}
|
202
|
|
203
|
function encodeCallInstruction(n) {
|
204
|
var out = [];
|
205
|
assertNotIdentifierNode(n.index);
|
206
|
out.push(0x10); // $FlowIgnore
|
207
|
|
208
|
out.push.apply(out, _toConsumableArray(encodeU32(n.index.value)));
|
209
|
return out;
|
210
|
}
|
211
|
|
212
|
function encodeCallIndirectInstruction(n) {
|
213
|
var out = []; // $FlowIgnore
|
214
|
|
215
|
assertNotIdentifierNode(n.index);
|
216
|
out.push(0x11); // $FlowIgnore
|
217
|
|
218
|
out.push.apply(out, _toConsumableArray(encodeU32(n.index.value))); // add a reserved byte
|
219
|
|
220
|
out.push(0x00);
|
221
|
return out;
|
222
|
}
|
223
|
|
224
|
function encodeModuleExport(n) {
|
225
|
var out = [];
|
226
|
assertNotIdentifierNode(n.descr.id);
|
227
|
var exportTypeByteString = _helperWasmBytecode.default.exportTypesByName[n.descr.exportType];
|
228
|
|
229
|
if (typeof exportTypeByteString === "undefined") {
|
230
|
throw new Error("Unknown export of type: " + n.descr.exportType);
|
231
|
}
|
232
|
|
233
|
var exportTypeByte = parseInt(exportTypeByteString, 10);
|
234
|
out.push.apply(out, _toConsumableArray(encodeUTF8Vec(n.name)));
|
235
|
out.push(exportTypeByte); // $FlowIgnore
|
236
|
|
237
|
out.push.apply(out, _toConsumableArray(encodeU32(n.descr.id.value)));
|
238
|
return out;
|
239
|
}
|
240
|
|
241
|
function encodeTypeInstruction(n) {
|
242
|
var out = [0x60];
|
243
|
var params = n.functype.params.map(function (x) {
|
244
|
return x.valtype;
|
245
|
}).map(encodeValtype);
|
246
|
var results = n.functype.results.map(encodeValtype);
|
247
|
out.push.apply(out, _toConsumableArray(encodeVec(params)));
|
248
|
out.push.apply(out, _toConsumableArray(encodeVec(results)));
|
249
|
return out;
|
250
|
}
|
251
|
|
252
|
function encodeInstr(n) {
|
253
|
var out = [];
|
254
|
var instructionName = n.id;
|
255
|
|
256
|
if (typeof n.object === "string") {
|
257
|
instructionName = "".concat(n.object, ".").concat(String(n.id));
|
258
|
}
|
259
|
|
260
|
var byteString = _helperWasmBytecode.default.symbolsByName[instructionName];
|
261
|
|
262
|
if (typeof byteString === "undefined") {
|
263
|
throw new Error("encodeInstr: unknown instruction " + JSON.stringify(instructionName));
|
264
|
}
|
265
|
|
266
|
var byte = parseInt(byteString, 10);
|
267
|
out.push(byte);
|
268
|
|
269
|
if (n.args) {
|
270
|
n.args.forEach(function (arg) {
|
271
|
var encoder = encodeU32; // find correct encoder
|
272
|
|
273
|
if (n.object === "i32") {
|
274
|
encoder = encodeI32;
|
275
|
}
|
276
|
|
277
|
if (n.object === "i64") {
|
278
|
encoder = encodeI64;
|
279
|
}
|
280
|
|
281
|
if (n.object === "f32") {
|
282
|
encoder = ieee754.encodeF32;
|
283
|
}
|
284
|
|
285
|
if (n.object === "f64") {
|
286
|
encoder = ieee754.encodeF64;
|
287
|
}
|
288
|
|
289
|
if (arg.type === "NumberLiteral" || arg.type === "FloatLiteral" || arg.type === "LongNumberLiteral") {
|
290
|
// $FlowIgnore
|
291
|
out.push.apply(out, _toConsumableArray(encoder(arg.value)));
|
292
|
} else {
|
293
|
throw new Error("Unsupported instruction argument encoding " + JSON.stringify(arg.type));
|
294
|
}
|
295
|
});
|
296
|
}
|
297
|
|
298
|
return out;
|
299
|
}
|
300
|
|
301
|
function encodeExpr(instrs) {
|
302
|
var out = [];
|
303
|
instrs.forEach(function (instr) {
|
304
|
// $FlowIgnore
|
305
|
var n = (0, _index.encodeNode)(instr);
|
306
|
out.push.apply(out, _toConsumableArray(n));
|
307
|
});
|
308
|
return out;
|
309
|
}
|
310
|
|
311
|
function encodeStringLiteral(n) {
|
312
|
return encodeUTF8Vec(n.value);
|
313
|
}
|
314
|
|
315
|
function encodeGlobal(n) {
|
316
|
var out = [];
|
317
|
var _n$globalType = n.globalType,
|
318
|
valtype = _n$globalType.valtype,
|
319
|
mutability = _n$globalType.mutability;
|
320
|
out.push(encodeValtype(valtype));
|
321
|
out.push(encodeMutability(mutability));
|
322
|
out.push.apply(out, _toConsumableArray(encodeExpr(n.init)));
|
323
|
return out;
|
324
|
}
|
325
|
|
326
|
function encodeFuncBody(n) {
|
327
|
var out = [];
|
328
|
out.push(-1); // temporary function body size
|
329
|
// FIXME(sven): get the func locals?
|
330
|
|
331
|
var localBytes = encodeVec([]);
|
332
|
out.push.apply(out, _toConsumableArray(localBytes));
|
333
|
var funcBodyBytes = encodeExpr(n.body);
|
334
|
out[0] = funcBodyBytes.length + localBytes.length;
|
335
|
out.push.apply(out, _toConsumableArray(funcBodyBytes));
|
336
|
return out;
|
337
|
}
|
338
|
|
339
|
function encodeIndexInFuncSection(n) {
|
340
|
assertNotIdentifierNode(n.index); // $FlowIgnore
|
341
|
|
342
|
return encodeU32(n.index.value);
|
343
|
}
|
344
|
|
345
|
function encodeElem(n) {
|
346
|
var out = [];
|
347
|
assertNotIdentifierNode(n.table); // $FlowIgnore
|
348
|
|
349
|
out.push.apply(out, _toConsumableArray(encodeU32(n.table.value)));
|
350
|
out.push.apply(out, _toConsumableArray(encodeExpr(n.offset))); // $FlowIgnore
|
351
|
|
352
|
var funcs = n.funcs.reduce(function (acc, x) {
|
353
|
return _toConsumableArray(acc).concat(_toConsumableArray(encodeU32(x.value)));
|
354
|
}, []);
|
355
|
out.push.apply(out, _toConsumableArray(encodeVec(funcs)));
|
356
|
return out;
|
357
|
}
|