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.

46 lines
1.5 KiB

4 years ago
  1. import { traverse, shiftSection } from "@webassemblyjs/ast";
  2. import { encodeU32 } from "@webassemblyjs/wasm-gen/lib/encoder";
  3. import { overrideBytesInBuffer } from "@webassemblyjs/helper-buffer";
  4. function shiftFollowingSections(ast, _ref, deltaInSizeEncoding) {
  5. var section = _ref.section;
  6. // Once we hit our section every that is after needs to be shifted by the delta
  7. var encounteredSection = false;
  8. traverse(ast, {
  9. SectionMetadata: function SectionMetadata(path) {
  10. if (path.node.section === section) {
  11. encounteredSection = true;
  12. return;
  13. }
  14. if (encounteredSection === true) {
  15. shiftSection(ast, path.node, deltaInSizeEncoding);
  16. }
  17. }
  18. });
  19. }
  20. export function shrinkPaddedLEB128(ast, uint8Buffer) {
  21. traverse(ast, {
  22. SectionMetadata: function SectionMetadata(_ref2) {
  23. var node = _ref2.node;
  24. /**
  25. * Section size
  26. */
  27. {
  28. var newu32Encoded = encodeU32(node.size.value);
  29. var newu32EncodedLen = newu32Encoded.length;
  30. var start = node.size.loc.start.column;
  31. var end = node.size.loc.end.column;
  32. var oldu32EncodedLen = end - start;
  33. if (newu32EncodedLen !== oldu32EncodedLen) {
  34. var deltaInSizeEncoding = oldu32EncodedLen - newu32EncodedLen;
  35. uint8Buffer = overrideBytesInBuffer(uint8Buffer, start, end, newu32Encoded);
  36. shiftFollowingSections(ast, node, -deltaInSizeEncoding);
  37. }
  38. }
  39. }
  40. });
  41. return uint8Buffer;
  42. }