index.js 9.2 KB

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