source: imaps-frontend/node_modules/@webassemblyjs/wasm-gen/lib/encoder/index.js@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 11.9 KB
Line 
1"use strict";
2
3function _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
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports.encodeVersion = encodeVersion;
9exports.encodeHeader = encodeHeader;
10exports.encodeU32 = encodeU32;
11exports.encodeI32 = encodeI32;
12exports.encodeI64 = encodeI64;
13exports.encodeVec = encodeVec;
14exports.encodeValtype = encodeValtype;
15exports.encodeMutability = encodeMutability;
16exports.encodeUTF8Vec = encodeUTF8Vec;
17exports.encodeLimits = encodeLimits;
18exports.encodeModuleImport = encodeModuleImport;
19exports.encodeSectionMetadata = encodeSectionMetadata;
20exports.encodeCallInstruction = encodeCallInstruction;
21exports.encodeCallIndirectInstruction = encodeCallIndirectInstruction;
22exports.encodeModuleExport = encodeModuleExport;
23exports.encodeTypeInstruction = encodeTypeInstruction;
24exports.encodeInstr = encodeInstr;
25exports.encodeStringLiteral = encodeStringLiteral;
26exports.encodeGlobal = encodeGlobal;
27exports.encodeFuncBody = encodeFuncBody;
28exports.encodeIndexInFuncSection = encodeIndexInFuncSection;
29exports.encodeElem = encodeElem;
30
31var leb = _interopRequireWildcard(require("@webassemblyjs/leb128"));
32
33var ieee754 = _interopRequireWildcard(require("@webassemblyjs/ieee754"));
34
35var utf8 = _interopRequireWildcard(require("@webassemblyjs/utf8"));
36
37var _helperWasmBytecode = _interopRequireDefault(require("@webassemblyjs/helper-wasm-bytecode"));
38
39var _index = require("../index");
40
41function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
42
43function _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
45function _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
47function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
48
49function _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
51function _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
53function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
54
55function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
56
57function _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
59function assertNotIdentifierNode(n) {
60 if (n.type === "Identifier") {
61 throw new Error("Unsupported node Identifier");
62 }
63}
64
65function encodeVersion(v) {
66 var bytes = _helperWasmBytecode["default"].moduleVersion;
67 bytes[0] = v;
68 return bytes;
69}
70
71function encodeHeader() {
72 return _helperWasmBytecode["default"].magicModuleHeader;
73}
74
75function encodeU32(v) {
76 var uint8view = new Uint8Array(leb.encodeU32(v));
77
78 var array = _toConsumableArray(uint8view);
79
80 return array;
81}
82
83function encodeI32(v) {
84 var uint8view = new Uint8Array(leb.encodeI32(v));
85
86 var array = _toConsumableArray(uint8view);
87
88 return array;
89}
90
91function encodeI64(v) {
92 var uint8view = new Uint8Array(leb.encodeI64(v));
93
94 var array = _toConsumableArray(uint8view);
95
96 return array;
97}
98
99function encodeVec(elements) {
100 var size = encodeU32(elements.length);
101 return [].concat(_toConsumableArray(size), _toConsumableArray(elements));
102}
103
104function 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
114function 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
124function encodeUTF8Vec(str) {
125 return encodeVec(utf8.encode(str));
126}
127
128function 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
144function 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
195function 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
217function 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
226function 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
238function 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
255function 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
266function 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
316function 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
326function encodeStringLiteral(n) {
327 return encodeUTF8Vec(n.value);
328}
329
330function 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
341function 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
354function encodeIndexInFuncSection(n) {
355 assertNotIdentifierNode(n.index); // $FlowIgnore
356
357 return encodeU32(n.index.value);
358}
359
360function 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}
Note: See TracBrowser for help on using the repository browser.