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.

89 lines
3.1 KiB

4 years ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.resizeSectionByteSize = resizeSectionByteSize;
  6. exports.resizeSectionVecSize = resizeSectionVecSize;
  7. var _wasmGen = require("@webassemblyjs/wasm-gen");
  8. var _ast = require("@webassemblyjs/ast");
  9. var _helperBuffer = require("@webassemblyjs/helper-buffer");
  10. function resizeSectionByteSize(ast, uint8Buffer, section, deltaBytes) {
  11. var sectionMetadata = (0, _ast.getSectionMetadata)(ast, section);
  12. if (typeof sectionMetadata === "undefined") {
  13. throw new Error("Section metadata not found");
  14. }
  15. if (typeof sectionMetadata.size.loc === "undefined") {
  16. throw new Error("SectionMetadata " + section + " has no loc");
  17. } // keep old node location to be overriden
  18. var start = sectionMetadata.size.loc.start.column;
  19. var end = sectionMetadata.size.loc.end.column;
  20. var newSectionSize = sectionMetadata.size.value + deltaBytes;
  21. var newBytes = (0, _wasmGen.encodeU32)(newSectionSize);
  22. /**
  23. * update AST
  24. */
  25. sectionMetadata.size.value = newSectionSize;
  26. var oldu32EncodedLen = end - start;
  27. var newu32EncodedLen = newBytes.length; // the new u32 has a different encoded length
  28. if (newu32EncodedLen !== oldu32EncodedLen) {
  29. var deltaInSizeEncoding = newu32EncodedLen - oldu32EncodedLen;
  30. sectionMetadata.size.loc.end.column = start + newu32EncodedLen;
  31. deltaBytes += deltaInSizeEncoding; // move the vec size pointer size the section size is now smaller
  32. sectionMetadata.vectorOfSize.loc.start.column += deltaInSizeEncoding;
  33. sectionMetadata.vectorOfSize.loc.end.column += deltaInSizeEncoding;
  34. } // Once we hit our section every that is after needs to be shifted by the delta
  35. var encounteredSection = false;
  36. (0, _ast.traverse)(ast, {
  37. SectionMetadata: function SectionMetadata(path) {
  38. if (path.node.section === section) {
  39. encounteredSection = true;
  40. return;
  41. }
  42. if (encounteredSection === true) {
  43. (0, _ast.shiftSection)(ast, path.node, deltaBytes);
  44. }
  45. }
  46. });
  47. return (0, _helperBuffer.overrideBytesInBuffer)(uint8Buffer, start, end, newBytes);
  48. }
  49. function resizeSectionVecSize(ast, uint8Buffer, section, deltaElements) {
  50. var sectionMetadata = (0, _ast.getSectionMetadata)(ast, section);
  51. if (typeof sectionMetadata === "undefined") {
  52. throw new Error("Section metadata not found");
  53. }
  54. if (typeof sectionMetadata.vectorOfSize.loc === "undefined") {
  55. throw new Error("SectionMetadata " + section + " has no loc");
  56. } // Section has no vector
  57. if (sectionMetadata.vectorOfSize.value === -1) {
  58. return uint8Buffer;
  59. } // keep old node location to be overriden
  60. var start = sectionMetadata.vectorOfSize.loc.start.column;
  61. var end = sectionMetadata.vectorOfSize.loc.end.column;
  62. var newValue = sectionMetadata.vectorOfSize.value + deltaElements;
  63. var newBytes = (0, _wasmGen.encodeU32)(newValue); // Update AST
  64. sectionMetadata.vectorOfSize.value = newValue;
  65. sectionMetadata.vectorOfSize.loc.end.column = start + newBytes.length;
  66. return (0, _helperBuffer.overrideBytesInBuffer)(uint8Buffer, start, end, newBytes);
  67. }