apply.js 9.5 KB

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