index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
  2. 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); }
  3. 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; }
  4. import { decode } from "@webassemblyjs/wasm-parser";
  5. import { traverse } from "@webassemblyjs/ast";
  6. import { cloneNode } from "@webassemblyjs/ast/lib/clone";
  7. import { shrinkPaddedLEB128 } from "@webassemblyjs/wasm-opt";
  8. import { getSectionForNode } from "@webassemblyjs/helper-wasm-bytecode";
  9. import constants from "@webassemblyjs/helper-wasm-bytecode";
  10. import { applyOperations } from "./apply";
  11. function hashNode(node) {
  12. return JSON.stringify(node);
  13. }
  14. function preprocess(ab) {
  15. var optBin = shrinkPaddedLEB128(new Uint8Array(ab));
  16. return optBin.buffer;
  17. }
  18. function sortBySectionOrder(nodes) {
  19. var originalOrder = new Map();
  20. var _iterator = _createForOfIteratorHelper(nodes),
  21. _step;
  22. try {
  23. for (_iterator.s(); !(_step = _iterator.n()).done;) {
  24. var node = _step.value;
  25. originalOrder.set(node, originalOrder.size);
  26. }
  27. } catch (err) {
  28. _iterator.e(err);
  29. } finally {
  30. _iterator.f();
  31. }
  32. nodes.sort(function (a, b) {
  33. var sectionA = getSectionForNode(a);
  34. var sectionB = getSectionForNode(b);
  35. var aId = constants.sections[sectionA];
  36. var bId = constants.sections[sectionB];
  37. if (typeof aId !== "number" || typeof bId !== "number") {
  38. throw new Error("Section id not found");
  39. }
  40. if (aId === bId) {
  41. // $FlowIgnore originalOrder is filled for all nodes
  42. return originalOrder.get(a) - originalOrder.get(b);
  43. }
  44. return aId - bId;
  45. });
  46. }
  47. export function edit(ab, visitors) {
  48. ab = preprocess(ab);
  49. var ast = decode(ab);
  50. return editWithAST(ast, ab, visitors);
  51. }
  52. export function editWithAST(ast, ab, visitors) {
  53. var operations = [];
  54. var uint8Buffer = new Uint8Array(ab);
  55. var nodeBefore;
  56. function before(type, path) {
  57. nodeBefore = cloneNode(path.node);
  58. }
  59. function after(type, path) {
  60. if (path.node._deleted === true) {
  61. operations.push({
  62. kind: "delete",
  63. node: path.node
  64. }); // $FlowIgnore
  65. } else if (hashNode(nodeBefore) !== hashNode(path.node)) {
  66. operations.push({
  67. kind: "update",
  68. oldNode: nodeBefore,
  69. node: path.node
  70. });
  71. }
  72. }
  73. traverse(ast, visitors, before, after);
  74. uint8Buffer = applyOperations(ast, uint8Buffer, operations);
  75. return uint8Buffer.buffer;
  76. }
  77. export function add(ab, newNodes) {
  78. ab = preprocess(ab);
  79. var ast = decode(ab);
  80. return addWithAST(ast, ab, newNodes);
  81. }
  82. export function addWithAST(ast, ab, newNodes) {
  83. // Sort nodes by insertion order
  84. sortBySectionOrder(newNodes);
  85. var uint8Buffer = new Uint8Array(ab); // Map node into operations
  86. var operations = newNodes.map(function (n) {
  87. return {
  88. kind: "add",
  89. node: n
  90. };
  91. });
  92. uint8Buffer = applyOperations(ast, uint8Buffer, operations);
  93. return uint8Buffer.buffer;
  94. }