index.js 12 KB

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