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]; // $FlowIgnore: metadata can not be empty
|
---|
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]; // Ignore custom section since they can actually occur everywhere
|
---|
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 | // previous section after which we are going to insert our section
|
---|
53 | var lastSection = findLastSection(ast, section);
|
---|
54 | var start, end;
|
---|
55 | /**
|
---|
56 | * It's the first section
|
---|
57 | */
|
---|
58 |
|
---|
59 | if (lastSection == null || lastSection.section === "custom") {
|
---|
60 | start = 8
|
---|
61 | /* wasm header size */
|
---|
62 | ;
|
---|
63 | end = start;
|
---|
64 | } else {
|
---|
65 | start = lastSection.startOffset + lastSection.size.value + 1;
|
---|
66 | end = start;
|
---|
67 | } // section id
|
---|
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 | }; // 1 byte for the empty vector
|
---|
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); // Add section into the AST for later lookups
|
---|
93 |
|
---|
94 | if (_typeof(ast.body[0].metadata) === "object") {
|
---|
95 | // $FlowIgnore: metadata can not be empty
|
---|
96 | ast.body[0].metadata.sections.push(sectionMetadata);
|
---|
97 | t.sortSectionMetadata(ast.body[0]);
|
---|
98 | }
|
---|
99 | /**
|
---|
100 | * Update AST
|
---|
101 | */
|
---|
102 | // Once we hit our section every that is after needs to be shifted by the delta
|
---|
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 | } |
---|