leb128.js 1.6 KB

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