apply.js 9.9 KB

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