apply.js 8.6 KB

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