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 _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; } }
|
---|
17 |
|
---|
18 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
---|
19 |
|
---|
20 | 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); }
|
---|
21 |
|
---|
22 | function findLastSection(ast, forSection) {
|
---|
23 | var targetSectionId = _helperWasmBytecode.default.sections[forSection]; // $FlowIgnore: metadata can not be empty
|
---|
24 |
|
---|
25 | var moduleSections = ast.body[0].metadata.sections;
|
---|
26 | var lastSection;
|
---|
27 | var lastId = 0;
|
---|
28 |
|
---|
29 | for (var i = 0, len = moduleSections.length; i < len; i++) {
|
---|
30 | var section = moduleSections[i]; // Ignore custom section since they can actually occur everywhere
|
---|
31 |
|
---|
32 | if (section.section === "custom") {
|
---|
33 | continue;
|
---|
34 | }
|
---|
35 |
|
---|
36 | var sectionId = _helperWasmBytecode.default.sections[section.section];
|
---|
37 |
|
---|
38 | if (targetSectionId > lastId && targetSectionId < sectionId) {
|
---|
39 | return lastSection;
|
---|
40 | }
|
---|
41 |
|
---|
42 | lastId = sectionId;
|
---|
43 | lastSection = section;
|
---|
44 | }
|
---|
45 |
|
---|
46 | return lastSection;
|
---|
47 | }
|
---|
48 |
|
---|
49 | function createEmptySection(ast, uint8Buffer, section) {
|
---|
50 | // previous section after which we are going to insert our section
|
---|
51 | var lastSection = findLastSection(ast, section);
|
---|
52 | var start, end;
|
---|
53 | /**
|
---|
54 | * It's the first section
|
---|
55 | */
|
---|
56 |
|
---|
57 | if (lastSection == null || lastSection.section === "custom") {
|
---|
58 | start = 8
|
---|
59 | /* wasm header size */
|
---|
60 | ;
|
---|
61 | end = start;
|
---|
62 | } else {
|
---|
63 | start = lastSection.startOffset + lastSection.size.value + 1;
|
---|
64 | end = start;
|
---|
65 | } // section id
|
---|
66 |
|
---|
67 |
|
---|
68 | start += 1;
|
---|
69 | var sizeStartLoc = {
|
---|
70 | line: -1,
|
---|
71 | column: start
|
---|
72 | };
|
---|
73 | var sizeEndLoc = {
|
---|
74 | line: -1,
|
---|
75 | column: start + 1
|
---|
76 | }; // 1 byte for the empty vector
|
---|
77 |
|
---|
78 | var size = t.withLoc(t.numberLiteralFromRaw(1), sizeEndLoc, sizeStartLoc);
|
---|
79 | var vectorOfSizeStartLoc = {
|
---|
80 | line: -1,
|
---|
81 | column: sizeEndLoc.column
|
---|
82 | };
|
---|
83 | var vectorOfSizeEndLoc = {
|
---|
84 | line: -1,
|
---|
85 | column: sizeEndLoc.column + 1
|
---|
86 | };
|
---|
87 | var vectorOfSize = t.withLoc(t.numberLiteralFromRaw(0), vectorOfSizeEndLoc, vectorOfSizeStartLoc);
|
---|
88 | var sectionMetadata = t.sectionMetadata(section, start, size, vectorOfSize);
|
---|
89 | var sectionBytes = (0, _wasmGen.encodeNode)(sectionMetadata);
|
---|
90 | uint8Buffer = (0, _helperBuffer.overrideBytesInBuffer)(uint8Buffer, start - 1, end, sectionBytes); // Add section into the AST for later lookups
|
---|
91 |
|
---|
92 | if (_typeof(ast.body[0].metadata) === "object") {
|
---|
93 | // $FlowIgnore: metadata can not be empty
|
---|
94 | ast.body[0].metadata.sections.push(sectionMetadata);
|
---|
95 | t.sortSectionMetadata(ast.body[0]);
|
---|
96 | }
|
---|
97 | /**
|
---|
98 | * Update AST
|
---|
99 | */
|
---|
100 | // Once we hit our section every that is after needs to be shifted by the delta
|
---|
101 |
|
---|
102 |
|
---|
103 | var deltaBytes = +sectionBytes.length;
|
---|
104 | var encounteredSection = false;
|
---|
105 | t.traverse(ast, {
|
---|
106 | SectionMetadata: function SectionMetadata(path) {
|
---|
107 | if (path.node.section === section) {
|
---|
108 | encounteredSection = true;
|
---|
109 | return;
|
---|
110 | }
|
---|
111 |
|
---|
112 | if (encounteredSection === true) {
|
---|
113 | t.shiftSection(ast, path.node, deltaBytes);
|
---|
114 | }
|
---|
115 | }
|
---|
116 | });
|
---|
117 | return {
|
---|
118 | uint8Buffer: uint8Buffer,
|
---|
119 | sectionMetadata: sectionMetadata
|
---|
120 | };
|
---|
121 | } |
---|