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.

120 lines
4.0 KiB

4 years ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.createEmptySection = createEmptySection;
  6. var _wasmGen = require("@webassemblyjs/wasm-gen");
  7. var _helperBuffer = require("@webassemblyjs/helper-buffer");
  8. var _helperWasmBytecode = _interopRequireDefault(require("@webassemblyjs/helper-wasm-bytecode"));
  9. var t = _interopRequireWildcard(require("@webassemblyjs/ast"));
  10. 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; } }
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
  13. function findLastSection(ast, forSection) {
  14. var targetSectionId = _helperWasmBytecode.default.sections[forSection]; // $FlowIgnore: metadata can not be empty
  15. var moduleSections = ast.body[0].metadata.sections;
  16. var lastSection;
  17. var lastId = 0;
  18. for (var i = 0, len = moduleSections.length; i < len; i++) {
  19. var section = moduleSections[i]; // Ignore custom section since they can actually occur everywhere
  20. if (section.section === "custom") {
  21. continue;
  22. }
  23. var sectionId = _helperWasmBytecode.default.sections[section.section];
  24. if (targetSectionId > lastId && targetSectionId < sectionId) {
  25. return lastSection;
  26. }
  27. lastId = sectionId;
  28. lastSection = section;
  29. }
  30. return lastSection;
  31. }
  32. function createEmptySection(ast, uint8Buffer, section) {
  33. // previous section after which we are going to insert our section
  34. var lastSection = findLastSection(ast, section);
  35. var start, end;
  36. /**
  37. * It's the first section
  38. */
  39. if (lastSection == null || lastSection.section === "custom") {
  40. start = 8
  41. /* wasm header size */
  42. ;
  43. end = start;
  44. } else {
  45. start = lastSection.startOffset + lastSection.size.value + 1;
  46. end = start;
  47. } // section id
  48. start += 1;
  49. var sizeStartLoc = {
  50. line: -1,
  51. column: start
  52. };
  53. var sizeEndLoc = {
  54. line: -1,
  55. column: start + 1
  56. }; // 1 byte for the empty vector
  57. var size = t.withLoc(t.numberLiteralFromRaw(1), sizeEndLoc, sizeStartLoc);
  58. var vectorOfSizeStartLoc = {
  59. line: -1,
  60. column: sizeEndLoc.column
  61. };
  62. var vectorOfSizeEndLoc = {
  63. line: -1,
  64. column: sizeEndLoc.column + 1
  65. };
  66. var vectorOfSize = t.withLoc(t.numberLiteralFromRaw(0), vectorOfSizeEndLoc, vectorOfSizeStartLoc);
  67. var sectionMetadata = t.sectionMetadata(section, start, size, vectorOfSize);
  68. var sectionBytes = (0, _wasmGen.encodeNode)(sectionMetadata);
  69. uint8Buffer = (0, _helperBuffer.overrideBytesInBuffer)(uint8Buffer, start - 1, end, sectionBytes); // Add section into the AST for later lookups
  70. if (_typeof(ast.body[0].metadata) === "object") {
  71. // $FlowIgnore: metadata can not be empty
  72. ast.body[0].metadata.sections.push(sectionMetadata);
  73. t.sortSectionMetadata(ast.body[0]);
  74. }
  75. /**
  76. * Update AST
  77. */
  78. // Once we hit our section every that is after needs to be shifted by the delta
  79. var deltaBytes = +sectionBytes.length;
  80. var encounteredSection = false;
  81. t.traverse(ast, {
  82. SectionMetadata: function SectionMetadata(path) {
  83. if (path.node.section === section) {
  84. encounteredSection = true;
  85. return;
  86. }
  87. if (encounteredSection === true) {
  88. t.shiftSection(ast, path.node, deltaBytes);
  89. }
  90. }
  91. });
  92. return {
  93. uint8Buffer: uint8Buffer,
  94. sectionMetadata: sectionMetadata
  95. };
  96. }