index.js 7.1 KB

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