create.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. 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); }
  11. var t = require("@webassemblyjs/ast");
  12. var debug = require("debug")("webassemblyjs:wasm:createsection");
  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. debug("create empty section=%s first", section);
  45. } else {
  46. start = lastSection.startOffset + lastSection.size.value + 1;
  47. end = start;
  48. debug("create empty section=%s after=%s start=%d end=%d", section, lastSection.section, start, end);
  49. } // section id
  50. start += 1;
  51. var sizeStartLoc = {
  52. line: -1,
  53. column: start
  54. };
  55. var sizeEndLoc = {
  56. line: -1,
  57. column: start + 1
  58. }; // 1 byte for the empty vector
  59. var size = t.withLoc(t.numberLiteralFromRaw(1), sizeEndLoc, sizeStartLoc);
  60. var vectorOfSizeStartLoc = {
  61. line: -1,
  62. column: sizeEndLoc.column
  63. };
  64. var vectorOfSizeEndLoc = {
  65. line: -1,
  66. column: sizeEndLoc.column + 1
  67. };
  68. var vectorOfSize = t.withLoc(t.numberLiteralFromRaw(0), vectorOfSizeEndLoc, vectorOfSizeStartLoc);
  69. var sectionMetadata = t.sectionMetadata(section, start, size, vectorOfSize);
  70. var sectionBytes = (0, _wasmGen.encodeNode)(sectionMetadata);
  71. uint8Buffer = (0, _helperBuffer.overrideBytesInBuffer)(uint8Buffer, start - 1, end, sectionBytes); // Add section into the AST for later lookups
  72. if (_typeof(ast.body[0].metadata) === "object") {
  73. // $FlowIgnore: metadata can not be empty
  74. ast.body[0].metadata.sections.push(sectionMetadata);
  75. t.sortSectionMetadata(ast.body[0]);
  76. }
  77. /**
  78. * Update AST
  79. */
  80. // Once we hit our section every that is after needs to be shifted by the delta
  81. var deltaBytes = +sectionBytes.length;
  82. var encounteredSection = false;
  83. t.traverse(ast, {
  84. SectionMetadata: function SectionMetadata(path) {
  85. if (path.node.section === section) {
  86. encounteredSection = true;
  87. return;
  88. }
  89. if (encounteredSection === true) {
  90. t.shiftSection(ast, path.node, deltaBytes);
  91. debug("shift section section=%s detla=%d", path.node.section, deltaBytes);
  92. }
  93. }
  94. });
  95. return {
  96. uint8Buffer: uint8Buffer,
  97. sectionMetadata: sectionMetadata
  98. };
  99. }