apply.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. var debug = require("debug")("webassemblyjs:wasm");
  15. function shiftLocNodeByDelta(node, delta) {
  16. (0, _ast.assertHasLoc)(node); // $FlowIgnore: assertHasLoc ensures that
  17. node.loc.start.column += delta; // $FlowIgnore: assertHasLoc ensures that
  18. node.loc.end.column += delta;
  19. }
  20. function applyUpdate(ast, uint8Buffer, _ref) {
  21. var _ref2 = _slicedToArray(_ref, 2),
  22. oldNode = _ref2[0],
  23. newNode = _ref2[1];
  24. var deltaElements = 0;
  25. (0, _ast.assertHasLoc)(oldNode);
  26. var sectionName = (0, _helperWasmBytecode.getSectionForNode)(newNode);
  27. var replacementByteArray = (0, _wasmGen.encodeNode)(newNode);
  28. /**
  29. * Replace new node as bytes
  30. */
  31. uint8Buffer = (0, _helperBuffer.overrideBytesInBuffer)(uint8Buffer, // $FlowIgnore: assertHasLoc ensures that
  32. oldNode.loc.start.column, // $FlowIgnore: assertHasLoc ensures that
  33. oldNode.loc.end.column, replacementByteArray);
  34. /**
  35. * Update function body size if needed
  36. */
  37. if (sectionName === "code") {
  38. // Find the parent func
  39. (0, _ast.traverse)(ast, {
  40. Func: function Func(_ref3) {
  41. var node = _ref3.node;
  42. var funcHasThisIntr = node.body.find(function (n) {
  43. return n === newNode;
  44. }) !== undefined; // Update func's body size if needed
  45. if (funcHasThisIntr === true) {
  46. // These are the old functions locations informations
  47. (0, _ast.assertHasLoc)(node);
  48. var oldNodeSize = (0, _wasmGen.encodeNode)(oldNode).length;
  49. var bodySizeDeltaBytes = replacementByteArray.length - oldNodeSize;
  50. if (bodySizeDeltaBytes !== 0) {
  51. var newValue = node.metadata.bodySize + bodySizeDeltaBytes;
  52. var newByteArray = (0, _encoder.encodeU32)(newValue);
  53. debug("resize func body newValue=%d", newValue); // function body size byte
  54. // FIXME(sven): only handles one byte u32
  55. var start = node.loc.start.column;
  56. var end = start + 1;
  57. uint8Buffer = (0, _helperBuffer.overrideBytesInBuffer)(uint8Buffer, start, end, newByteArray);
  58. }
  59. }
  60. }
  61. });
  62. }
  63. /**
  64. * Update section size
  65. */
  66. var deltaBytes = replacementByteArray.length - ( // $FlowIgnore: assertHasLoc ensures that
  67. oldNode.loc.end.column - oldNode.loc.start.column); // Init location informations
  68. newNode.loc = {
  69. start: {
  70. line: -1,
  71. column: -1
  72. },
  73. end: {
  74. line: -1,
  75. column: -1
  76. }
  77. }; // Update new node end position
  78. // $FlowIgnore: assertHasLoc ensures that
  79. newNode.loc.start.column = oldNode.loc.start.column; // $FlowIgnore: assertHasLoc ensures that
  80. newNode.loc.end.column = // $FlowIgnore: assertHasLoc ensures that
  81. oldNode.loc.start.column + replacementByteArray.length;
  82. return {
  83. uint8Buffer: uint8Buffer,
  84. deltaBytes: deltaBytes,
  85. deltaElements: deltaElements
  86. };
  87. }
  88. function applyDelete(ast, uint8Buffer, node) {
  89. var deltaElements = -1; // since we removed an element
  90. (0, _ast.assertHasLoc)(node);
  91. var sectionName = (0, _helperWasmBytecode.getSectionForNode)(node);
  92. if (sectionName === "start") {
  93. var sectionMetadata = (0, _ast.getSectionMetadata)(ast, "start");
  94. /**
  95. * The start section only contains one element,
  96. * we need to remove the whole section
  97. */
  98. uint8Buffer = (0, _helperWasmSection.removeSection)(ast, uint8Buffer, "start");
  99. var _deltaBytes = -(sectionMetadata.size.value + 1)
  100. /* section id */
  101. ;
  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. * Add nodes
  134. */
  135. var newByteArray = (0, _wasmGen.encodeNode)(node); // The size of the section doesn't include the storage of the size itself
  136. // we need to manually add it here
  137. var start = (0, _ast.getEndOfSection)(sectionMetadata);
  138. var end = start;
  139. /**
  140. * Update section
  141. */
  142. var deltaBytes = newByteArray.length;
  143. debug("add node=%s section=%s after=%d deltaBytes=%s deltaElements=%s", node.type, sectionName, start, deltaBytes, deltaElements);
  144. uint8Buffer = (0, _helperBuffer.overrideBytesInBuffer)(uint8Buffer, start, end, newByteArray);
  145. node.loc = {
  146. start: {
  147. line: -1,
  148. column: start
  149. },
  150. end: {
  151. line: -1,
  152. column: start + deltaBytes
  153. }
  154. }; // for func add the additional metadata in the AST
  155. if (node.type === "Func") {
  156. // the size is the first byte
  157. // FIXME(sven): handle LEB128 correctly here
  158. var bodySize = newByteArray[0];
  159. node.metadata = {
  160. bodySize: bodySize
  161. };
  162. }
  163. if (node.type !== "IndexInFuncSection") {
  164. (0, _ast.orderedInsertNode)(ast.body[0], node);
  165. }
  166. return {
  167. uint8Buffer: uint8Buffer,
  168. deltaBytes: deltaBytes,
  169. deltaElements: deltaElements
  170. };
  171. }
  172. function applyOperations(ast, uint8Buffer, ops) {
  173. ops.forEach(function (op) {
  174. var state;
  175. var sectionName;
  176. switch (op.kind) {
  177. case "update":
  178. state = applyUpdate(ast, uint8Buffer, [op.oldNode, op.node]);
  179. sectionName = (0, _helperWasmBytecode.getSectionForNode)(op.node);
  180. break;
  181. case "delete":
  182. state = applyDelete(ast, uint8Buffer, op.node);
  183. sectionName = (0, _helperWasmBytecode.getSectionForNode)(op.node);
  184. break;
  185. case "add":
  186. state = applyAdd(ast, uint8Buffer, op.node);
  187. sectionName = (0, _helperWasmBytecode.getSectionForNode)(op.node);
  188. break;
  189. default:
  190. throw new Error("Unknown operation");
  191. }
  192. /**
  193. * Shift following operation's nodes
  194. */
  195. if (state.deltaBytes !== 0) {
  196. ops.forEach(function (op) {
  197. // We don't need to handle add ops, they are positioning independent
  198. switch (op.kind) {
  199. case "update":
  200. shiftLocNodeByDelta(op.oldNode, state.deltaBytes);
  201. break;
  202. case "delete":
  203. shiftLocNodeByDelta(op.node, state.deltaBytes);
  204. break;
  205. }
  206. });
  207. if (sectionName !== "start") {
  208. state.uint8Buffer = (0, _helperWasmSection.resizeSectionByteSize)(ast, state.uint8Buffer, sectionName, state.deltaBytes);
  209. }
  210. }
  211. if (state.deltaElements !== 0 && sectionName !== "start") {
  212. state.uint8Buffer = (0, _helperWasmSection.resizeSectionVecSize)(ast, state.uint8Buffer, sectionName, state.deltaElements);
  213. }
  214. uint8Buffer = state.uint8Buffer;
  215. });
  216. return uint8Buffer;
  217. }