Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

apply.js 9.1KB

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