index.js 6.0 KB

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