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 8.2KB

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