apply.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.applyOperations = applyOperations;
  6. var _wasmGen = require("@webassemblyjs/wasm-gen");
  7. var _encoder = require("@webassemblyjs/wasm-gen/lib/encoder");
  8. var _ast = require("@webassemblyjs/ast");
  9. var _helperWasmSection = require("@webassemblyjs/helper-wasm-section");
  10. var _helperBuffer = require("@webassemblyjs/helper-buffer");
  11. var _helperWasmBytecode = require("@webassemblyjs/helper-wasm-bytecode");
  12. function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
  13. 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."); }
  14. 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); }
  15. 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; }
  16. 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; }
  17. function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
  18. function shiftLocNodeByDelta(node, delta) {
  19. (0, _ast.assertHasLoc)(node); // $FlowIgnore: assertHasLoc ensures that
  20. node.loc.start.column += delta; // $FlowIgnore: assertHasLoc ensures that
  21. node.loc.end.column += delta;
  22. }
  23. function applyUpdate(ast, uint8Buffer, _ref) {
  24. var _ref2 = _slicedToArray(_ref, 2),
  25. oldNode = _ref2[0],
  26. newNode = _ref2[1];
  27. var deltaElements = 0;
  28. (0, _ast.assertHasLoc)(oldNode);
  29. var sectionName = (0, _helperWasmBytecode.getSectionForNode)(newNode);
  30. var replacementByteArray = (0, _wasmGen.encodeNode)(newNode);
  31. /**
  32. * Replace new node as bytes
  33. */
  34. uint8Buffer = (0, _helperBuffer.overrideBytesInBuffer)(uint8Buffer, // $FlowIgnore: assertHasLoc ensures that
  35. oldNode.loc.start.column, // $FlowIgnore: assertHasLoc ensures that
  36. oldNode.loc.end.column, replacementByteArray);
  37. /**
  38. * Update function body size if needed
  39. */
  40. if (sectionName === "code") {
  41. // Find the parent func
  42. (0, _ast.traverse)(ast, {
  43. Func: function Func(_ref3) {
  44. var node = _ref3.node;
  45. var funcHasThisIntr = node.body.find(function (n) {
  46. return n === newNode;
  47. }) !== undefined; // Update func's body size if needed
  48. if (funcHasThisIntr === true) {
  49. // These are the old functions locations informations
  50. (0, _ast.assertHasLoc)(node);
  51. var oldNodeSize = (0, _wasmGen.encodeNode)(oldNode).length;
  52. var bodySizeDeltaBytes = replacementByteArray.length - oldNodeSize;
  53. if (bodySizeDeltaBytes !== 0) {
  54. var newValue = node.metadata.bodySize + bodySizeDeltaBytes;
  55. var newByteArray = (0, _encoder.encodeU32)(newValue); // function body size byte
  56. // FIXME(sven): only handles one byte u32
  57. var start = node.loc.start.column;
  58. var end = start + 1;
  59. uint8Buffer = (0, _helperBuffer.overrideBytesInBuffer)(uint8Buffer, start, end, newByteArray);
  60. }
  61. }
  62. }
  63. });
  64. }
  65. /**
  66. * Update section size
  67. */
  68. var deltaBytes = replacementByteArray.length - (oldNode.loc.end.column - oldNode.loc.start.column); // Init location informations
  69. newNode.loc = {
  70. start: {
  71. line: -1,
  72. column: -1
  73. },
  74. end: {
  75. line: -1,
  76. column: -1
  77. }
  78. }; // Update new node end position
  79. // $FlowIgnore: assertHasLoc ensures that
  80. newNode.loc.start.column = oldNode.loc.start.column; // $FlowIgnore: assertHasLoc ensures that
  81. newNode.loc.end.column = // $FlowIgnore: assertHasLoc ensures that
  82. oldNode.loc.start.column + replacementByteArray.length;
  83. return {
  84. uint8Buffer: uint8Buffer,
  85. deltaBytes: deltaBytes,
  86. deltaElements: deltaElements
  87. };
  88. }
  89. function applyDelete(ast, uint8Buffer, node) {
  90. var deltaElements = -1; // since we removed an element
  91. (0, _ast.assertHasLoc)(node);
  92. var sectionName = (0, _helperWasmBytecode.getSectionForNode)(node);
  93. if (sectionName === "start") {
  94. var sectionMetadata = (0, _ast.getSectionMetadata)(ast, "start");
  95. /**
  96. * The start section only contains one element,
  97. * we need to remove the whole section
  98. */
  99. uint8Buffer = (0, _helperWasmSection.removeSections)(ast, uint8Buffer, "start");
  100. var _deltaBytes = -(sectionMetadata.size.value + 1);
  101. /* section id */
  102. return {
  103. uint8Buffer: uint8Buffer,
  104. deltaBytes: _deltaBytes,
  105. deltaElements: deltaElements
  106. };
  107. } // replacement is nothing
  108. var replacement = [];
  109. uint8Buffer = (0, _helperBuffer.overrideBytesInBuffer)(uint8Buffer, // $FlowIgnore: assertHasLoc ensures that
  110. node.loc.start.column, // $FlowIgnore: assertHasLoc ensures that
  111. node.loc.end.column, replacement);
  112. /**
  113. * Update section
  114. */
  115. // $FlowIgnore: assertHasLoc ensures that
  116. var deltaBytes = -(node.loc.end.column - node.loc.start.column);
  117. return {
  118. uint8Buffer: uint8Buffer,
  119. deltaBytes: deltaBytes,
  120. deltaElements: deltaElements
  121. };
  122. }
  123. function applyAdd(ast, uint8Buffer, node) {
  124. var deltaElements = +1; // since we added an element
  125. var sectionName = (0, _helperWasmBytecode.getSectionForNode)(node);
  126. var sectionMetadata = (0, _ast.getSectionMetadata)(ast, sectionName); // Section doesn't exists, we create an empty one
  127. if (typeof sectionMetadata === "undefined") {
  128. var res = (0, _helperWasmSection.createEmptySection)(ast, uint8Buffer, sectionName);
  129. uint8Buffer = res.uint8Buffer;
  130. sectionMetadata = res.sectionMetadata;
  131. }
  132. /**
  133. * check that the expressions were ended
  134. */
  135. if ((0, _ast.isFunc)(node)) {
  136. // $FlowIgnore
  137. var body = node.body;
  138. if (body.length === 0 || body[body.length - 1].id !== "end") {
  139. throw new Error("expressions must be ended");
  140. }
  141. }
  142. if ((0, _ast.isGlobal)(node)) {
  143. // $FlowIgnore
  144. var body = node.init;
  145. if (body.length === 0 || body[body.length - 1].id !== "end") {
  146. throw new Error("expressions must be ended");
  147. }
  148. }
  149. /**
  150. * Add nodes
  151. */
  152. var newByteArray = (0, _wasmGen.encodeNode)(node); // The size of the section doesn't include the storage of the size itself
  153. // we need to manually add it here
  154. var start = (0, _ast.getEndOfSection)(sectionMetadata);
  155. var end = start;
  156. /**
  157. * Update section
  158. */
  159. var deltaBytes = newByteArray.length;
  160. uint8Buffer = (0, _helperBuffer.overrideBytesInBuffer)(uint8Buffer, start, end, newByteArray);
  161. node.loc = {
  162. start: {
  163. line: -1,
  164. column: start
  165. },
  166. end: {
  167. line: -1,
  168. column: start + deltaBytes
  169. }
  170. }; // for func add the additional metadata in the AST
  171. if (node.type === "Func") {
  172. // the size is the first byte
  173. // FIXME(sven): handle LEB128 correctly here
  174. var bodySize = newByteArray[0];
  175. node.metadata = {
  176. bodySize: bodySize
  177. };
  178. }
  179. if (node.type !== "IndexInFuncSection") {
  180. (0, _ast.orderedInsertNode)(ast.body[0], node);
  181. }
  182. return {
  183. uint8Buffer: uint8Buffer,
  184. deltaBytes: deltaBytes,
  185. deltaElements: deltaElements
  186. };
  187. }
  188. function applyOperations(ast, uint8Buffer, ops) {
  189. ops.forEach(function (op) {
  190. var state;
  191. var sectionName;
  192. switch (op.kind) {
  193. case "update":
  194. state = applyUpdate(ast, uint8Buffer, [op.oldNode, op.node]);
  195. sectionName = (0, _helperWasmBytecode.getSectionForNode)(op.node);
  196. break;
  197. case "delete":
  198. state = applyDelete(ast, uint8Buffer, op.node);
  199. sectionName = (0, _helperWasmBytecode.getSectionForNode)(op.node);
  200. break;
  201. case "add":
  202. state = applyAdd(ast, uint8Buffer, op.node);
  203. sectionName = (0, _helperWasmBytecode.getSectionForNode)(op.node);
  204. break;
  205. default:
  206. throw new Error("Unknown operation");
  207. }
  208. /**
  209. * Resize section vec size.
  210. * If the length of the LEB-encoded size changes, this can change
  211. * the byte length of the section and the offset for nodes in the
  212. * section. So we do this first before resizing section byte size
  213. * or shifting following operations' nodes.
  214. */
  215. if (state.deltaElements !== 0 && sectionName !== "start") {
  216. var oldBufferLength = state.uint8Buffer.length;
  217. state.uint8Buffer = (0, _helperWasmSection.resizeSectionVecSize)(ast, state.uint8Buffer, sectionName, state.deltaElements); // Infer bytes added/removed by comparing buffer lengths
  218. state.deltaBytes += state.uint8Buffer.length - oldBufferLength;
  219. }
  220. /**
  221. * Resize section byte size.
  222. * If the length of the LEB-encoded size changes, this can change
  223. * the offset for nodes in the section. So we do this before
  224. * shifting following operations' nodes.
  225. */
  226. if (state.deltaBytes !== 0 && sectionName !== "start") {
  227. var _oldBufferLength = state.uint8Buffer.length;
  228. state.uint8Buffer = (0, _helperWasmSection.resizeSectionByteSize)(ast, state.uint8Buffer, sectionName, state.deltaBytes); // Infer bytes added/removed by comparing buffer lengths
  229. state.deltaBytes += state.uint8Buffer.length - _oldBufferLength;
  230. }
  231. /**
  232. * Shift following operation's nodes
  233. */
  234. if (state.deltaBytes !== 0) {
  235. ops.forEach(function (op) {
  236. // We don't need to handle add ops, they are positioning independent
  237. switch (op.kind) {
  238. case "update":
  239. shiftLocNodeByDelta(op.oldNode, state.deltaBytes);
  240. break;
  241. case "delete":
  242. shiftLocNodeByDelta(op.node, state.deltaBytes);
  243. break;
  244. }
  245. });
  246. }
  247. uint8Buffer = state.uint8Buffer;
  248. });
  249. return uint8Buffer;
  250. }