index.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.transform = transform;
  6. var _index = require("../../index");
  7. var _astModuleToModuleContext = require("../ast-module-to-module-context");
  8. function _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); }
  9. function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
  10. function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
  11. function _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); }
  12. function _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; }
  13. function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_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; }
  14. function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
  15. // FIXME(sven): do the same with all block instructions, must be more generic here
  16. function newUnexpectedFunction(i) {
  17. return new Error("unknown function at offset: " + i);
  18. }
  19. function transform(ast) {
  20. var module = null;
  21. (0, _index.traverse)(ast, {
  22. Module: function (_Module) {
  23. function Module(_x) {
  24. return _Module.apply(this, arguments);
  25. }
  26. Module.toString = function () {
  27. return _Module.toString();
  28. };
  29. return Module;
  30. }(function (path) {
  31. module = path.node;
  32. })
  33. });
  34. if (module == null) {
  35. throw new Error("Module not foudn in program");
  36. }
  37. var moduleContext = (0, _astModuleToModuleContext.moduleContextFromModuleAST)(module); // Transform the actual instruction in function bodies
  38. (0, _index.traverse)(ast, {
  39. Func: function (_Func) {
  40. function Func(_x2) {
  41. return _Func.apply(this, arguments);
  42. }
  43. Func.toString = function () {
  44. return _Func.toString();
  45. };
  46. return Func;
  47. }(function (path) {
  48. transformFuncPath(path, moduleContext);
  49. }),
  50. Start: function (_Start) {
  51. function Start(_x3) {
  52. return _Start.apply(this, arguments);
  53. }
  54. Start.toString = function () {
  55. return _Start.toString();
  56. };
  57. return Start;
  58. }(function (path) {
  59. var index = path.node.index;
  60. if ((0, _index.isIdentifier)(index) === true) {
  61. var offsetInModule = moduleContext.getFunctionOffsetByIdentifier(index.value);
  62. if (typeof offsetInModule === "undefined") {
  63. throw newUnexpectedFunction(index.value);
  64. } // Replace the index Identifier
  65. // $FlowIgnore: reference?
  66. path.node.index = (0, _index.numberLiteralFromRaw)(offsetInModule);
  67. }
  68. })
  69. });
  70. }
  71. function transformFuncPath(funcPath, moduleContext) {
  72. var funcNode = funcPath.node;
  73. var signature = funcNode.signature;
  74. if (signature.type !== "Signature") {
  75. throw new Error("Function signatures must be denormalised before execution");
  76. }
  77. var params = signature.params; // Add func locals in the context
  78. params.forEach(function (p) {
  79. return moduleContext.addLocal(p.valtype);
  80. });
  81. (0, _index.traverse)(funcNode, {
  82. Instr: function (_Instr) {
  83. function Instr(_x4) {
  84. return _Instr.apply(this, arguments);
  85. }
  86. Instr.toString = function () {
  87. return _Instr.toString();
  88. };
  89. return Instr;
  90. }(function (instrPath) {
  91. var instrNode = instrPath.node;
  92. /**
  93. * Local access
  94. */
  95. if (instrNode.id === "get_local" || instrNode.id === "set_local" || instrNode.id === "tee_local") {
  96. var _instrNode$args = _slicedToArray(instrNode.args, 1),
  97. firstArg = _instrNode$args[0];
  98. if (firstArg.type === "Identifier") {
  99. var offsetInParams = params.findIndex(function (_ref) {
  100. var id = _ref.id;
  101. return id === firstArg.value;
  102. });
  103. if (offsetInParams === -1) {
  104. throw new Error("".concat(firstArg.value, " not found in ").concat(instrNode.id, ": not declared in func params"));
  105. } // Replace the Identifer node by our new NumberLiteral node
  106. instrNode.args[0] = (0, _index.numberLiteralFromRaw)(offsetInParams);
  107. }
  108. }
  109. /**
  110. * Global access
  111. */
  112. if (instrNode.id === "get_global" || instrNode.id === "set_global") {
  113. var _instrNode$args2 = _slicedToArray(instrNode.args, 1),
  114. _firstArg = _instrNode$args2[0];
  115. if ((0, _index.isIdentifier)(_firstArg) === true) {
  116. var globalOffset = moduleContext.getGlobalOffsetByIdentifier( // $FlowIgnore: reference?
  117. _firstArg.value);
  118. if (typeof globalOffset === "undefined") {
  119. // $FlowIgnore: reference?
  120. throw new Error("global ".concat(_firstArg.value, " not found in module"));
  121. } // Replace the Identifer node by our new NumberLiteral node
  122. instrNode.args[0] = (0, _index.numberLiteralFromRaw)(globalOffset);
  123. }
  124. }
  125. /**
  126. * Labels lookup
  127. */
  128. if (instrNode.id === "br") {
  129. var _instrNode$args3 = _slicedToArray(instrNode.args, 1),
  130. _firstArg2 = _instrNode$args3[0];
  131. if ((0, _index.isIdentifier)(_firstArg2) === true) {
  132. // if the labels is not found it is going to be replaced with -1
  133. // which is invalid.
  134. var relativeBlockCount = -1; // $FlowIgnore: reference?
  135. instrPath.findParent(function (_ref2) {
  136. var node = _ref2.node;
  137. if ((0, _index.isBlock)(node)) {
  138. relativeBlockCount++; // $FlowIgnore: reference?
  139. var name = node.label || node.name;
  140. if (_typeof(name) === "object") {
  141. // $FlowIgnore: isIdentifier ensures that
  142. if (name.value === _firstArg2.value) {
  143. // Found it
  144. return false;
  145. }
  146. }
  147. }
  148. if ((0, _index.isFunc)(node)) {
  149. return false;
  150. }
  151. }); // Replace the Identifer node by our new NumberLiteral node
  152. instrNode.args[0] = (0, _index.numberLiteralFromRaw)(relativeBlockCount);
  153. }
  154. }
  155. }),
  156. /**
  157. * Func lookup
  158. */
  159. CallInstruction: function (_CallInstruction) {
  160. function CallInstruction(_x5) {
  161. return _CallInstruction.apply(this, arguments);
  162. }
  163. CallInstruction.toString = function () {
  164. return _CallInstruction.toString();
  165. };
  166. return CallInstruction;
  167. }(function (_ref3) {
  168. var node = _ref3.node;
  169. var index = node.index;
  170. if ((0, _index.isIdentifier)(index) === true) {
  171. var offsetInModule = moduleContext.getFunctionOffsetByIdentifier(index.value);
  172. if (typeof offsetInModule === "undefined") {
  173. throw newUnexpectedFunction(index.value);
  174. } // Replace the index Identifier
  175. // $FlowIgnore: reference?
  176. node.index = (0, _index.numberLiteralFromRaw)(offsetInModule);
  177. }
  178. })
  179. });
  180. }