source: trip-planner-front/node_modules/@webassemblyjs/wast-printer/esm/index.js@ 8d391a1

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

initial commit

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