source: imaps-frontend/node_modules/@webassemblyjs/wasm-parser/esm/index.js

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

F4 Finalna Verzija

  • 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; // $FlowIgnore
56
57 nodeName.numeric = oldValue; // $FlowIgnore
58
59 delete nodeName.raw;
60 }
61 }),
62 // Also update the reference in the export
63 ModuleExport: function (_ModuleExport) {
64 function ModuleExport(_x2) {
65 return _ModuleExport.apply(this, arguments);
66 }
67
68 ModuleExport.toString = function () {
69 return _ModuleExport.toString();
70 };
71
72 return ModuleExport;
73 }(function (_ref3) {
74 var node = _ref3.node;
75
76 if (node.descr.exportType === "Func") {
77 // $FlowIgnore
78 var nodeName = node.descr.id;
79 var index = nodeName.value;
80 var functionName = functionNames.find(function (f) {
81 return f.index === index;
82 });
83
84 if (functionName) {
85 node.descr.id = t.identifier(functionName.name);
86 }
87 }
88 }),
89 ModuleImport: function (_ModuleImport) {
90 function ModuleImport(_x3) {
91 return _ModuleImport.apply(this, arguments);
92 }
93
94 ModuleImport.toString = function () {
95 return _ModuleImport.toString();
96 };
97
98 return ModuleImport;
99 }(function (_ref4) {
100 var node = _ref4.node;
101
102 if (node.descr.type === "FuncImportDescr") {
103 // $FlowIgnore
104 var indexBasedFunctionName = node.descr.id;
105 var index = Number(indexBasedFunctionName.replace("func_", ""));
106 var functionName = functionNames.find(function (f) {
107 return f.index === index;
108 });
109
110 if (functionName) {
111 // $FlowIgnore
112 node.descr.id = t.identifier(functionName.name);
113 }
114 }
115 }),
116 CallInstruction: function (_CallInstruction) {
117 function CallInstruction(_x4) {
118 return _CallInstruction.apply(this, arguments);
119 }
120
121 CallInstruction.toString = function () {
122 return _CallInstruction.toString();
123 };
124
125 return CallInstruction;
126 }(function (nodePath) {
127 var node = nodePath.node;
128 var index = node.index.value;
129 var functionName = functionNames.find(function (f) {
130 return f.index === index;
131 });
132
133 if (functionName) {
134 var oldValue = node.index;
135 node.index = t.identifier(functionName.name);
136 node.numeric = oldValue; // $FlowIgnore
137
138 delete node.raw;
139 }
140 })
141 });
142}
143
144function restoreLocalNames(ast) {
145 var localNames = [];
146 t.traverse(ast, {
147 LocalNameMetadata: function LocalNameMetadata(_ref5) {
148 var node = _ref5.node;
149 localNames.push({
150 name: node.value,
151 localIndex: node.localIndex,
152 functionIndex: node.functionIndex
153 });
154 }
155 });
156
157 if (localNames.length === 0) {
158 return;
159 }
160
161 t.traverse(ast, {
162 Func: function (_Func2) {
163 function Func(_x5) {
164 return _Func2.apply(this, arguments);
165 }
166
167 Func.toString = function () {
168 return _Func2.toString();
169 };
170
171 return Func;
172 }(function (_ref6) {
173 var node = _ref6.node;
174 var signature = node.signature;
175
176 if (signature.type !== "Signature") {
177 return;
178 } // $FlowIgnore
179
180
181 var nodeName = node.name;
182 var indexBasedFunctionName = nodeName.value;
183 var functionIndex = Number(indexBasedFunctionName.replace("func_", ""));
184 signature.params.forEach(function (param, paramIndex) {
185 var paramName = localNames.find(function (f) {
186 return f.localIndex === paramIndex && f.functionIndex === functionIndex;
187 });
188
189 if (paramName && paramName.name !== "") {
190 param.id = paramName.name;
191 }
192 });
193 })
194 });
195}
196
197function restoreModuleName(ast) {
198 t.traverse(ast, {
199 ModuleNameMetadata: function (_ModuleNameMetadata) {
200 function ModuleNameMetadata(_x6) {
201 return _ModuleNameMetadata.apply(this, arguments);
202 }
203
204 ModuleNameMetadata.toString = function () {
205 return _ModuleNameMetadata.toString();
206 };
207
208 return ModuleNameMetadata;
209 }(function (moduleNameMetadataPath) {
210 // update module
211 t.traverse(ast, {
212 Module: function (_Module) {
213 function Module(_x7) {
214 return _Module.apply(this, arguments);
215 }
216
217 Module.toString = function () {
218 return _Module.toString();
219 };
220
221 return Module;
222 }(function (_ref7) {
223 var node = _ref7.node;
224 var name = moduleNameMetadataPath.node.value; // compatiblity with wast-parser
225
226 if (name === "") {
227 name = null;
228 }
229
230 node.id = name;
231 })
232 });
233 })
234 });
235}
236
237export function decode(buf, customOpts) {
238 var opts = Object.assign({}, defaultDecoderOpts, customOpts);
239 var ast = decoder.decode(buf, opts);
240
241 if (opts.ignoreCustomNameSection === false) {
242 restoreFunctionNames(ast);
243 restoreLocalNames(ast);
244 restoreModuleName(ast);
245 }
246
247 return ast;
248}
Note: See TracBrowser for help on using the repository browser.