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.
 
 
 
 

104 lines
3.0 KiB

  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. nodes.sort(function (a, b) {
  25. var sectionA = (0, _helperWasmBytecode.getSectionForNode)(a);
  26. var sectionB = (0, _helperWasmBytecode.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. }