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