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

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

F4 Finalna Verzija

  • Property mode set to 100644
File size: 9.2 KB
Line 
1function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
2
3function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
4
5function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
6
7function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
8
9function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
10
11function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
12
13import * as leb from "@webassemblyjs/leb128";
14import * as ieee754 from "@webassemblyjs/ieee754";
15import * as utf8 from "@webassemblyjs/utf8";
16import constants from "@webassemblyjs/helper-wasm-bytecode";
17import { encodeNode } from "../index";
18
19function assertNotIdentifierNode(n) {
20 if (n.type === "Identifier") {
21 throw new Error("Unsupported node Identifier");
22 }
23}
24
25export function encodeVersion(v) {
26 var bytes = constants.moduleVersion;
27 bytes[0] = v;
28 return bytes;
29}
30export function encodeHeader() {
31 return constants.magicModuleHeader;
32}
33export function encodeU32(v) {
34 var uint8view = new Uint8Array(leb.encodeU32(v));
35
36 var array = _toConsumableArray(uint8view);
37
38 return array;
39}
40export function encodeI32(v) {
41 var uint8view = new Uint8Array(leb.encodeI32(v));
42
43 var array = _toConsumableArray(uint8view);
44
45 return array;
46}
47export function encodeI64(v) {
48 var uint8view = new Uint8Array(leb.encodeI64(v));
49
50 var array = _toConsumableArray(uint8view);
51
52 return array;
53}
54export function encodeVec(elements) {
55 var size = encodeU32(elements.length);
56 return [].concat(_toConsumableArray(size), _toConsumableArray(elements));
57}
58export function encodeValtype(v) {
59 var _byte = constants.valtypesByString[v];
60
61 if (typeof _byte === "undefined") {
62 throw new Error("Unknown valtype: " + v);
63 }
64
65 return parseInt(_byte, 10);
66}
67export function encodeMutability(v) {
68 var _byte2 = constants.globalTypesByString[v];
69
70 if (typeof _byte2 === "undefined") {
71 throw new Error("Unknown mutability: " + v);
72 }
73
74 return parseInt(_byte2, 10);
75}
76export function encodeUTF8Vec(str) {
77 return encodeVec(utf8.encode(str));
78}
79export function encodeLimits(n) {
80 var out = [];
81
82 if (typeof n.max === "number") {
83 out.push(0x01);
84 out.push.apply(out, _toConsumableArray(encodeU32(n.min))); // $FlowIgnore: ensured by the typeof
85
86 out.push.apply(out, _toConsumableArray(encodeU32(n.max)));
87 } else {
88 out.push(0x00);
89 out.push.apply(out, _toConsumableArray(encodeU32(n.min)));
90 }
91
92 return out;
93}
94export function encodeModuleImport(n) {
95 var out = [];
96 out.push.apply(out, _toConsumableArray(encodeUTF8Vec(n.module)));
97 out.push.apply(out, _toConsumableArray(encodeUTF8Vec(n.name)));
98
99 switch (n.descr.type) {
100 case "GlobalType":
101 {
102 out.push(0x03); // $FlowIgnore: GlobalType ensure that these props exists
103
104 out.push(encodeValtype(n.descr.valtype)); // $FlowIgnore: GlobalType ensure that these props exists
105
106 out.push(encodeMutability(n.descr.mutability));
107 break;
108 }
109
110 case "Memory":
111 {
112 out.push(0x02); // $FlowIgnore
113
114 out.push.apply(out, _toConsumableArray(encodeLimits(n.descr.limits)));
115 break;
116 }
117
118 case "Table":
119 {
120 out.push(0x01);
121 out.push(0x70); // element type
122 // $FlowIgnore
123
124 out.push.apply(out, _toConsumableArray(encodeLimits(n.descr.limits)));
125 break;
126 }
127
128 case "FuncImportDescr":
129 {
130 out.push(0x00); // $FlowIgnore
131
132 assertNotIdentifierNode(n.descr.id); // $FlowIgnore
133
134 out.push.apply(out, _toConsumableArray(encodeU32(n.descr.id.value)));
135 break;
136 }
137
138 default:
139 throw new Error("Unsupport operation: encode module import of type: " + n.descr.type);
140 }
141
142 return out;
143}
144export function encodeSectionMetadata(n) {
145 var out = [];
146 var sectionId = constants.sections[n.section];
147
148 if (typeof sectionId === "undefined") {
149 throw new Error("Unknown section: " + n.section);
150 }
151
152 if (n.section === "start") {
153 /**
154 * This is not implemented yet because it's a special case which
155 * doesn't have a vector in its section.
156 */
157 throw new Error("Unsupported section encoding of type start");
158 }
159
160 out.push(sectionId);
161 out.push.apply(out, _toConsumableArray(encodeU32(n.size.value)));
162 out.push.apply(out, _toConsumableArray(encodeU32(n.vectorOfSize.value)));
163 return out;
164}
165export function encodeCallInstruction(n) {
166 var out = [];
167 assertNotIdentifierNode(n.index);
168 out.push(0x10); // $FlowIgnore
169
170 out.push.apply(out, _toConsumableArray(encodeU32(n.index.value)));
171 return out;
172}
173export function encodeCallIndirectInstruction(n) {
174 var out = []; // $FlowIgnore
175
176 assertNotIdentifierNode(n.index);
177 out.push(0x11); // $FlowIgnore
178
179 out.push.apply(out, _toConsumableArray(encodeU32(n.index.value))); // add a reserved byte
180
181 out.push(0x00);
182 return out;
183}
184export function encodeModuleExport(n) {
185 var out = [];
186 assertNotIdentifierNode(n.descr.id);
187 var exportTypeByteString = constants.exportTypesByName[n.descr.exportType];
188
189 if (typeof exportTypeByteString === "undefined") {
190 throw new Error("Unknown export of type: " + n.descr.exportType);
191 }
192
193 var exportTypeByte = parseInt(exportTypeByteString, 10);
194 out.push.apply(out, _toConsumableArray(encodeUTF8Vec(n.name)));
195 out.push(exportTypeByte); // $FlowIgnore
196
197 out.push.apply(out, _toConsumableArray(encodeU32(n.descr.id.value)));
198 return out;
199}
200export function encodeTypeInstruction(n) {
201 var out = [0x60];
202 var params = n.functype.params.map(function (x) {
203 return x.valtype;
204 }).map(encodeValtype);
205 var results = n.functype.results.map(encodeValtype);
206 out.push.apply(out, _toConsumableArray(encodeVec(params)));
207 out.push.apply(out, _toConsumableArray(encodeVec(results)));
208 return out;
209}
210export function encodeInstr(n) {
211 var out = [];
212 var instructionName = n.id;
213
214 if (typeof n.object === "string") {
215 instructionName = "".concat(n.object, ".").concat(String(n.id));
216 }
217
218 var byteString = constants.symbolsByName[instructionName];
219
220 if (typeof byteString === "undefined") {
221 throw new Error("encodeInstr: unknown instruction " + JSON.stringify(instructionName));
222 }
223
224 var _byte3 = parseInt(byteString, 10);
225
226 out.push(_byte3);
227
228 if (n.args) {
229 n.args.forEach(function (arg) {
230 var encoder = encodeU32; // find correct encoder
231
232 if (n.object === "i32") {
233 encoder = encodeI32;
234 }
235
236 if (n.object === "i64") {
237 encoder = encodeI64;
238 }
239
240 if (n.object === "f32") {
241 encoder = ieee754.encodeF32;
242 }
243
244 if (n.object === "f64") {
245 encoder = ieee754.encodeF64;
246 }
247
248 if (arg.type === "NumberLiteral" || arg.type === "FloatLiteral" || arg.type === "LongNumberLiteral") {
249 // $FlowIgnore
250 out.push.apply(out, _toConsumableArray(encoder(arg.value)));
251 } else {
252 throw new Error("Unsupported instruction argument encoding " + JSON.stringify(arg.type));
253 }
254 });
255 }
256
257 return out;
258}
259
260function encodeExpr(instrs) {
261 var out = [];
262 instrs.forEach(function (instr) {
263 // $FlowIgnore
264 var n = encodeNode(instr);
265 out.push.apply(out, _toConsumableArray(n));
266 });
267 return out;
268}
269
270export function encodeStringLiteral(n) {
271 return encodeUTF8Vec(n.value);
272}
273export function encodeGlobal(n) {
274 var out = [];
275 var _n$globalType = n.globalType,
276 valtype = _n$globalType.valtype,
277 mutability = _n$globalType.mutability;
278 out.push(encodeValtype(valtype));
279 out.push(encodeMutability(mutability));
280 out.push.apply(out, _toConsumableArray(encodeExpr(n.init)));
281 return out;
282}
283export function encodeFuncBody(n) {
284 var out = [];
285 out.push(-1); // temporary function body size
286 // FIXME(sven): get the func locals?
287
288 var localBytes = encodeVec([]);
289 out.push.apply(out, _toConsumableArray(localBytes));
290 var funcBodyBytes = encodeExpr(n.body);
291 out[0] = funcBodyBytes.length + localBytes.length;
292 out.push.apply(out, _toConsumableArray(funcBodyBytes));
293 return out;
294}
295export function encodeIndexInFuncSection(n) {
296 assertNotIdentifierNode(n.index); // $FlowIgnore
297
298 return encodeU32(n.index.value);
299}
300export function encodeElem(n) {
301 var out = [];
302 assertNotIdentifierNode(n.table); // $FlowIgnore
303
304 out.push.apply(out, _toConsumableArray(encodeU32(n.table.value)));
305 out.push.apply(out, _toConsumableArray(encodeExpr(n.offset))); // $FlowIgnore
306
307 var funcs = n.funcs.reduce(function (acc, x) {
308 return [].concat(_toConsumableArray(acc), _toConsumableArray(encodeU32(x.value)));
309 }, []);
310 out.push.apply(out, _toConsumableArray(encodeVec(funcs)));
311 return out;
312}
Note: See TracBrowser for help on using the repository browser.