source: trip-planner-front/node_modules/@webassemblyjs/wasm-parser/lib/index.js@ b738035

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

initial commit

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