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