source: trip-planner-front/node_modules/@webassemblyjs/wasm-gen/lib/encoder/index.js

Last change on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 9.8 KB
RevLine 
[6a3a178]1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.encodeVersion = encodeVersion;
7exports.encodeHeader = encodeHeader;
8exports.encodeU32 = encodeU32;
9exports.encodeI32 = encodeI32;
10exports.encodeI64 = encodeI64;
11exports.encodeVec = encodeVec;
12exports.encodeValtype = encodeValtype;
13exports.encodeMutability = encodeMutability;
14exports.encodeUTF8Vec = encodeUTF8Vec;
15exports.encodeLimits = encodeLimits;
16exports.encodeModuleImport = encodeModuleImport;
17exports.encodeSectionMetadata = encodeSectionMetadata;
18exports.encodeCallInstruction = encodeCallInstruction;
19exports.encodeCallIndirectInstruction = encodeCallIndirectInstruction;
20exports.encodeModuleExport = encodeModuleExport;
21exports.encodeTypeInstruction = encodeTypeInstruction;
22exports.encodeInstr = encodeInstr;
23exports.encodeStringLiteral = encodeStringLiteral;
24exports.encodeGlobal = encodeGlobal;
25exports.encodeFuncBody = encodeFuncBody;
26exports.encodeIndexInFuncSection = encodeIndexInFuncSection;
27exports.encodeElem = encodeElem;
28
29var leb = _interopRequireWildcard(require("@webassemblyjs/leb128"));
30
31var ieee754 = _interopRequireWildcard(require("@webassemblyjs/ieee754"));
32
33var utf8 = _interopRequireWildcard(require("@webassemblyjs/utf8"));
34
35var _helperWasmBytecode = _interopRequireDefault(require("@webassemblyjs/helper-wasm-bytecode"));
36
37var _index = require("../index");
38
39function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
40
41function _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
43function _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
45function assertNotIdentifierNode(n) {
46 if (n.type === "Identifier") {
47 throw new Error("Unsupported node Identifier");
48 }
49}
50
51function encodeVersion(v) {
52 var bytes = _helperWasmBytecode.default.moduleVersion;
53 bytes[0] = v;
54 return bytes;
55}
56
57function encodeHeader() {
58 return _helperWasmBytecode.default.magicModuleHeader;
59}
60
61function encodeU32(v) {
62 var uint8view = new Uint8Array(leb.encodeU32(v));
63
64 var array = _toConsumableArray(uint8view);
65
66 return array;
67}
68
69function encodeI32(v) {
70 var uint8view = new Uint8Array(leb.encodeI32(v));
71
72 var array = _toConsumableArray(uint8view);
73
74 return array;
75}
76
77function encodeI64(v) {
78 var uint8view = new Uint8Array(leb.encodeI64(v));
79
80 var array = _toConsumableArray(uint8view);
81
82 return array;
83}
84
85function encodeVec(elements) {
86 var size = encodeU32(elements.length);
87 return _toConsumableArray(size).concat(_toConsumableArray(elements));
88}
89
90function 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
100function 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
110function encodeUTF8Vec(str) {
111 return encodeVec(utf8.encode(str));
112}
113
114function 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
130function 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
181function 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
203function 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
212function 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
224function 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
241function 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
252function 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
301function 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
311function encodeStringLiteral(n) {
312 return encodeUTF8Vec(n.value);
313}
314
315function 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
326function 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
339function encodeIndexInFuncSection(n) {
340 assertNotIdentifierNode(n.index); // $FlowIgnore
341
342 return encodeU32(n.index.value);
343}
344
345function 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}
Note: See TracBrowser for help on using the repository browser.