index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.edit = edit;
  6. exports.editWithAST = editWithAST;
  7. exports.add = add;
  8. exports.addWithAST = addWithAST;
  9. var _wasmParser = require("@webassemblyjs/wasm-parser");
  10. var _ast = require("@webassemblyjs/ast");
  11. var _clone = require("@webassemblyjs/ast/lib/clone");
  12. var _wasmOpt = require("@webassemblyjs/wasm-opt");
  13. var _helperWasmBytecode = _interopRequireWildcard(require("@webassemblyjs/helper-wasm-bytecode"));
  14. var _apply = require("./apply");
  15. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
  16. function hashNode(node) {
  17. return JSON.stringify(node);
  18. }
  19. function preprocess(ab) {
  20. var optBin = (0, _wasmOpt.shrinkPaddedLEB128)(new Uint8Array(ab));
  21. return optBin.buffer;
  22. }
  23. function sortBySectionOrder(nodes) {
  24. var originalOrder = new Map();
  25. var _iteratorNormalCompletion = true;
  26. var _didIteratorError = false;
  27. var _iteratorError = undefined;
  28. try {
  29. for (var _iterator = nodes[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
  30. var _node = _step.value;
  31. originalOrder.set(_node, originalOrder.size);
  32. }
  33. } catch (err) {
  34. _didIteratorError = true;
  35. _iteratorError = err;
  36. } finally {
  37. try {
  38. if (!_iteratorNormalCompletion && _iterator.return != null) {
  39. _iterator.return();
  40. }
  41. } finally {
  42. if (_didIteratorError) {
  43. throw _iteratorError;
  44. }
  45. }
  46. }
  47. nodes.sort(function (a, b) {
  48. var sectionA = (0, _helperWasmBytecode.getSectionForNode)(a);
  49. var sectionB = (0, _helperWasmBytecode.getSectionForNode)(b);
  50. var aId = _helperWasmBytecode.default.sections[sectionA];
  51. var bId = _helperWasmBytecode.default.sections[sectionB];
  52. if (typeof aId !== "number" || typeof bId !== "number") {
  53. throw new Error("Section id not found");
  54. }
  55. if (aId === bId) {
  56. // $FlowIgnore originalOrder is filled for all nodes
  57. return originalOrder.get(a) - originalOrder.get(b);
  58. }
  59. return aId - bId;
  60. });
  61. }
  62. function edit(ab, visitors) {
  63. ab = preprocess(ab);
  64. var ast = (0, _wasmParser.decode)(ab);
  65. return editWithAST(ast, ab, visitors);
  66. }
  67. function editWithAST(ast, ab, visitors) {
  68. var operations = [];
  69. var uint8Buffer = new Uint8Array(ab);
  70. var nodeBefore;
  71. function before(type, path) {
  72. nodeBefore = (0, _clone.cloneNode)(path.node);
  73. }
  74. function after(type, path) {
  75. if (path.node._deleted === true) {
  76. operations.push({
  77. kind: "delete",
  78. node: path.node
  79. }); // $FlowIgnore
  80. } else if (hashNode(nodeBefore) !== hashNode(path.node)) {
  81. operations.push({
  82. kind: "update",
  83. oldNode: nodeBefore,
  84. node: path.node
  85. });
  86. }
  87. }
  88. (0, _ast.traverse)(ast, visitors, before, after);
  89. uint8Buffer = (0, _apply.applyOperations)(ast, uint8Buffer, operations);
  90. return uint8Buffer.buffer;
  91. }
  92. function add(ab, newNodes) {
  93. ab = preprocess(ab);
  94. var ast = (0, _wasmParser.decode)(ab);
  95. return addWithAST(ast, ab, newNodes);
  96. }
  97. function addWithAST(ast, ab, newNodes) {
  98. // Sort nodes by insertion order
  99. sortBySectionOrder(newNodes);
  100. var uint8Buffer = new Uint8Array(ab); // Map node into operations
  101. var operations = newNodes.map(function (n) {
  102. return {
  103. kind: "add",
  104. node: n
  105. };
  106. });
  107. uint8Buffer = (0, _apply.applyOperations)(ast, uint8Buffer, operations);
  108. return uint8Buffer.buffer;
  109. }