25f112f6558d2a641495af3fb2baa3f67cb0547b98a4efbf5200ec32488b667c4b7417d70c65352260f3a89c5256549fbe62c3b1b48afa8954293297394154 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.transform = transform;
  6. var t = require("../../index"); // func and call_indirect instructions can either define a signature inline, or
  7. // reference a signature, e.g.
  8. //
  9. // ;; inline signature
  10. // (func (result i64)
  11. // (i64.const 2)
  12. // )
  13. // ;; signature reference
  14. // (type (func (result i64)))
  15. // (func (type 0)
  16. // (i64.const 2))
  17. // )
  18. //
  19. // this AST transform denormalises the type references, making all signatures within the module
  20. // inline.
  21. function transform(ast) {
  22. var typeInstructions = [];
  23. t.traverse(ast, {
  24. TypeInstruction: function TypeInstruction(_ref) {
  25. var node = _ref.node;
  26. typeInstructions.push(node);
  27. }
  28. });
  29. if (!typeInstructions.length) {
  30. return;
  31. }
  32. function denormalizeSignature(signature) {
  33. // signature referenced by identifier
  34. if (signature.type === "Identifier") {
  35. var identifier = signature;
  36. var typeInstruction = typeInstructions.find(function (t) {
  37. return t.id.type === identifier.type && t.id.value === identifier.value;
  38. });
  39. if (!typeInstruction) {
  40. throw new Error("A type instruction reference was not found ".concat(JSON.stringify(signature)));
  41. }
  42. return typeInstruction.functype;
  43. } // signature referenced by index
  44. if (signature.type === "NumberLiteral") {
  45. var signatureRef = signature;
  46. var _typeInstruction = typeInstructions[signatureRef.value];
  47. return _typeInstruction.functype;
  48. }
  49. return signature;
  50. }
  51. t.traverse(ast, {
  52. Func: function (_Func) {
  53. function Func(_x) {
  54. return _Func.apply(this, arguments);
  55. }
  56. Func.toString = function () {
  57. return _Func.toString();
  58. };
  59. return Func;
  60. }(function (_ref2) {
  61. var node = _ref2.node;
  62. node.signature = denormalizeSignature(node.signature);
  63. }),
  64. CallIndirectInstruction: function CallIndirectInstruction(_ref3) {
  65. var node = _ref3.node;
  66. node.signature = denormalizeSignature(node.signature);
  67. }
  68. });
  69. }