source: trip-planner-front/node_modules/@webassemblyjs/wasm-parser/esm/index.js@ 6c1585f

Last change on this file since 6c1585f was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 6.2 KB
Line 
1import * as decoder from "./decoder";
2import * as t from "@webassemblyjs/ast";
3/**
4 * TODO(sven): I added initial props, but we should rather fix
5 * https://github.com/xtuc/webassemblyjs/issues/405
6 */
7
8var defaultDecoderOpts = {
9 dump: false,
10 ignoreCodeSection: false,
11 ignoreDataSection: false,
12 ignoreCustomNameSection: false
13}; // traverses the AST, locating function name metadata, which is then
14// used to update index-based identifiers with function names
15
16function restoreFunctionNames(ast) {
17 var functionNames = [];
18 t.traverse(ast, {
19 FunctionNameMetadata: function FunctionNameMetadata(_ref) {
20 var node = _ref.node;
21 functionNames.push({
22 name: node.value,
23 index: node.index
24 });
25 }
26 });
27
28 if (functionNames.length === 0) {
29 return;
30 }
31
32 t.traverse(ast, {
33 Func: function (_Func) {
34 function Func(_x) {
35 return _Func.apply(this, arguments);
36 }
37
38 Func.toString = function () {
39 return _Func.toString();
40 };
41
42 return Func;
43 }(function (_ref2) {
44 var node = _ref2.node;
45 // $FlowIgnore
46 var nodeName = node.name;
47 var indexBasedFunctionName = nodeName.value;
48 var index = Number(indexBasedFunctionName.replace("func_", ""));
49 var functionName = functionNames.find(function (f) {
50 return f.index === index;
51 });
52
53 if (functionName) {
54 var oldValue = nodeName.value;
55 nodeName.value = functionName.name;
56 nodeName.numeric = oldValue; // $FlowIgnore
57
58 delete nodeName.raw;
59 }
60 }),
61 // Also update the reference in the export
62 ModuleExport: function (_ModuleExport) {
63 function ModuleExport(_x2) {
64 return _ModuleExport.apply(this, arguments);
65 }
66
67 ModuleExport.toString = function () {
68 return _ModuleExport.toString();
69 };
70
71 return ModuleExport;
72 }(function (_ref3) {
73 var node = _ref3.node;
74
75 if (node.descr.exportType === "Func") {
76 // $FlowIgnore
77 var nodeName = node.descr.id;
78 var index = nodeName.value;
79 var functionName = functionNames.find(function (f) {
80 return f.index === index;
81 });
82
83 if (functionName) {
84 node.descr.id = t.identifier(functionName.name);
85 }
86 }
87 }),
88 ModuleImport: function (_ModuleImport) {
89 function ModuleImport(_x3) {
90 return _ModuleImport.apply(this, arguments);
91 }
92
93 ModuleImport.toString = function () {
94 return _ModuleImport.toString();
95 };
96
97 return ModuleImport;
98 }(function (_ref4) {
99 var node = _ref4.node;
100
101 if (node.descr.type === "FuncImportDescr") {
102 // $FlowIgnore
103 var indexBasedFunctionName = node.descr.id;
104 var index = Number(indexBasedFunctionName.replace("func_", ""));
105 var functionName = functionNames.find(function (f) {
106 return f.index === index;
107 });
108
109 if (functionName) {
110 // $FlowIgnore
111 node.descr.id = t.identifier(functionName.name);
112 }
113 }
114 }),
115 CallInstruction: function (_CallInstruction) {
116 function CallInstruction(_x4) {
117 return _CallInstruction.apply(this, arguments);
118 }
119
120 CallInstruction.toString = function () {
121 return _CallInstruction.toString();
122 };
123
124 return CallInstruction;
125 }(function (nodePath) {
126 var node = nodePath.node;
127 var index = node.index.value;
128 var functionName = functionNames.find(function (f) {
129 return f.index === index;
130 });
131
132 if (functionName) {
133 var oldValue = node.index;
134 node.index = t.identifier(functionName.name);
135 node.numeric = oldValue; // $FlowIgnore
136
137 delete node.raw;
138 }
139 })
140 });
141}
142
143function restoreLocalNames(ast) {
144 var localNames = [];
145 t.traverse(ast, {
146 LocalNameMetadata: function LocalNameMetadata(_ref5) {
147 var node = _ref5.node;
148 localNames.push({
149 name: node.value,
150 localIndex: node.localIndex,
151 functionIndex: node.functionIndex
152 });
153 }
154 });
155
156 if (localNames.length === 0) {
157 return;
158 }
159
160 t.traverse(ast, {
161 Func: function (_Func2) {
162 function Func(_x5) {
163 return _Func2.apply(this, arguments);
164 }
165
166 Func.toString = function () {
167 return _Func2.toString();
168 };
169
170 return Func;
171 }(function (_ref6) {
172 var node = _ref6.node;
173 var signature = node.signature;
174
175 if (signature.type !== "Signature") {
176 return;
177 } // $FlowIgnore
178
179
180 var nodeName = node.name;
181 var indexBasedFunctionName = nodeName.value;
182 var functionIndex = Number(indexBasedFunctionName.replace("func_", ""));
183 signature.params.forEach(function (param, paramIndex) {
184 var paramName = localNames.find(function (f) {
185 return f.localIndex === paramIndex && f.functionIndex === functionIndex;
186 });
187
188 if (paramName && paramName.name !== "") {
189 param.id = paramName.name;
190 }
191 });
192 })
193 });
194}
195
196function restoreModuleName(ast) {
197 t.traverse(ast, {
198 ModuleNameMetadata: function (_ModuleNameMetadata) {
199 function ModuleNameMetadata(_x6) {
200 return _ModuleNameMetadata.apply(this, arguments);
201 }
202
203 ModuleNameMetadata.toString = function () {
204 return _ModuleNameMetadata.toString();
205 };
206
207 return ModuleNameMetadata;
208 }(function (moduleNameMetadataPath) {
209 // update module
210 t.traverse(ast, {
211 Module: function (_Module) {
212 function Module(_x7) {
213 return _Module.apply(this, arguments);
214 }
215
216 Module.toString = function () {
217 return _Module.toString();
218 };
219
220 return Module;
221 }(function (_ref7) {
222 var node = _ref7.node;
223 var name = moduleNameMetadataPath.node.value; // compatiblity with wast-parser
224
225 if (name === "") {
226 name = null;
227 }
228
229 node.id = name;
230 })
231 });
232 })
233 });
234}
235
236export function decode(buf, customOpts) {
237 var opts = Object.assign({}, defaultDecoderOpts, customOpts);
238 var ast = decoder.decode(buf, opts);
239
240 if (opts.ignoreCustomNameSection === false) {
241 restoreFunctionNames(ast);
242 restoreLocalNames(ast);
243 restoreModuleName(ast);
244 }
245
246 return ast;
247}
Note: See TracBrowser for help on using the repository browser.