source: imaps-frontend/node_modules/@webassemblyjs/wasm-parser/lib/index.js@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

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