source: imaps-frontend/node_modules/@webassemblyjs/ast/esm/transform/ast-module-to-module-context/index.js

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

F4 Finalna Verzija

  • Property mode set to 100644
File size: 9.4 KB
Line 
1function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
2
3function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
4
5function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
6
7import { isSignature, isNumberLiteral } from "../../nodes.js";
8export function moduleContextFromModuleAST(m) {
9 var moduleContext = new ModuleContext();
10
11 if (!(m.type === "Module")) {
12 throw new Error('m.type === "Module"' + " error: " + (undefined || "unknown"));
13 }
14
15 m.fields.forEach(function (field) {
16 switch (field.type) {
17 case "Start":
18 {
19 moduleContext.setStart(field.index);
20 break;
21 }
22
23 case "TypeInstruction":
24 {
25 moduleContext.addType(field);
26 break;
27 }
28
29 case "Func":
30 {
31 moduleContext.addFunction(field);
32 break;
33 }
34
35 case "Global":
36 {
37 moduleContext.defineGlobal(field);
38 break;
39 }
40
41 case "ModuleImport":
42 {
43 switch (field.descr.type) {
44 case "GlobalType":
45 {
46 moduleContext.importGlobal(field.descr.valtype, field.descr.mutability);
47 break;
48 }
49
50 case "Memory":
51 {
52 moduleContext.addMemory(field.descr.limits.min, field.descr.limits.max);
53 break;
54 }
55
56 case "FuncImportDescr":
57 {
58 moduleContext.importFunction(field.descr);
59 break;
60 }
61
62 case "Table":
63 {
64 // FIXME(sven): not implemented yet
65 break;
66 }
67
68 default:
69 throw new Error("Unsupported ModuleImport of type " + JSON.stringify(field.descr.type));
70 }
71
72 break;
73 }
74
75 case "Memory":
76 {
77 moduleContext.addMemory(field.limits.min, field.limits.max);
78 break;
79 }
80 }
81 });
82 return moduleContext;
83}
84/**
85 * Module context for type checking
86 */
87
88export var ModuleContext = /*#__PURE__*/function () {
89 function ModuleContext() {
90 _classCallCheck(this, ModuleContext);
91
92 this.funcs = [];
93 this.funcsOffsetByIdentifier = [];
94 this.types = [];
95 this.globals = [];
96 this.globalsOffsetByIdentifier = [];
97 this.mems = []; // Current stack frame
98
99 this.locals = [];
100 this.labels = [];
101 this["return"] = [];
102 this.debugName = "unknown";
103 this.start = null;
104 }
105 /**
106 * Set start segment
107 */
108
109
110 _createClass(ModuleContext, [{
111 key: "setStart",
112 value: function setStart(index) {
113 this.start = index.value;
114 }
115 /**
116 * Get start function
117 */
118
119 }, {
120 key: "getStart",
121 value: function getStart() {
122 return this.start;
123 }
124 /**
125 * Reset the active stack frame
126 */
127
128 }, {
129 key: "newContext",
130 value: function newContext(debugName, expectedResult) {
131 this.locals = [];
132 this.labels = [expectedResult];
133 this["return"] = expectedResult;
134 this.debugName = debugName;
135 }
136 /**
137 * Functions
138 */
139
140 }, {
141 key: "addFunction",
142 value: function addFunction(func) {
143 /* eslint-disable */
144 // $FlowIgnore
145 var _ref = func.signature || {},
146 _ref$params = _ref.params,
147 args = _ref$params === void 0 ? [] : _ref$params,
148 _ref$results = _ref.results,
149 result = _ref$results === void 0 ? [] : _ref$results;
150 /* eslint-enable */
151
152
153 args = args.map(function (arg) {
154 return arg.valtype;
155 });
156 this.funcs.push({
157 args: args,
158 result: result
159 });
160
161 if (typeof func.name !== "undefined") {
162 // $FlowIgnore
163 this.funcsOffsetByIdentifier[func.name.value] = this.funcs.length - 1;
164 }
165 }
166 }, {
167 key: "importFunction",
168 value: function importFunction(funcimport) {
169 if (isSignature(funcimport.signature)) {
170 // eslint-disable-next-line prefer-const
171 var _funcimport$signature = funcimport.signature,
172 args = _funcimport$signature.params,
173 result = _funcimport$signature.results;
174 args = args.map(function (arg) {
175 return arg.valtype;
176 });
177 this.funcs.push({
178 args: args,
179 result: result
180 });
181 } else {
182 if (!isNumberLiteral(funcimport.signature)) {
183 throw new Error('isNumberLiteral(funcimport.signature)' + " error: " + (undefined || "unknown"));
184 }
185
186 var typeId = funcimport.signature.value;
187
188 if (!this.hasType(typeId)) {
189 throw new Error('this.hasType(typeId)' + " error: " + (undefined || "unknown"));
190 }
191
192 var signature = this.getType(typeId);
193 this.funcs.push({
194 args: signature.params.map(function (arg) {
195 return arg.valtype;
196 }),
197 result: signature.results
198 });
199 }
200
201 if (typeof funcimport.id !== "undefined") {
202 // imports are first, we can assume their index in the array
203 this.funcsOffsetByIdentifier[funcimport.id.value] = this.funcs.length - 1;
204 }
205 }
206 }, {
207 key: "hasFunction",
208 value: function hasFunction(index) {
209 return typeof this.getFunction(index) !== "undefined";
210 }
211 }, {
212 key: "getFunction",
213 value: function getFunction(index) {
214 if (typeof index !== "number") {
215 throw new Error("getFunction only supported for number index");
216 }
217
218 return this.funcs[index];
219 }
220 }, {
221 key: "getFunctionOffsetByIdentifier",
222 value: function getFunctionOffsetByIdentifier(name) {
223 if (!(typeof name === "string")) {
224 throw new Error('typeof name === "string"' + " error: " + (undefined || "unknown"));
225 }
226
227 return this.funcsOffsetByIdentifier[name];
228 }
229 /**
230 * Labels
231 */
232
233 }, {
234 key: "addLabel",
235 value: function addLabel(result) {
236 this.labels.unshift(result);
237 }
238 }, {
239 key: "hasLabel",
240 value: function hasLabel(index) {
241 return this.labels.length > index && index >= 0;
242 }
243 }, {
244 key: "getLabel",
245 value: function getLabel(index) {
246 return this.labels[index];
247 }
248 }, {
249 key: "popLabel",
250 value: function popLabel() {
251 this.labels.shift();
252 }
253 /**
254 * Locals
255 */
256
257 }, {
258 key: "hasLocal",
259 value: function hasLocal(index) {
260 return typeof this.getLocal(index) !== "undefined";
261 }
262 }, {
263 key: "getLocal",
264 value: function getLocal(index) {
265 return this.locals[index];
266 }
267 }, {
268 key: "addLocal",
269 value: function addLocal(type) {
270 this.locals.push(type);
271 }
272 /**
273 * Types
274 */
275
276 }, {
277 key: "addType",
278 value: function addType(type) {
279 if (!(type.functype.type === "Signature")) {
280 throw new Error('type.functype.type === "Signature"' + " error: " + (undefined || "unknown"));
281 }
282
283 this.types.push(type.functype);
284 }
285 }, {
286 key: "hasType",
287 value: function hasType(index) {
288 return this.types[index] !== undefined;
289 }
290 }, {
291 key: "getType",
292 value: function getType(index) {
293 return this.types[index];
294 }
295 /**
296 * Globals
297 */
298
299 }, {
300 key: "hasGlobal",
301 value: function hasGlobal(index) {
302 return this.globals.length > index && index >= 0;
303 }
304 }, {
305 key: "getGlobal",
306 value: function getGlobal(index) {
307 return this.globals[index].type;
308 }
309 }, {
310 key: "getGlobalOffsetByIdentifier",
311 value: function getGlobalOffsetByIdentifier(name) {
312 if (!(typeof name === "string")) {
313 throw new Error('typeof name === "string"' + " error: " + (undefined || "unknown"));
314 }
315
316 // $FlowIgnore
317 return this.globalsOffsetByIdentifier[name];
318 }
319 }, {
320 key: "defineGlobal",
321 value: function defineGlobal(global) {
322 var type = global.globalType.valtype;
323 var mutability = global.globalType.mutability;
324 this.globals.push({
325 type: type,
326 mutability: mutability
327 });
328
329 if (typeof global.name !== "undefined") {
330 // $FlowIgnore
331 this.globalsOffsetByIdentifier[global.name.value] = this.globals.length - 1;
332 }
333 }
334 }, {
335 key: "importGlobal",
336 value: function importGlobal(type, mutability) {
337 this.globals.push({
338 type: type,
339 mutability: mutability
340 });
341 }
342 }, {
343 key: "isMutableGlobal",
344 value: function isMutableGlobal(index) {
345 return this.globals[index].mutability === "var";
346 }
347 }, {
348 key: "isImmutableGlobal",
349 value: function isImmutableGlobal(index) {
350 return this.globals[index].mutability === "const";
351 }
352 /**
353 * Memories
354 */
355
356 }, {
357 key: "hasMemory",
358 value: function hasMemory(index) {
359 return this.mems.length > index && index >= 0;
360 }
361 }, {
362 key: "addMemory",
363 value: function addMemory(min, max) {
364 this.mems.push({
365 min: min,
366 max: max
367 });
368 }
369 }, {
370 key: "getMemory",
371 value: function getMemory(index) {
372 return this.mems[index];
373 }
374 }]);
375
376 return ModuleContext;
377}();
Note: See TracBrowser for help on using the repository browser.