index.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. Object.defineProperty(exports, "getSectionForNode", {
  6. enumerable: true,
  7. get: function get() {
  8. return _section.getSectionForNode;
  9. }
  10. });
  11. exports.default = void 0;
  12. var _section = require("./section");
  13. var illegalop = "illegal";
  14. var magicModuleHeader = [0x00, 0x61, 0x73, 0x6d];
  15. var moduleVersion = [0x01, 0x00, 0x00, 0x00];
  16. function invertMap(obj) {
  17. var keyModifierFn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function (k) {
  18. return k;
  19. };
  20. var result = {};
  21. var keys = Object.keys(obj);
  22. for (var i = 0, length = keys.length; i < length; i++) {
  23. result[keyModifierFn(obj[keys[i]])] = keys[i];
  24. }
  25. return result;
  26. }
  27. function createSymbolObject(name
  28. /*: string */
  29. , object
  30. /*: string */
  31. )
  32. /*: Symbol*/
  33. {
  34. var numberOfArgs
  35. /*: number*/
  36. = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
  37. return {
  38. name: name,
  39. object: object,
  40. numberOfArgs: numberOfArgs
  41. };
  42. }
  43. function createSymbol(name
  44. /*: string */
  45. )
  46. /*: Symbol*/
  47. {
  48. var numberOfArgs
  49. /*: number*/
  50. = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
  51. return {
  52. name: name,
  53. numberOfArgs: numberOfArgs
  54. };
  55. }
  56. var types = {
  57. func: 0x60,
  58. result: 0x40
  59. };
  60. var exportTypes = {
  61. 0x00: "Func",
  62. 0x01: "Table",
  63. 0x02: "Mem",
  64. 0x03: "Global"
  65. };
  66. var exportTypesByName = invertMap(exportTypes);
  67. var valtypes = {
  68. 0x7f: "i32",
  69. 0x7e: "i64",
  70. 0x7d: "f32",
  71. 0x7c: "f64",
  72. 0x7b: "v128"
  73. };
  74. var valtypesByString = invertMap(valtypes);
  75. var tableTypes = {
  76. 0x70: "anyfunc"
  77. };
  78. var blockTypes = Object.assign({}, valtypes, {
  79. // https://webassembly.github.io/spec/core/binary/types.html#binary-blocktype
  80. 0x40: null,
  81. // https://webassembly.github.io/spec/core/binary/types.html#binary-valtype
  82. 0x7f: "i32",
  83. 0x7e: "i64",
  84. 0x7d: "f32",
  85. 0x7c: "f64"
  86. });
  87. var globalTypes = {
  88. 0x00: "const",
  89. 0x01: "var"
  90. };
  91. var globalTypesByString = invertMap(globalTypes);
  92. var importTypes = {
  93. 0x00: "func",
  94. 0x01: "table",
  95. 0x02: "mem",
  96. 0x03: "global"
  97. };
  98. var sections = {
  99. custom: 0,
  100. type: 1,
  101. import: 2,
  102. func: 3,
  103. table: 4,
  104. memory: 5,
  105. global: 6,
  106. export: 7,
  107. start: 8,
  108. element: 9,
  109. code: 10,
  110. data: 11
  111. };
  112. var symbolsByByte = {
  113. 0x00: createSymbol("unreachable"),
  114. 0x01: createSymbol("nop"),
  115. 0x02: createSymbol("block"),
  116. 0x03: createSymbol("loop"),
  117. 0x04: createSymbol("if"),
  118. 0x05: createSymbol("else"),
  119. 0x06: illegalop,
  120. 0x07: illegalop,
  121. 0x08: illegalop,
  122. 0x09: illegalop,
  123. 0x0a: illegalop,
  124. 0x0b: createSymbol("end"),
  125. 0x0c: createSymbol("br", 1),
  126. 0x0d: createSymbol("br_if", 1),
  127. 0x0e: createSymbol("br_table"),
  128. 0x0f: createSymbol("return"),
  129. 0x10: createSymbol("call", 1),
  130. 0x11: createSymbol("call_indirect", 2),
  131. 0x12: illegalop,
  132. 0x13: illegalop,
  133. 0x14: illegalop,
  134. 0x15: illegalop,
  135. 0x16: illegalop,
  136. 0x17: illegalop,
  137. 0x18: illegalop,
  138. 0x19: illegalop,
  139. 0x1a: createSymbol("drop"),
  140. 0x1b: createSymbol("select"),
  141. 0x1c: illegalop,
  142. 0x1d: illegalop,
  143. 0x1e: illegalop,
  144. 0x1f: illegalop,
  145. 0x20: createSymbol("get_local", 1),
  146. 0x21: createSymbol("set_local", 1),
  147. 0x22: createSymbol("tee_local", 1),
  148. 0x23: createSymbol("get_global", 1),
  149. 0x24: createSymbol("set_global", 1),
  150. 0x25: illegalop,
  151. 0x26: illegalop,
  152. 0x27: illegalop,
  153. 0x28: createSymbolObject("load", "u32", 1),
  154. 0x29: createSymbolObject("load", "u64", 1),
  155. 0x2a: createSymbolObject("load", "f32", 1),
  156. 0x2b: createSymbolObject("load", "f64", 1),
  157. 0x2c: createSymbolObject("load8_s", "u32", 1),
  158. 0x2d: createSymbolObject("load8_u", "u32", 1),
  159. 0x2e: createSymbolObject("load16_s", "u32", 1),
  160. 0x2f: createSymbolObject("load16_u", "u32", 1),
  161. 0x30: createSymbolObject("load8_s", "u64", 1),
  162. 0x31: createSymbolObject("load8_u", "u64", 1),
  163. 0x32: createSymbolObject("load16_s", "u64", 1),
  164. 0x33: createSymbolObject("load16_u", "u64", 1),
  165. 0x34: createSymbolObject("load32_s", "u64", 1),
  166. 0x35: createSymbolObject("load32_u", "u64", 1),
  167. 0x36: createSymbolObject("store", "u32", 1),
  168. 0x37: createSymbolObject("store", "u64", 1),
  169. 0x38: createSymbolObject("store", "f32", 1),
  170. 0x39: createSymbolObject("store", "f64", 1),
  171. 0x3a: createSymbolObject("store8", "u32", 1),
  172. 0x3b: createSymbolObject("store16", "u32", 1),
  173. 0x3c: createSymbolObject("store8", "u64", 1),
  174. 0x3d: createSymbolObject("store16", "u64", 1),
  175. 0x3e: createSymbolObject("store32", "u64", 1),
  176. 0x3f: createSymbolObject("current_memory"),
  177. 0x40: createSymbolObject("grow_memory"),
  178. 0x41: createSymbolObject("const", "i32", 1),
  179. 0x42: createSymbolObject("const", "i64", 1),
  180. 0x43: createSymbolObject("const", "f32", 1),
  181. 0x44: createSymbolObject("const", "f64", 1),
  182. 0x45: createSymbolObject("eqz", "i32"),
  183. 0x46: createSymbolObject("eq", "i32"),
  184. 0x47: createSymbolObject("ne", "i32"),
  185. 0x48: createSymbolObject("lt_s", "i32"),
  186. 0x49: createSymbolObject("lt_u", "i32"),
  187. 0x4a: createSymbolObject("gt_s", "i32"),
  188. 0x4b: createSymbolObject("gt_u", "i32"),
  189. 0x4c: createSymbolObject("le_s", "i32"),
  190. 0x4d: createSymbolObject("le_u", "i32"),
  191. 0x4e: createSymbolObject("ge_s", "i32"),
  192. 0x4f: createSymbolObject("ge_u", "i32"),
  193. 0x50: createSymbolObject("eqz", "i64"),
  194. 0x51: createSymbolObject("eq", "i64"),
  195. 0x52: createSymbolObject("ne", "i64"),
  196. 0x53: createSymbolObject("lt_s", "i64"),
  197. 0x54: createSymbolObject("lt_u", "i64"),
  198. 0x55: createSymbolObject("gt_s", "i64"),
  199. 0x56: createSymbolObject("gt_u", "i64"),
  200. 0x57: createSymbolObject("le_s", "i64"),
  201. 0x58: createSymbolObject("le_u", "i64"),
  202. 0x59: createSymbolObject("ge_s", "i64"),
  203. 0x5a: createSymbolObject("ge_u", "i64"),
  204. 0x5b: createSymbolObject("eq", "f32"),
  205. 0x5c: createSymbolObject("ne", "f32"),
  206. 0x5d: createSymbolObject("lt", "f32"),
  207. 0x5e: createSymbolObject("gt", "f32"),
  208. 0x5f: createSymbolObject("le", "f32"),
  209. 0x60: createSymbolObject("ge", "f32"),
  210. 0x61: createSymbolObject("eq", "f64"),
  211. 0x62: createSymbolObject("ne", "f64"),
  212. 0x63: createSymbolObject("lt", "f64"),
  213. 0x64: createSymbolObject("gt", "f64"),
  214. 0x65: createSymbolObject("le", "f64"),
  215. 0x66: createSymbolObject("ge", "f64"),
  216. 0x67: createSymbolObject("clz", "i32"),
  217. 0x68: createSymbolObject("ctz", "i32"),
  218. 0x69: createSymbolObject("popcnt", "i32"),
  219. 0x6a: createSymbolObject("add", "i32"),
  220. 0x6b: createSymbolObject("sub", "i32"),
  221. 0x6c: createSymbolObject("mul", "i32"),
  222. 0x6d: createSymbolObject("div_s", "i32"),
  223. 0x6e: createSymbolObject("div_u", "i32"),
  224. 0x6f: createSymbolObject("rem_s", "i32"),
  225. 0x70: createSymbolObject("rem_u", "i32"),
  226. 0x71: createSymbolObject("and", "i32"),
  227. 0x72: createSymbolObject("or", "i32"),
  228. 0x73: createSymbolObject("xor", "i32"),
  229. 0x74: createSymbolObject("shl", "i32"),
  230. 0x75: createSymbolObject("shr_s", "i32"),
  231. 0x76: createSymbolObject("shr_u", "i32"),
  232. 0x77: createSymbolObject("rotl", "i32"),
  233. 0x78: createSymbolObject("rotr", "i32"),
  234. 0x79: createSymbolObject("clz", "i64"),
  235. 0x7a: createSymbolObject("ctz", "i64"),
  236. 0x7b: createSymbolObject("popcnt", "i64"),
  237. 0x7c: createSymbolObject("add", "i64"),
  238. 0x7d: createSymbolObject("sub", "i64"),
  239. 0x7e: createSymbolObject("mul", "i64"),
  240. 0x7f: createSymbolObject("div_s", "i64"),
  241. 0x80: createSymbolObject("div_u", "i64"),
  242. 0x81: createSymbolObject("rem_s", "i64"),
  243. 0x82: createSymbolObject("rem_u", "i64"),
  244. 0x83: createSymbolObject("and", "i64"),
  245. 0x84: createSymbolObject("or", "i64"),
  246. 0x85: createSymbolObject("xor", "i64"),
  247. 0x86: createSymbolObject("shl", "i64"),
  248. 0x87: createSymbolObject("shr_s", "i64"),
  249. 0x88: createSymbolObject("shr_u", "i64"),
  250. 0x89: createSymbolObject("rotl", "i64"),
  251. 0x8a: createSymbolObject("rotr", "i64"),
  252. 0x8b: createSymbolObject("abs", "f32"),
  253. 0x8c: createSymbolObject("neg", "f32"),
  254. 0x8d: createSymbolObject("ceil", "f32"),
  255. 0x8e: createSymbolObject("floor", "f32"),
  256. 0x8f: createSymbolObject("trunc", "f32"),
  257. 0x90: createSymbolObject("nearest", "f32"),
  258. 0x91: createSymbolObject("sqrt", "f32"),
  259. 0x92: createSymbolObject("add", "f32"),
  260. 0x93: createSymbolObject("sub", "f32"),
  261. 0x94: createSymbolObject("mul", "f32"),
  262. 0x95: createSymbolObject("div", "f32"),
  263. 0x96: createSymbolObject("min", "f32"),
  264. 0x97: createSymbolObject("max", "f32"),
  265. 0x98: createSymbolObject("copysign", "f32"),
  266. 0x99: createSymbolObject("abs", "f64"),
  267. 0x9a: createSymbolObject("neg", "f64"),
  268. 0x9b: createSymbolObject("ceil", "f64"),
  269. 0x9c: createSymbolObject("floor", "f64"),
  270. 0x9d: createSymbolObject("trunc", "f64"),
  271. 0x9e: createSymbolObject("nearest", "f64"),
  272. 0x9f: createSymbolObject("sqrt", "f64"),
  273. 0xa0: createSymbolObject("add", "f64"),
  274. 0xa1: createSymbolObject("sub", "f64"),
  275. 0xa2: createSymbolObject("mul", "f64"),
  276. 0xa3: createSymbolObject("div", "f64"),
  277. 0xa4: createSymbolObject("min", "f64"),
  278. 0xa5: createSymbolObject("max", "f64"),
  279. 0xa6: createSymbolObject("copysign", "f64"),
  280. 0xa7: createSymbolObject("wrap/i64", "i32"),
  281. 0xa8: createSymbolObject("trunc_s/f32", "i32"),
  282. 0xa9: createSymbolObject("trunc_u/f32", "i32"),
  283. 0xaa: createSymbolObject("trunc_s/f64", "i32"),
  284. 0xab: createSymbolObject("trunc_u/f64", "i32"),
  285. 0xac: createSymbolObject("extend_s/i32", "i64"),
  286. 0xad: createSymbolObject("extend_u/i32", "i64"),
  287. 0xae: createSymbolObject("trunc_s/f32", "i64"),
  288. 0xaf: createSymbolObject("trunc_u/f32", "i64"),
  289. 0xb0: createSymbolObject("trunc_s/f64", "i64"),
  290. 0xb1: createSymbolObject("trunc_u/f64", "i64"),
  291. 0xb2: createSymbolObject("convert_s/i32", "f32"),
  292. 0xb3: createSymbolObject("convert_u/i32", "f32"),
  293. 0xb4: createSymbolObject("convert_s/i64", "f32"),
  294. 0xb5: createSymbolObject("convert_u/i64", "f32"),
  295. 0xb6: createSymbolObject("demote/f64", "f32"),
  296. 0xb7: createSymbolObject("convert_s/i32", "f64"),
  297. 0xb8: createSymbolObject("convert_u/i32", "f64"),
  298. 0xb9: createSymbolObject("convert_s/i64", "f64"),
  299. 0xba: createSymbolObject("convert_u/i64", "f64"),
  300. 0xbb: createSymbolObject("promote/f32", "f64"),
  301. 0xbc: createSymbolObject("reinterpret/f32", "i32"),
  302. 0xbd: createSymbolObject("reinterpret/f64", "i64"),
  303. 0xbe: createSymbolObject("reinterpret/i32", "f32"),
  304. 0xbf: createSymbolObject("reinterpret/i64", "f64"),
  305. // Atomic Memory Instructions
  306. 0xfe00: createSymbol("memory.atomic.notify", 1),
  307. 0xfe01: createSymbol("memory.atomic.wait32", 1),
  308. 0xfe02: createSymbol("memory.atomic.wait64", 1),
  309. 0xfe10: createSymbolObject("atomic.load", "i32", 1),
  310. 0xfe11: createSymbolObject("atomic.load", "i64", 1),
  311. 0xfe12: createSymbolObject("atomic.load8_u", "i32", 1),
  312. 0xfe13: createSymbolObject("atomic.load16_u", "i32", 1),
  313. 0xfe14: createSymbolObject("atomic.load8_u", "i64", 1),
  314. 0xfe15: createSymbolObject("atomic.load16_u", "i64", 1),
  315. 0xfe16: createSymbolObject("atomic.load32_u", "i64", 1),
  316. 0xfe17: createSymbolObject("atomic.store", "i32", 1),
  317. 0xfe18: createSymbolObject("atomic.store", "i64", 1),
  318. 0xfe19: createSymbolObject("atomic.store8_u", "i32", 1),
  319. 0xfe1a: createSymbolObject("atomic.store16_u", "i32", 1),
  320. 0xfe1b: createSymbolObject("atomic.store8_u", "i64", 1),
  321. 0xfe1c: createSymbolObject("atomic.store16_u", "i64", 1),
  322. 0xfe1d: createSymbolObject("atomic.store32_u", "i64", 1),
  323. 0xfe1e: createSymbolObject("atomic.rmw.add", "i32", 1),
  324. 0xfe1f: createSymbolObject("atomic.rmw.add", "i64", 1),
  325. 0xfe20: createSymbolObject("atomic.rmw8_u.add_u", "i32", 1),
  326. 0xfe21: createSymbolObject("atomic.rmw16_u.add_u", "i32", 1),
  327. 0xfe22: createSymbolObject("atomic.rmw8_u.add_u", "i64", 1),
  328. 0xfe23: createSymbolObject("atomic.rmw16_u.add_u", "i64", 1),
  329. 0xfe24: createSymbolObject("atomic.rmw32_u.add_u", "i64", 1),
  330. 0xfe25: createSymbolObject("atomic.rmw.sub", "i32", 1),
  331. 0xfe26: createSymbolObject("atomic.rmw.sub", "i64", 1),
  332. 0xfe27: createSymbolObject("atomic.rmw8_u.sub_u", "i32", 1),
  333. 0xfe28: createSymbolObject("atomic.rmw16_u.sub_u", "i32", 1),
  334. 0xfe29: createSymbolObject("atomic.rmw8_u.sub_u", "i64", 1),
  335. 0xfe2a: createSymbolObject("atomic.rmw16_u.sub_u", "i64", 1),
  336. 0xfe2b: createSymbolObject("atomic.rmw32_u.sub_u", "i64", 1),
  337. 0xfe2c: createSymbolObject("atomic.rmw.and", "i32", 1),
  338. 0xfe2d: createSymbolObject("atomic.rmw.and", "i64", 1),
  339. 0xfe2e: createSymbolObject("atomic.rmw8_u.and_u", "i32", 1),
  340. 0xfe2f: createSymbolObject("atomic.rmw16_u.and_u", "i32", 1),
  341. 0xfe30: createSymbolObject("atomic.rmw8_u.and_u", "i64", 1),
  342. 0xfe31: createSymbolObject("atomic.rmw16_u.and_u", "i64", 1),
  343. 0xfe32: createSymbolObject("atomic.rmw32_u.and_u", "i64", 1),
  344. 0xfe33: createSymbolObject("atomic.rmw.or", "i32", 1),
  345. 0xfe34: createSymbolObject("atomic.rmw.or", "i64", 1),
  346. 0xfe35: createSymbolObject("atomic.rmw8_u.or_u", "i32", 1),
  347. 0xfe36: createSymbolObject("atomic.rmw16_u.or_u", "i32", 1),
  348. 0xfe37: createSymbolObject("atomic.rmw8_u.or_u", "i64", 1),
  349. 0xfe38: createSymbolObject("atomic.rmw16_u.or_u", "i64", 1),
  350. 0xfe39: createSymbolObject("atomic.rmw32_u.or_u", "i64", 1),
  351. 0xfe3a: createSymbolObject("atomic.rmw.xor", "i32", 1),
  352. 0xfe3b: createSymbolObject("atomic.rmw.xor", "i64", 1),
  353. 0xfe3c: createSymbolObject("atomic.rmw8_u.xor_u", "i32", 1),
  354. 0xfe3d: createSymbolObject("atomic.rmw16_u.xor_u", "i32", 1),
  355. 0xfe3e: createSymbolObject("atomic.rmw8_u.xor_u", "i64", 1),
  356. 0xfe3f: createSymbolObject("atomic.rmw16_u.xor_u", "i64", 1),
  357. 0xfe40: createSymbolObject("atomic.rmw32_u.xor_u", "i64", 1),
  358. 0xfe41: createSymbolObject("atomic.rmw.xchg", "i32", 1),
  359. 0xfe42: createSymbolObject("atomic.rmw.xchg", "i64", 1),
  360. 0xfe43: createSymbolObject("atomic.rmw8_u.xchg_u", "i32", 1),
  361. 0xfe44: createSymbolObject("atomic.rmw16_u.xchg_u", "i32", 1),
  362. 0xfe45: createSymbolObject("atomic.rmw8_u.xchg_u", "i64", 1),
  363. 0xfe46: createSymbolObject("atomic.rmw16_u.xchg_u", "i64", 1),
  364. 0xfe47: createSymbolObject("atomic.rmw32_u.xchg_u", "i64", 1),
  365. 0xfe48: createSymbolObject("atomic.rmw.cmpxchg", "i32", 1),
  366. 0xfe49: createSymbolObject("atomic.rmw.cmpxchg", "i64", 1),
  367. 0xfe4a: createSymbolObject("atomic.rmw8_u.cmpxchg_u", "i32", 1),
  368. 0xfe4b: createSymbolObject("atomic.rmw16_u.cmpxchg_u", "i32", 1),
  369. 0xfe4c: createSymbolObject("atomic.rmw8_u.cmpxchg_u", "i64", 1),
  370. 0xfe4d: createSymbolObject("atomic.rmw16_u.cmpxchg_u", "i64", 1),
  371. 0xfe4e: createSymbolObject("atomic.rmw32_u.cmpxchg_u", "i64", 1)
  372. };
  373. var symbolsByName = invertMap(symbolsByByte, function (obj) {
  374. if (typeof obj.object === "string") {
  375. return "".concat(obj.object, ".").concat(obj.name);
  376. }
  377. return obj.name;
  378. });
  379. var _default = {
  380. symbolsByByte: symbolsByByte,
  381. sections: sections,
  382. magicModuleHeader: magicModuleHeader,
  383. moduleVersion: moduleVersion,
  384. types: types,
  385. valtypes: valtypes,
  386. exportTypes: exportTypes,
  387. blockTypes: blockTypes,
  388. tableTypes: tableTypes,
  389. globalTypes: globalTypes,
  390. importTypes: importTypes,
  391. valtypesByString: valtypesByString,
  392. globalTypesByString: globalTypesByString,
  393. exportTypesByName: exportTypesByName,
  394. symbolsByName: symbolsByName
  395. };
  396. exports.default = _default;