1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.createEmptySection = createEmptySection;
|
7 |
|
8 | var _wasmGen = require("@webassemblyjs/wasm-gen");
|
9 |
|
10 | var _helperBuffer = require("@webassemblyjs/helper-buffer");
|
11 |
|
12 | var _helperWasmBytecode = _interopRequireDefault(require("@webassemblyjs/helper-wasm-bytecode"));
|
13 |
|
14 | var t = _interopRequireWildcard(require("@webassemblyjs/ast"));
|
15 |
|
16 | 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); }
|
17 |
|
18 | 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; }
|
19 |
|
20 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
21 |
|
22 | 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); }
|
23 |
|
24 | function findLastSection(ast, forSection) {
|
25 | var targetSectionId = _helperWasmBytecode["default"].sections[forSection];
|
26 |
|
27 | var moduleSections = ast.body[0].metadata.sections;
|
28 | var lastSection;
|
29 | var lastId = 0;
|
30 |
|
31 | for (var i = 0, len = moduleSections.length; i < len; i++) {
|
32 | var section = moduleSections[i];
|
33 |
|
34 | if (section.section === "custom") {
|
35 | continue;
|
36 | }
|
37 |
|
38 | var sectionId = _helperWasmBytecode["default"].sections[section.section];
|
39 |
|
40 | if (targetSectionId > lastId && targetSectionId < sectionId) {
|
41 | return lastSection;
|
42 | }
|
43 |
|
44 | lastId = sectionId;
|
45 | lastSection = section;
|
46 | }
|
47 |
|
48 | return lastSection;
|
49 | }
|
50 |
|
51 | function createEmptySection(ast, uint8Buffer, section) {
|
52 |
|
53 | var lastSection = findLastSection(ast, section);
|
54 | var start, end;
|
55 | |
56 |
|
57 |
|
58 |
|
59 | if (lastSection == null || lastSection.section === "custom") {
|
60 | start = 8
|
61 |
|
62 | ;
|
63 | end = start;
|
64 | } else {
|
65 | start = lastSection.startOffset + lastSection.size.value + 1;
|
66 | end = start;
|
67 | }
|
68 |
|
69 |
|
70 | start += 1;
|
71 | var sizeStartLoc = {
|
72 | line: -1,
|
73 | column: start
|
74 | };
|
75 | var sizeEndLoc = {
|
76 | line: -1,
|
77 | column: start + 1
|
78 | };
|
79 |
|
80 | var size = t.withLoc(t.numberLiteralFromRaw(1), sizeEndLoc, sizeStartLoc);
|
81 | var vectorOfSizeStartLoc = {
|
82 | line: -1,
|
83 | column: sizeEndLoc.column
|
84 | };
|
85 | var vectorOfSizeEndLoc = {
|
86 | line: -1,
|
87 | column: sizeEndLoc.column + 1
|
88 | };
|
89 | var vectorOfSize = t.withLoc(t.numberLiteralFromRaw(0), vectorOfSizeEndLoc, vectorOfSizeStartLoc);
|
90 | var sectionMetadata = t.sectionMetadata(section, start, size, vectorOfSize);
|
91 | var sectionBytes = (0, _wasmGen.encodeNode)(sectionMetadata);
|
92 | uint8Buffer = (0, _helperBuffer.overrideBytesInBuffer)(uint8Buffer, start - 1, end, sectionBytes);
|
93 |
|
94 | if (_typeof(ast.body[0].metadata) === "object") {
|
95 |
|
96 | ast.body[0].metadata.sections.push(sectionMetadata);
|
97 | t.sortSectionMetadata(ast.body[0]);
|
98 | }
|
99 | |
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | var deltaBytes = +sectionBytes.length;
|
106 | var encounteredSection = false;
|
107 | t.traverse(ast, {
|
108 | SectionMetadata: function SectionMetadata(path) {
|
109 | if (path.node.section === section) {
|
110 | encounteredSection = true;
|
111 | return;
|
112 | }
|
113 |
|
114 | if (encounteredSection === true) {
|
115 | t.shiftSection(ast, path.node, deltaBytes);
|
116 | }
|
117 | }
|
118 | });
|
119 | return {
|
120 | uint8Buffer: uint8Buffer,
|
121 | sectionMetadata: sectionMetadata
|
122 | };
|
123 | } |
\ | No newline at end of file |