apply.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 { isFunc, isGlobal, 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. return {
  95. uint8Buffer: uint8Buffer,
  96. deltaBytes: _deltaBytes,
  97. deltaElements: deltaElements
  98. };
  99. } // replacement is nothing
  100. var replacement = [];
  101. uint8Buffer = overrideBytesInBuffer(uint8Buffer, // $FlowIgnore: assertHasLoc ensures that
  102. node.loc.start.column, // $FlowIgnore: assertHasLoc ensures that
  103. node.loc.end.column, replacement);
  104. /**
  105. * Update section
  106. */
  107. // $FlowIgnore: assertHasLoc ensures that
  108. var deltaBytes = -(node.loc.end.column - node.loc.start.column);
  109. return {
  110. uint8Buffer: uint8Buffer,
  111. deltaBytes: deltaBytes,
  112. deltaElements: deltaElements
  113. };
  114. }
  115. function applyAdd(ast, uint8Buffer, node) {
  116. var deltaElements = +1; // since we added an element
  117. var sectionName = getSectionForNode(node);
  118. var sectionMetadata = getSectionMetadata(ast, sectionName); // Section doesn't exists, we create an empty one
  119. if (typeof sectionMetadata === "undefined") {
  120. var res = createEmptySection(ast, uint8Buffer, sectionName);
  121. uint8Buffer = res.uint8Buffer;
  122. sectionMetadata = res.sectionMetadata;
  123. }
  124. /**
  125. * check that the expressions were ended
  126. */
  127. if (isFunc(node)) {
  128. // $FlowIgnore
  129. var body = node.body;
  130. if (body.length === 0 || body[body.length - 1].id !== "end") {
  131. throw new Error("expressions must be ended");
  132. }
  133. }
  134. if (isGlobal(node)) {
  135. // $FlowIgnore
  136. var body = node.init;
  137. if (body.length === 0 || body[body.length - 1].id !== "end") {
  138. throw new Error("expressions must be ended");
  139. }
  140. }
  141. /**
  142. * Add nodes
  143. */
  144. var newByteArray = encodeNode(node); // The size of the section doesn't include the storage of the size itself
  145. // we need to manually add it here
  146. var start = getEndOfSection(sectionMetadata);
  147. var end = start;
  148. /**
  149. * Update section
  150. */
  151. var deltaBytes = newByteArray.length;
  152. uint8Buffer = overrideBytesInBuffer(uint8Buffer, start, end, newByteArray);
  153. node.loc = {
  154. start: {
  155. line: -1,
  156. column: start
  157. },
  158. end: {
  159. line: -1,
  160. column: start + deltaBytes
  161. }
  162. }; // for func add the additional metadata in the AST
  163. if (node.type === "Func") {
  164. // the size is the first byte
  165. // FIXME(sven): handle LEB128 correctly here
  166. var bodySize = newByteArray[0];
  167. node.metadata = {
  168. bodySize: bodySize
  169. };
  170. }
  171. if (node.type !== "IndexInFuncSection") {
  172. orderedInsertNode(ast.body[0], node);
  173. }
  174. return {
  175. uint8Buffer: uint8Buffer,
  176. deltaBytes: deltaBytes,
  177. deltaElements: deltaElements
  178. };
  179. }
  180. export function applyOperations(ast, uint8Buffer, ops) {
  181. ops.forEach(function (op) {
  182. var state;
  183. var sectionName;
  184. switch (op.kind) {
  185. case "update":
  186. state = applyUpdate(ast, uint8Buffer, [op.oldNode, op.node]);
  187. sectionName = getSectionForNode(op.node);
  188. break;
  189. case "delete":
  190. state = applyDelete(ast, uint8Buffer, op.node);
  191. sectionName = getSectionForNode(op.node);
  192. break;
  193. case "add":
  194. state = applyAdd(ast, uint8Buffer, op.node);
  195. sectionName = getSectionForNode(op.node);
  196. break;
  197. default:
  198. throw new Error("Unknown operation");
  199. }
  200. /**
  201. * Resize section vec size.
  202. * If the length of the LEB-encoded size changes, this can change
  203. * the byte length of the section and the offset for nodes in the
  204. * section. So we do this first before resizing section byte size
  205. * or shifting following operations' nodes.
  206. */
  207. if (state.deltaElements !== 0 && sectionName !== "start") {
  208. var oldBufferLength = state.uint8Buffer.length;
  209. state.uint8Buffer = resizeSectionVecSize(ast, state.uint8Buffer, sectionName, state.deltaElements); // Infer bytes added/removed by comparing buffer lengths
  210. state.deltaBytes += state.uint8Buffer.length - oldBufferLength;
  211. }
  212. /**
  213. * Resize section byte size.
  214. * If the length of the LEB-encoded size changes, this can change
  215. * the offset for nodes in the section. So we do this before
  216. * shifting following operations' nodes.
  217. */
  218. if (state.deltaBytes !== 0 && sectionName !== "start") {
  219. var _oldBufferLength = state.uint8Buffer.length;
  220. state.uint8Buffer = resizeSectionByteSize(ast, state.uint8Buffer, sectionName, state.deltaBytes); // Infer bytes added/removed by comparing buffer lengths
  221. state.deltaBytes += state.uint8Buffer.length - _oldBufferLength;
  222. }
  223. /**
  224. * Shift following operation's nodes
  225. */
  226. if (state.deltaBytes !== 0) {
  227. ops.forEach(function (op) {
  228. // We don't need to handle add ops, they are positioning independent
  229. switch (op.kind) {
  230. case "update":
  231. shiftLocNodeByDelta(op.oldNode, state.deltaBytes);
  232. break;
  233. case "delete":
  234. shiftLocNodeByDelta(op.node, state.deltaBytes);
  235. break;
  236. }
  237. });
  238. }
  239. uint8Buffer = state.uint8Buffer;
  240. });
  241. return uint8Buffer;
  242. }