You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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