index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 = _interopRequireDefault(require("@webassemblyjs/helper-wasm-bytecode"));
  14. var _apply = require("./apply");
  15. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  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. nodes.sort(function (a, b) {
  25. var sectionA = _helperWasmBytecode.default.getSectionForNode(a);
  26. var sectionB = _helperWasmBytecode.default.getSectionForNode(b);
  27. var aId = _helperWasmBytecode.default.sections[sectionA];
  28. var bId = _helperWasmBytecode.default.sections[sectionB];
  29. if (typeof aId !== "number" || typeof bId !== "number") {
  30. throw new Error("Section id not found");
  31. } // $FlowIgnore ensured above
  32. return aId > bId;
  33. });
  34. }
  35. function edit(ab, visitors) {
  36. ab = preprocess(ab);
  37. var ast = (0, _wasmParser.decode)(ab);
  38. return editWithAST(ast, ab, visitors);
  39. }
  40. function editWithAST(ast, ab, visitors) {
  41. var operations = [];
  42. var uint8Buffer = new Uint8Array(ab);
  43. var nodeBefore;
  44. function before(type, path) {
  45. nodeBefore = (0, _clone.cloneNode)(path.node);
  46. }
  47. function after(type, path) {
  48. if (path.node._deleted === true) {
  49. operations.push({
  50. kind: "delete",
  51. node: path.node
  52. }); // $FlowIgnore
  53. } else if (hashNode(nodeBefore) !== hashNode(path.node)) {
  54. operations.push({
  55. kind: "update",
  56. oldNode: nodeBefore,
  57. node: path.node
  58. });
  59. }
  60. }
  61. (0, _ast.traverse)(ast, visitors, before, after);
  62. uint8Buffer = (0, _apply.applyOperations)(ast, uint8Buffer, operations);
  63. return uint8Buffer.buffer;
  64. }
  65. function add(ab, newNodes) {
  66. ab = preprocess(ab);
  67. var ast = (0, _wasmParser.decode)(ab);
  68. return addWithAST(ast, ab, newNodes);
  69. }
  70. function addWithAST(ast, ab, newNodes) {
  71. // Sort nodes by insertion order
  72. sortBySectionOrder(newNodes);
  73. var uint8Buffer = new Uint8Array(ab); // Map node into operations
  74. var operations = newNodes.map(function (n) {
  75. return {
  76. kind: "add",
  77. node: n
  78. };
  79. });
  80. uint8Buffer = (0, _apply.applyOperations)(ast, uint8Buffer, operations);
  81. return uint8Buffer.buffer;
  82. }