Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

resize.js 2.8KB

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