create.js 4.7 KB

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