You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 9.8KB

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