source: trip-planner-front/node_modules/@webassemblyjs/wast-printer/lib/index.js@ 1ad8e64

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

initial commit

  • Property mode set to 100644
File size: 17.4 KB
RevLine 
[6a3a178]1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.print = print;
7
8var _ast = require("@webassemblyjs/ast");
9
10var _long = _interopRequireDefault(require("@xtuc/long"));
11
12function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
14function _typeof(obj) { 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); }
15
16function _sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
17
18function _slicedToArray(arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return _sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }
19
20var compact = false;
21var space = " ";
22
23var quote = function quote(str) {
24 return "\"".concat(str, "\"");
25};
26
27function indent(nb) {
28 return Array(nb).fill(space + space).join("");
29} // TODO(sven): allow arbitrary ast nodes
30
31
32function print(n) {
33 if (n.type === "Program") {
34 return printProgram(n, 0);
35 } else {
36 throw new Error("Unsupported node in print of type: " + String(n.type));
37 }
38}
39
40function printProgram(n, depth) {
41 return n.body.reduce(function (acc, child) {
42 if (child.type === "Module") {
43 acc += printModule(child, depth + 1);
44 }
45
46 if (child.type === "Func") {
47 acc += printFunc(child, depth + 1);
48 }
49
50 if (child.type === "BlockComment") {
51 acc += printBlockComment(child);
52 }
53
54 if (child.type === "LeadingComment") {
55 acc += printLeadingComment(child);
56 }
57
58 if (compact === false) {
59 acc += "\n";
60 }
61
62 return acc;
63 }, "");
64}
65
66function printTypeInstruction(n) {
67 var out = "";
68 out += "(";
69 out += "type";
70 out += space;
71
72 if (n.id != null) {
73 out += printIndex(n.id);
74 out += space;
75 }
76
77 out += "(";
78 out += "func";
79 n.functype.params.forEach(function (param) {
80 out += space;
81 out += "(";
82 out += "param";
83 out += space;
84 out += printFuncParam(param);
85 out += ")";
86 });
87 n.functype.results.forEach(function (result) {
88 out += space;
89 out += "(";
90 out += "result";
91 out += space;
92 out += result;
93 out += ")";
94 });
95 out += ")"; // func
96
97 out += ")";
98 return out;
99}
100
101function printModule(n, depth) {
102 var out = "(";
103 out += "module";
104
105 if (typeof n.id === "string") {
106 out += space;
107 out += n.id;
108 }
109
110 if (compact === false) {
111 out += "\n";
112 } else {
113 out += space;
114 }
115
116 n.fields.forEach(function (field) {
117 if (compact === false) {
118 out += indent(depth);
119 }
120
121 switch (field.type) {
122 case "Func":
123 {
124 out += printFunc(field, depth + 1);
125 break;
126 }
127
128 case "TypeInstruction":
129 {
130 out += printTypeInstruction(field);
131 break;
132 }
133
134 case "Table":
135 {
136 out += printTable(field);
137 break;
138 }
139
140 case "Global":
141 {
142 out += printGlobal(field, depth + 1);
143 break;
144 }
145
146 case "ModuleExport":
147 {
148 out += printModuleExport(field);
149 break;
150 }
151
152 case "ModuleImport":
153 {
154 out += printModuleImport(field);
155 break;
156 }
157
158 case "Memory":
159 {
160 out += printMemory(field);
161 break;
162 }
163
164 case "BlockComment":
165 {
166 out += printBlockComment(field);
167 break;
168 }
169
170 case "LeadingComment":
171 {
172 out += printLeadingComment(field);
173 break;
174 }
175
176 case "Start":
177 {
178 out += printStart(field);
179 break;
180 }
181
182 case "Elem":
183 {
184 out += printElem(field, depth);
185 break;
186 }
187
188 case "Data":
189 {
190 out += printData(field, depth);
191 break;
192 }
193
194 default:
195 throw new Error("Unsupported node in printModule: " + String(field.type));
196 }
197
198 if (compact === false) {
199 out += "\n";
200 }
201 });
202 out += ")";
203 return out;
204}
205
206function printData(n, depth) {
207 var out = "";
208 out += "(";
209 out += "data";
210 out += space;
211 out += printIndex(n.memoryIndex);
212 out += space;
213 out += printInstruction(n.offset, depth);
214 out += space;
215 out += '"';
216 n.init.values.forEach(function (byte) {
217 // Avoid non-displayable characters
218 if (byte <= 31 || byte == 34 || byte == 92 || byte >= 127) {
219 out += "\\";
220 out += ("00" + byte.toString(16)).substr(-2);
221 } else if (byte > 255) {
222 throw new Error("Unsupported byte in data segment: " + byte);
223 } else {
224 out += String.fromCharCode(byte);
225 }
226 });
227 out += '"';
228 out += ")";
229 return out;
230}
231
232function printElem(n, depth) {
233 var out = "";
234 out += "(";
235 out += "elem";
236 out += space;
237 out += printIndex(n.table);
238
239 var _n$offset = _slicedToArray(n.offset, 1),
240 firstOffset = _n$offset[0];
241
242 out += space;
243 out += "(";
244 out += "offset";
245 out += space;
246 out += printInstruction(firstOffset, depth);
247 out += ")";
248 n.funcs.forEach(function (func) {
249 out += space;
250 out += printIndex(func);
251 });
252 out += ")";
253 return out;
254}
255
256function printStart(n) {
257 var out = "";
258 out += "(";
259 out += "start";
260 out += space;
261 out += printIndex(n.index);
262 out += ")";
263 return out;
264}
265
266function printLeadingComment(n) {
267 // Don't print leading comments in compact mode
268 if (compact === true) {
269 return "";
270 }
271
272 var out = "";
273 out += ";;";
274 out += n.value;
275 out += "\n";
276 return out;
277}
278
279function printBlockComment(n) {
280 // Don't print block comments in compact mode
281 if (compact === true) {
282 return "";
283 }
284
285 var out = "";
286 out += "(;";
287 out += n.value;
288 out += ";)";
289 out += "\n";
290 return out;
291}
292
293function printSignature(n) {
294 var out = "";
295 n.params.forEach(function (param) {
296 out += space;
297 out += "(";
298 out += "param";
299 out += space;
300 out += printFuncParam(param);
301 out += ")";
302 });
303 n.results.forEach(function (result) {
304 out += space;
305 out += "(";
306 out += "result";
307 out += space;
308 out += result;
309 out += ")";
310 });
311 return out;
312}
313
314function printModuleImportDescr(n) {
315 var out = "";
316
317 if (n.type === "FuncImportDescr") {
318 out += "(";
319 out += "func";
320
321 if ((0, _ast.isAnonymous)(n.id) === false) {
322 out += space;
323 out += printIdentifier(n.id);
324 }
325
326 out += printSignature(n.signature);
327 out += ")";
328 }
329
330 if (n.type === "GlobalType") {
331 out += "(";
332 out += "global";
333 out += space;
334 out += printGlobalType(n);
335 out += ")";
336 }
337
338 if (n.type === "Table") {
339 out += printTable(n);
340 }
341
342 return out;
343}
344
345function printModuleImport(n) {
346 var out = "";
347 out += "(";
348 out += "import";
349 out += space;
350 out += quote(n.module);
351 out += space;
352 out += quote(n.name);
353 out += space;
354 out += printModuleImportDescr(n.descr);
355 out += ")";
356 return out;
357}
358
359function printGlobalType(n) {
360 var out = "";
361
362 if (n.mutability === "var") {
363 out += "(";
364 out += "mut";
365 out += space;
366 out += n.valtype;
367 out += ")";
368 } else {
369 out += n.valtype;
370 }
371
372 return out;
373}
374
375function printGlobal(n, depth) {
376 var out = "";
377 out += "(";
378 out += "global";
379 out += space;
380
381 if (n.name != null && (0, _ast.isAnonymous)(n.name) === false) {
382 out += printIdentifier(n.name);
383 out += space;
384 }
385
386 out += printGlobalType(n.globalType);
387 out += space;
388 n.init.forEach(function (i) {
389 out += printInstruction(i, depth + 1);
390 });
391 out += ")";
392 return out;
393}
394
395function printTable(n) {
396 var out = "";
397 out += "(";
398 out += "table";
399 out += space;
400
401 if (n.name != null && (0, _ast.isAnonymous)(n.name) === false) {
402 out += printIdentifier(n.name);
403 out += space;
404 }
405
406 out += printLimit(n.limits);
407 out += space;
408 out += n.elementType;
409 out += ")";
410 return out;
411}
412
413function printFuncParam(n) {
414 var out = "";
415
416 if (typeof n.id === "string") {
417 out += "$" + n.id;
418 out += space;
419 }
420
421 out += n.valtype;
422 return out;
423}
424
425function printFunc(n, depth) {
426 var out = "";
427 out += "(";
428 out += "func";
429
430 if (n.name != null) {
431 if (n.name.type === "Identifier" && (0, _ast.isAnonymous)(n.name) === false) {
432 out += space;
433 out += printIdentifier(n.name);
434 }
435 }
436
437 if (n.signature.type === "Signature") {
438 out += printSignature(n.signature);
439 } else {
440 var index = n.signature;
441 out += space;
442 out += "(";
443 out += "type";
444 out += space;
445 out += printIndex(index);
446 out += ")";
447 }
448
449 if (n.body.length > 0) {
450 // func is empty since we ignore the default end instruction
451 if (n.body.length === 1 && n.body[0].id === "end") {
452 out += ")";
453 return out;
454 }
455
456 if (compact === false) {
457 out += "\n";
458 }
459
460 n.body.forEach(function (i) {
461 if (i.id !== "end") {
462 out += indent(depth);
463 out += printInstruction(i, depth);
464
465 if (compact === false) {
466 out += "\n";
467 }
468 }
469 });
470 out += indent(depth - 1) + ")";
471 } else {
472 out += ")";
473 }
474
475 return out;
476}
477
478function printInstruction(n, depth) {
479 switch (n.type) {
480 case "Instr":
481 // $FlowIgnore
482 return printGenericInstruction(n, depth + 1);
483
484 case "BlockInstruction":
485 // $FlowIgnore
486 return printBlockInstruction(n, depth + 1);
487
488 case "IfInstruction":
489 // $FlowIgnore
490 return printIfInstruction(n, depth + 1);
491
492 case "CallInstruction":
493 // $FlowIgnore
494 return printCallInstruction(n, depth + 1);
495
496 case "CallIndirectInstruction":
497 // $FlowIgnore
498 return printCallIndirectIntruction(n, depth + 1);
499
500 case "LoopInstruction":
501 // $FlowIgnore
502 return printLoopInstruction(n, depth + 1);
503
504 default:
505 throw new Error("Unsupported instruction: " + JSON.stringify(n.type));
506 }
507}
508
509function printCallIndirectIntruction(n, depth) {
510 var out = "";
511 out += "(";
512 out += "call_indirect";
513
514 if (n.signature.type === "Signature") {
515 out += printSignature(n.signature);
516 } else if (n.signature.type === "Identifier") {
517 out += space;
518 out += "(";
519 out += "type";
520 out += space;
521 out += printIdentifier(n.signature);
522 out += ")";
523 } else {
524 throw new Error("CallIndirectInstruction: unsupported signature " + JSON.stringify(n.signature.type));
525 }
526
527 out += space;
528
529 if (n.intrs != null) {
530 // $FlowIgnore
531 n.intrs.forEach(function (i, index) {
532 // $FlowIgnore
533 out += printInstruction(i, depth + 1); // $FlowIgnore
534
535 if (index !== n.intrs.length - 1) {
536 out += space;
537 }
538 });
539 }
540
541 out += ")";
542 return out;
543}
544
545function printLoopInstruction(n, depth) {
546 var out = "";
547 out += "(";
548 out += "loop";
549
550 if (n.label != null && (0, _ast.isAnonymous)(n.label) === false) {
551 out += space;
552 out += printIdentifier(n.label);
553 }
554
555 if (typeof n.resulttype === "string") {
556 out += space;
557 out += "(";
558 out += "result";
559 out += space;
560 out += n.resulttype;
561 out += ")";
562 }
563
564 if (n.instr.length > 0) {
565 n.instr.forEach(function (e) {
566 if (compact === false) {
567 out += "\n";
568 }
569
570 out += indent(depth);
571 out += printInstruction(e, depth + 1);
572 });
573
574 if (compact === false) {
575 out += "\n";
576 out += indent(depth - 1);
577 }
578 }
579
580 out += ")";
581 return out;
582}
583
584function printCallInstruction(n, depth) {
585 var out = "";
586 out += "(";
587 out += "call";
588 out += space;
589 out += printIndex(n.index);
590
591 if (_typeof(n.instrArgs) === "object") {
592 // $FlowIgnore
593 n.instrArgs.forEach(function (arg) {
594 out += space;
595 out += printFuncInstructionArg(arg, depth + 1);
596 });
597 }
598
599 out += ")";
600 return out;
601}
602
603function printIfInstruction(n, depth) {
604 var out = "";
605 out += "(";
606 out += "if";
607
608 if (n.testLabel != null && (0, _ast.isAnonymous)(n.testLabel) === false) {
609 out += space;
610 out += printIdentifier(n.testLabel);
611 }
612
613 if (typeof n.result === "string") {
614 out += space;
615 out += "(";
616 out += "result";
617 out += space;
618 out += n.result;
619 out += ")";
620 }
621
622 if (n.test.length > 0) {
623 out += space;
624 n.test.forEach(function (i) {
625 out += printInstruction(i, depth + 1);
626 });
627 }
628
629 if (n.consequent.length > 0) {
630 if (compact === false) {
631 out += "\n";
632 }
633
634 out += indent(depth);
635 out += "(";
636 out += "then";
637 depth++;
638 n.consequent.forEach(function (i) {
639 if (compact === false) {
640 out += "\n";
641 }
642
643 out += indent(depth);
644 out += printInstruction(i, depth + 1);
645 });
646 depth--;
647
648 if (compact === false) {
649 out += "\n";
650 out += indent(depth);
651 }
652
653 out += ")";
654 } else {
655 if (compact === false) {
656 out += "\n";
657 out += indent(depth);
658 }
659
660 out += "(";
661 out += "then";
662 out += ")";
663 }
664
665 if (n.alternate.length > 0) {
666 if (compact === false) {
667 out += "\n";
668 }
669
670 out += indent(depth);
671 out += "(";
672 out += "else";
673 depth++;
674 n.alternate.forEach(function (i) {
675 if (compact === false) {
676 out += "\n";
677 }
678
679 out += indent(depth);
680 out += printInstruction(i, depth + 1);
681 });
682 depth--;
683
684 if (compact === false) {
685 out += "\n";
686 out += indent(depth);
687 }
688
689 out += ")";
690 } else {
691 if (compact === false) {
692 out += "\n";
693 out += indent(depth);
694 }
695
696 out += "(";
697 out += "else";
698 out += ")";
699 }
700
701 if (compact === false) {
702 out += "\n";
703 out += indent(depth - 1);
704 }
705
706 out += ")";
707 return out;
708}
709
710function printBlockInstruction(n, depth) {
711 var out = "";
712 out += "(";
713 out += "block";
714
715 if (n.label != null && (0, _ast.isAnonymous)(n.label) === false) {
716 out += space;
717 out += printIdentifier(n.label);
718 }
719
720 if (typeof n.result === "string") {
721 out += space;
722 out += "(";
723 out += "result";
724 out += space;
725 out += n.result;
726 out += ")";
727 }
728
729 if (n.instr.length > 0) {
730 n.instr.forEach(function (i) {
731 if (compact === false) {
732 out += "\n";
733 }
734
735 out += indent(depth);
736 out += printInstruction(i, depth + 1);
737 });
738
739 if (compact === false) {
740 out += "\n";
741 }
742
743 out += indent(depth - 1);
744 out += ")";
745 } else {
746 out += ")";
747 }
748
749 return out;
750}
751
752function printGenericInstruction(n, depth) {
753 var out = "";
754 out += "(";
755
756 if (typeof n.object === "string") {
757 out += n.object;
758 out += ".";
759 }
760
761 out += n.id;
762 n.args.forEach(function (arg) {
763 out += space;
764 out += printFuncInstructionArg(arg, depth + 1);
765 });
766 out += ")";
767 return out;
768}
769
770function printLongNumberLiteral(n) {
771 if (typeof n.raw === "string") {
772 return n.raw;
773 }
774
775 var _n$value = n.value,
776 low = _n$value.low,
777 high = _n$value.high;
778 var v = new _long.default(low, high);
779 return v.toString();
780}
781
782function printFloatLiteral(n) {
783 if (typeof n.raw === "string") {
784 return n.raw;
785 }
786
787 return String(n.value);
788}
789
790function printFuncInstructionArg(n, depth) {
791 var out = "";
792
793 if (n.type === "NumberLiteral") {
794 out += printNumberLiteral(n);
795 }
796
797 if (n.type === "LongNumberLiteral") {
798 out += printLongNumberLiteral(n);
799 }
800
801 if (n.type === "Identifier" && (0, _ast.isAnonymous)(n) === false) {
802 out += printIdentifier(n);
803 }
804
805 if (n.type === "ValtypeLiteral") {
806 out += n.name;
807 }
808
809 if (n.type === "FloatLiteral") {
810 out += printFloatLiteral(n);
811 }
812
813 if ((0, _ast.isInstruction)(n)) {
814 out += printInstruction(n, depth + 1);
815 }
816
817 return out;
818}
819
820function printNumberLiteral(n) {
821 if (typeof n.raw === "string") {
822 return n.raw;
823 }
824
825 return String(n.value);
826}
827
828function printModuleExport(n) {
829 var out = "";
830 out += "(";
831 out += "export";
832 out += space;
833 out += quote(n.name);
834
835 if (n.descr.exportType === "Func") {
836 out += space;
837 out += "(";
838 out += "func";
839 out += space;
840 out += printIndex(n.descr.id);
841 out += ")";
842 } else if (n.descr.exportType === "Global") {
843 out += space;
844 out += "(";
845 out += "global";
846 out += space;
847 out += printIndex(n.descr.id);
848 out += ")";
849 } else if (n.descr.exportType === "Memory" || n.descr.exportType === "Mem") {
850 out += space;
851 out += "(";
852 out += "memory";
853 out += space;
854 out += printIndex(n.descr.id);
855 out += ")";
856 } else if (n.descr.exportType === "Table") {
857 out += space;
858 out += "(";
859 out += "table";
860 out += space;
861 out += printIndex(n.descr.id);
862 out += ")";
863 } else {
864 throw new Error("printModuleExport: unknown type: " + n.descr.exportType);
865 }
866
867 out += ")";
868 return out;
869}
870
871function printIdentifier(n) {
872 return "$" + n.value;
873}
874
875function printIndex(n) {
876 if (n.type === "Identifier") {
877 return printIdentifier(n);
878 } else if (n.type === "NumberLiteral") {
879 return printNumberLiteral(n);
880 } else {
881 throw new Error("Unsupported index: " + n.type);
882 }
883}
884
885function printMemory(n) {
886 var out = "";
887 out += "(";
888 out += "memory";
889
890 if (n.id != null) {
891 out += space;
892 out += printIndex(n.id);
893 out += space;
894 }
895
896 out += printLimit(n.limits);
897 out += ")";
898 return out;
899}
900
901function printLimit(n) {
902 var out = "";
903 out += n.min + "";
904
905 if (n.max != null) {
906 out += space;
907 out += String(n.max);
908
909 if (n.shared === true) {
910 out += ' shared';
911 }
912 }
913
914 return out;
915}
Note: See TracBrowser for help on using the repository browser.