index.js
11.9 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
"use strict";
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.encodeVersion = encodeVersion;
exports.encodeHeader = encodeHeader;
exports.encodeU32 = encodeU32;
exports.encodeI32 = encodeI32;
exports.encodeI64 = encodeI64;
exports.encodeVec = encodeVec;
exports.encodeValtype = encodeValtype;
exports.encodeMutability = encodeMutability;
exports.encodeUTF8Vec = encodeUTF8Vec;
exports.encodeLimits = encodeLimits;
exports.encodeModuleImport = encodeModuleImport;
exports.encodeSectionMetadata = encodeSectionMetadata;
exports.encodeCallInstruction = encodeCallInstruction;
exports.encodeCallIndirectInstruction = encodeCallIndirectInstruction;
exports.encodeModuleExport = encodeModuleExport;
exports.encodeTypeInstruction = encodeTypeInstruction;
exports.encodeInstr = encodeInstr;
exports.encodeStringLiteral = encodeStringLiteral;
exports.encodeGlobal = encodeGlobal;
exports.encodeFuncBody = encodeFuncBody;
exports.encodeIndexInFuncSection = encodeIndexInFuncSection;
exports.encodeElem = encodeElem;
var leb = _interopRequireWildcard(require("@webassemblyjs/leb128"));
var ieee754 = _interopRequireWildcard(require("@webassemblyjs/ieee754"));
var utf8 = _interopRequireWildcard(require("@webassemblyjs/utf8"));
var _helperWasmBytecode = _interopRequireDefault(require("@webassemblyjs/helper-wasm-bytecode"));
var _index = require("../index");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function assertNotIdentifierNode(n) {
if (n.type === "Identifier") {
throw new Error("Unsupported node Identifier");
}
}
function encodeVersion(v) {
var bytes = _helperWasmBytecode["default"].moduleVersion;
bytes[0] = v;
return bytes;
}
function encodeHeader() {
return _helperWasmBytecode["default"].magicModuleHeader;
}
function encodeU32(v) {
var uint8view = new Uint8Array(leb.encodeU32(v));
var array = _toConsumableArray(uint8view);
return array;
}
function encodeI32(v) {
var uint8view = new Uint8Array(leb.encodeI32(v));
var array = _toConsumableArray(uint8view);
return array;
}
function encodeI64(v) {
var uint8view = new Uint8Array(leb.encodeI64(v));
var array = _toConsumableArray(uint8view);
return array;
}
function encodeVec(elements) {
var size = encodeU32(elements.length);
return [].concat(_toConsumableArray(size), _toConsumableArray(elements));
}
function encodeValtype(v) {
var _byte = _helperWasmBytecode["default"].valtypesByString[v];
if (typeof _byte === "undefined") {
throw new Error("Unknown valtype: " + v);
}
return parseInt(_byte, 10);
}
function encodeMutability(v) {
var _byte2 = _helperWasmBytecode["default"].globalTypesByString[v];
if (typeof _byte2 === "undefined") {
throw new Error("Unknown mutability: " + v);
}
return parseInt(_byte2, 10);
}
function encodeUTF8Vec(str) {
return encodeVec(utf8.encode(str));
}
function encodeLimits(n) {
var out = [];
if (typeof n.max === "number") {
out.push(0x01);
out.push.apply(out, _toConsumableArray(encodeU32(n.min))); // $FlowIgnore: ensured by the typeof
out.push.apply(out, _toConsumableArray(encodeU32(n.max)));
} else {
out.push(0x00);
out.push.apply(out, _toConsumableArray(encodeU32(n.min)));
}
return out;
}
function encodeModuleImport(n) {
var out = [];
out.push.apply(out, _toConsumableArray(encodeUTF8Vec(n.module)));
out.push.apply(out, _toConsumableArray(encodeUTF8Vec(n.name)));
switch (n.descr.type) {
case "GlobalType":
{
out.push(0x03); // $FlowIgnore: GlobalType ensure that these props exists
out.push(encodeValtype(n.descr.valtype)); // $FlowIgnore: GlobalType ensure that these props exists
out.push(encodeMutability(n.descr.mutability));
break;
}
case "Memory":
{
out.push(0x02); // $FlowIgnore
out.push.apply(out, _toConsumableArray(encodeLimits(n.descr.limits)));
break;
}
case "Table":
{
out.push(0x01);
out.push(0x70); // element type
// $FlowIgnore
out.push.apply(out, _toConsumableArray(encodeLimits(n.descr.limits)));
break;
}
case "FuncImportDescr":
{
out.push(0x00); // $FlowIgnore
assertNotIdentifierNode(n.descr.id); // $FlowIgnore
out.push.apply(out, _toConsumableArray(encodeU32(n.descr.id.value)));
break;
}
default:
throw new Error("Unsupport operation: encode module import of type: " + n.descr.type);
}
return out;
}
function encodeSectionMetadata(n) {
var out = [];
var sectionId = _helperWasmBytecode["default"].sections[n.section];
if (typeof sectionId === "undefined") {
throw new Error("Unknown section: " + n.section);
}
if (n.section === "start") {
/**
* This is not implemented yet because it's a special case which
* doesn't have a vector in its section.
*/
throw new Error("Unsupported section encoding of type start");
}
out.push(sectionId);
out.push.apply(out, _toConsumableArray(encodeU32(n.size.value)));
out.push.apply(out, _toConsumableArray(encodeU32(n.vectorOfSize.value)));
return out;
}
function encodeCallInstruction(n) {
var out = [];
assertNotIdentifierNode(n.index);
out.push(0x10); // $FlowIgnore
out.push.apply(out, _toConsumableArray(encodeU32(n.index.value)));
return out;
}
function encodeCallIndirectInstruction(n) {
var out = []; // $FlowIgnore
assertNotIdentifierNode(n.index);
out.push(0x11); // $FlowIgnore
out.push.apply(out, _toConsumableArray(encodeU32(n.index.value))); // add a reserved byte
out.push(0x00);
return out;
}
function encodeModuleExport(n) {
var out = [];
assertNotIdentifierNode(n.descr.id);
var exportTypeByteString = _helperWasmBytecode["default"].exportTypesByName[n.descr.exportType];
if (typeof exportTypeByteString === "undefined") {
throw new Error("Unknown export of type: " + n.descr.exportType);
}
var exportTypeByte = parseInt(exportTypeByteString, 10);
out.push.apply(out, _toConsumableArray(encodeUTF8Vec(n.name)));
out.push(exportTypeByte); // $FlowIgnore
out.push.apply(out, _toConsumableArray(encodeU32(n.descr.id.value)));
return out;
}
function encodeTypeInstruction(n) {
var out = [0x60];
var params = n.functype.params.map(function (x) {
return x.valtype;
}).map(encodeValtype);
var results = n.functype.results.map(encodeValtype);
out.push.apply(out, _toConsumableArray(encodeVec(params)));
out.push.apply(out, _toConsumableArray(encodeVec(results)));
return out;
}
function encodeInstr(n) {
var out = [];
var instructionName = n.id;
if (typeof n.object === "string") {
instructionName = "".concat(n.object, ".").concat(String(n.id));
}
var byteString = _helperWasmBytecode["default"].symbolsByName[instructionName];
if (typeof byteString === "undefined") {
throw new Error("encodeInstr: unknown instruction " + JSON.stringify(instructionName));
}
var _byte3 = parseInt(byteString, 10);
out.push(_byte3);
if (n.args) {
n.args.forEach(function (arg) {
var encoder = encodeU32; // find correct encoder
if (n.object === "i32") {
encoder = encodeI32;
}
if (n.object === "i64") {
encoder = encodeI64;
}
if (n.object === "f32") {
encoder = ieee754.encodeF32;
}
if (n.object === "f64") {
encoder = ieee754.encodeF64;
}
if (arg.type === "NumberLiteral" || arg.type === "FloatLiteral" || arg.type === "LongNumberLiteral") {
// $FlowIgnore
out.push.apply(out, _toConsumableArray(encoder(arg.value)));
} else {
throw new Error("Unsupported instruction argument encoding " + JSON.stringify(arg.type));
}
});
}
return out;
}
function encodeExpr(instrs) {
var out = [];
instrs.forEach(function (instr) {
// $FlowIgnore
var n = (0, _index.encodeNode)(instr);
out.push.apply(out, _toConsumableArray(n));
});
return out;
}
function encodeStringLiteral(n) {
return encodeUTF8Vec(n.value);
}
function encodeGlobal(n) {
var out = [];
var _n$globalType = n.globalType,
valtype = _n$globalType.valtype,
mutability = _n$globalType.mutability;
out.push(encodeValtype(valtype));
out.push(encodeMutability(mutability));
out.push.apply(out, _toConsumableArray(encodeExpr(n.init)));
return out;
}
function encodeFuncBody(n) {
var out = [];
out.push(-1); // temporary function body size
// FIXME(sven): get the func locals?
var localBytes = encodeVec([]);
out.push.apply(out, _toConsumableArray(localBytes));
var funcBodyBytes = encodeExpr(n.body);
out[0] = funcBodyBytes.length + localBytes.length;
out.push.apply(out, _toConsumableArray(funcBodyBytes));
return out;
}
function encodeIndexInFuncSection(n) {
assertNotIdentifierNode(n.index); // $FlowIgnore
return encodeU32(n.index.value);
}
function encodeElem(n) {
var out = [];
assertNotIdentifierNode(n.table); // $FlowIgnore
out.push.apply(out, _toConsumableArray(encodeU32(n.table.value)));
out.push.apply(out, _toConsumableArray(encodeExpr(n.offset))); // $FlowIgnore
var funcs = n.funcs.reduce(function (acc, x) {
return [].concat(_toConsumableArray(acc), _toConsumableArray(encodeU32(x.value)));
}, []);
out.push.apply(out, _toConsumableArray(encodeVec(funcs)));
return out;
}