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.

322 lines
10 KiB

4 years ago
  1. var illegalop = "illegal";
  2. var magicModuleHeader = [0x00, 0x61, 0x73, 0x6d];
  3. var moduleVersion = [0x01, 0x00, 0x00, 0x00];
  4. function invertMap(obj) {
  5. var keyModifierFn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function (k) {
  6. return k;
  7. };
  8. var result = {};
  9. var keys = Object.keys(obj);
  10. for (var i = 0, length = keys.length; i < length; i++) {
  11. result[keyModifierFn(obj[keys[i]])] = keys[i];
  12. }
  13. return result;
  14. }
  15. function createSymbolObject(name
  16. /*: string */
  17. , object
  18. /*: string */
  19. )
  20. /*: Symbol*/
  21. {
  22. var numberOfArgs
  23. /*: number*/
  24. = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
  25. return {
  26. name: name,
  27. object: object,
  28. numberOfArgs: numberOfArgs
  29. };
  30. }
  31. function createSymbol(name
  32. /*: string */
  33. )
  34. /*: Symbol*/
  35. {
  36. var numberOfArgs
  37. /*: number*/
  38. = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
  39. return {
  40. name: name,
  41. numberOfArgs: numberOfArgs
  42. };
  43. }
  44. var types = {
  45. func: 0x60,
  46. result: 0x40
  47. };
  48. var exportTypes = {
  49. 0x00: "Func",
  50. 0x01: "Table",
  51. 0x02: "Mem",
  52. 0x03: "Global"
  53. };
  54. var exportTypesByName = invertMap(exportTypes);
  55. var valtypes = {
  56. 0x7f: "i32",
  57. 0x7e: "i64",
  58. 0x7d: "f32",
  59. 0x7c: "f64"
  60. };
  61. var valtypesByString = invertMap(valtypes);
  62. var tableTypes = {
  63. 0x70: "anyfunc"
  64. };
  65. var blockTypes = Object.assign({}, valtypes, {
  66. // https://webassembly.github.io/spec/core/binary/types.html#binary-blocktype
  67. 0x40: null,
  68. // https://webassembly.github.io/spec/core/binary/types.html#binary-valtype
  69. 0x7f: "i32",
  70. 0x7e: "i64",
  71. 0x7d: "f32",
  72. 0x7c: "f64"
  73. });
  74. var globalTypes = {
  75. 0x00: "const",
  76. 0x01: "var"
  77. };
  78. var globalTypesByString = invertMap(globalTypes);
  79. var importTypes = {
  80. 0x00: "func",
  81. 0x01: "table",
  82. 0x02: "mem",
  83. 0x03: "global"
  84. };
  85. var sections = {
  86. custom: 0,
  87. type: 1,
  88. import: 2,
  89. func: 3,
  90. table: 4,
  91. memory: 5,
  92. global: 6,
  93. export: 7,
  94. start: 8,
  95. element: 9,
  96. code: 10,
  97. data: 11
  98. };
  99. var symbolsByByte = {
  100. 0x00: createSymbol("unreachable"),
  101. 0x01: createSymbol("nop"),
  102. 0x02: createSymbol("block"),
  103. 0x03: createSymbol("loop"),
  104. 0x04: createSymbol("if"),
  105. 0x05: createSymbol("else"),
  106. 0x06: illegalop,
  107. 0x07: illegalop,
  108. 0x08: illegalop,
  109. 0x09: illegalop,
  110. 0x0a: illegalop,
  111. 0x0b: createSymbol("end"),
  112. 0x0c: createSymbol("br", 1),
  113. 0x0d: createSymbol("br_if", 1),
  114. 0x0e: createSymbol("br_table"),
  115. 0x0f: createSymbol("return"),
  116. 0x10: createSymbol("call", 1),
  117. 0x11: createSymbol("call_indirect", 2),
  118. 0x12: illegalop,
  119. 0x13: illegalop,
  120. 0x14: illegalop,
  121. 0x15: illegalop,
  122. 0x16: illegalop,
  123. 0x17: illegalop,
  124. 0x18: illegalop,
  125. 0x19: illegalop,
  126. 0x1a: createSymbol("drop"),
  127. 0x1b: createSymbol("select"),
  128. 0x1c: illegalop,
  129. 0x1d: illegalop,
  130. 0x1e: illegalop,
  131. 0x1f: illegalop,
  132. 0x20: createSymbol("get_local", 1),
  133. 0x21: createSymbol("set_local", 1),
  134. 0x22: createSymbol("tee_local", 1),
  135. 0x23: createSymbol("get_global", 1),
  136. 0x24: createSymbol("set_global", 1),
  137. 0x25: illegalop,
  138. 0x26: illegalop,
  139. 0x27: illegalop,
  140. 0x28: createSymbolObject("load", "u32", 1),
  141. 0x29: createSymbolObject("load", "u64", 1),
  142. 0x2a: createSymbolObject("load", "f32", 1),
  143. 0x2b: createSymbolObject("load", "f64", 1),
  144. 0x2c: createSymbolObject("load8_s", "u32", 1),
  145. 0x2d: createSymbolObject("load8_u", "u32", 1),
  146. 0x2e: createSymbolObject("load16_s", "u32", 1),
  147. 0x2f: createSymbolObject("load16_u", "u32", 1),
  148. 0x30: createSymbolObject("load8_s", "u64", 1),
  149. 0x31: createSymbolObject("load8_u", "u64", 1),
  150. 0x32: createSymbolObject("load16_s", "u64", 1),
  151. 0x33: createSymbolObject("load16_u", "u64", 1),
  152. 0x34: createSymbolObject("load32_s", "u64", 1),
  153. 0x35: createSymbolObject("load32_u", "u64", 1),
  154. 0x36: createSymbolObject("store", "u32", 1),
  155. 0x37: createSymbolObject("store", "u64", 1),
  156. 0x38: createSymbolObject("store", "f32", 1),
  157. 0x39: createSymbolObject("store", "f64", 1),
  158. 0x3a: createSymbolObject("store8", "u32", 1),
  159. 0x3b: createSymbolObject("store16", "u32", 1),
  160. 0x3c: createSymbolObject("store8", "u64", 1),
  161. 0x3d: createSymbolObject("store16", "u64", 1),
  162. 0x3e: createSymbolObject("store32", "u64", 1),
  163. 0x3f: createSymbolObject("current_memory"),
  164. 0x40: createSymbolObject("grow_memory"),
  165. 0x41: createSymbolObject("const", "i32", 1),
  166. 0x42: createSymbolObject("const", "i64", 1),
  167. 0x43: createSymbolObject("const", "f32", 1),
  168. 0x44: createSymbolObject("const", "f64", 1),
  169. 0x45: createSymbolObject("eqz", "i32"),
  170. 0x46: createSymbolObject("eq", "i32"),
  171. 0x47: createSymbolObject("ne", "i32"),
  172. 0x48: createSymbolObject("lt_s", "i32"),
  173. 0x49: createSymbolObject("lt_u", "i32"),
  174. 0x4a: createSymbolObject("gt_s", "i32"),
  175. 0x4b: createSymbolObject("gt_u", "i32"),
  176. 0x4c: createSymbolObject("le_s", "i32"),
  177. 0x4d: createSymbolObject("le_u", "i32"),
  178. 0x4e: createSymbolObject("ge_s", "i32"),
  179. 0x4f: createSymbolObject("ge_u", "i32"),
  180. 0x50: createSymbolObject("eqz", "i64"),
  181. 0x51: createSymbolObject("eq", "i64"),
  182. 0x52: createSymbolObject("ne", "i64"),
  183. 0x53: createSymbolObject("lt_s", "i64"),
  184. 0x54: createSymbolObject("lt_u", "i64"),
  185. 0x55: createSymbolObject("gt_s", "i64"),
  186. 0x56: createSymbolObject("gt_u", "i64"),
  187. 0x57: createSymbolObject("le_s", "i64"),
  188. 0x58: createSymbolObject("le_u", "i64"),
  189. 0x59: createSymbolObject("ge_s", "i64"),
  190. 0x5a: createSymbolObject("ge_u", "i64"),
  191. 0x5b: createSymbolObject("eq", "f32"),
  192. 0x5c: createSymbolObject("ne", "f32"),
  193. 0x5d: createSymbolObject("lt", "f32"),
  194. 0x5e: createSymbolObject("gt", "f32"),
  195. 0x5f: createSymbolObject("le", "f32"),
  196. 0x60: createSymbolObject("ge", "f32"),
  197. 0x61: createSymbolObject("eq", "f64"),
  198. 0x62: createSymbolObject("ne", "f64"),
  199. 0x63: createSymbolObject("lt", "f64"),
  200. 0x64: createSymbolObject("gt", "f64"),
  201. 0x65: createSymbolObject("le", "f64"),
  202. 0x66: createSymbolObject("ge", "f64"),
  203. 0x67: createSymbolObject("clz", "i32"),
  204. 0x68: createSymbolObject("ctz", "i32"),
  205. 0x69: createSymbolObject("popcnt", "i32"),
  206. 0x6a: createSymbolObject("add", "i32"),
  207. 0x6b: createSymbolObject("sub", "i32"),
  208. 0x6c: createSymbolObject("mul", "i32"),
  209. 0x6d: createSymbolObject("div_s", "i32"),
  210. 0x6e: createSymbolObject("div_u", "i32"),
  211. 0x6f: createSymbolObject("rem_s", "i32"),
  212. 0x70: createSymbolObject("rem_u", "i32"),
  213. 0x71: createSymbolObject("and", "i32"),
  214. 0x72: createSymbolObject("or", "i32"),
  215. 0x73: createSymbolObject("xor", "i32"),
  216. 0x74: createSymbolObject("shl", "i32"),
  217. 0x75: createSymbolObject("shr_s", "i32"),
  218. 0x76: createSymbolObject("shr_u", "i32"),
  219. 0x77: createSymbolObject("rotl", "i32"),
  220. 0x78: createSymbolObject("rotr", "i32"),
  221. 0x79: createSymbolObject("clz", "i64"),
  222. 0x7a: createSymbolObject("ctz", "i64"),
  223. 0x7b: createSymbolObject("popcnt", "i64"),
  224. 0x7c: createSymbolObject("add", "i64"),
  225. 0x7d: createSymbolObject("sub", "i64"),
  226. 0x7e: createSymbolObject("mul", "i64"),
  227. 0x7f: createSymbolObject("div_s", "i64"),
  228. 0x80: createSymbolObject("div_u", "i64"),
  229. 0x81: createSymbolObject("rem_s", "i64"),
  230. 0x82: createSymbolObject("rem_u", "i64"),
  231. 0x83: createSymbolObject("and", "i64"),
  232. 0x84: createSymbolObject("or", "i64"),
  233. 0x85: createSymbolObject("xor", "i64"),
  234. 0x86: createSymbolObject("shl", "i64"),
  235. 0x87: createSymbolObject("shr_s", "i64"),
  236. 0x88: createSymbolObject("shr_u", "i64"),
  237. 0x89: createSymbolObject("rotl", "i64"),
  238. 0x8a: createSymbolObject("rotr", "i64"),
  239. 0x8b: createSymbolObject("abs", "f32"),
  240. 0x8c: createSymbolObject("neg", "f32"),
  241. 0x8d: createSymbolObject("ceil", "f32"),
  242. 0x8e: createSymbolObject("floor", "f32"),
  243. 0x8f: createSymbolObject("trunc", "f32"),
  244. 0x90: createSymbolObject("nearest", "f32"),
  245. 0x91: createSymbolObject("sqrt", "f32"),
  246. 0x92: createSymbolObject("add", "f32"),
  247. 0x93: createSymbolObject("sub", "f32"),
  248. 0x94: createSymbolObject("mul", "f32"),
  249. 0x95: createSymbolObject("div", "f32"),
  250. 0x96: createSymbolObject("min", "f32"),
  251. 0x97: createSymbolObject("max", "f32"),
  252. 0x98: createSymbolObject("copysign", "f32"),
  253. 0x99: createSymbolObject("abs", "f64"),
  254. 0x9a: createSymbolObject("neg", "f64"),
  255. 0x9b: createSymbolObject("ceil", "f64"),
  256. 0x9c: createSymbolObject("floor", "f64"),
  257. 0x9d: createSymbolObject("trunc", "f64"),
  258. 0x9e: createSymbolObject("nearest", "f64"),
  259. 0x9f: createSymbolObject("sqrt", "f64"),
  260. 0xa0: createSymbolObject("add", "f64"),
  261. 0xa1: createSymbolObject("sub", "f64"),
  262. 0xa2: createSymbolObject("mul", "f64"),
  263. 0xa3: createSymbolObject("div", "f64"),
  264. 0xa4: createSymbolObject("min", "f64"),
  265. 0xa5: createSymbolObject("max", "f64"),
  266. 0xa6: createSymbolObject("copysign", "f64"),
  267. 0xa7: createSymbolObject("wrap/i64", "i32"),
  268. 0xa8: createSymbolObject("trunc_s/f32", "i32"),
  269. 0xa9: createSymbolObject("trunc_u/f32", "i32"),
  270. 0xaa: createSymbolObject("trunc_s/f64", "i32"),
  271. 0xab: createSymbolObject("trunc_u/f64", "i32"),
  272. 0xac: createSymbolObject("extend_s/i32", "i64"),
  273. 0xad: createSymbolObject("extend_u/i32", "i64"),
  274. 0xae: createSymbolObject("trunc_s/f32", "i64"),
  275. 0xaf: createSymbolObject("trunc_u/f32", "i64"),
  276. 0xb0: createSymbolObject("trunc_s/f64", "i64"),
  277. 0xb1: createSymbolObject("trunc_u/f64", "i64"),
  278. 0xb2: createSymbolObject("convert_s/i32", "f32"),
  279. 0xb3: createSymbolObject("convert_u/i32", "f32"),
  280. 0xb4: createSymbolObject("convert_s/i64", "f32"),
  281. 0xb5: createSymbolObject("convert_u/i64", "f32"),
  282. 0xb6: createSymbolObject("demote/f64", "f32"),
  283. 0xb7: createSymbolObject("convert_s/i32", "f64"),
  284. 0xb8: createSymbolObject("convert_u/i32", "f64"),
  285. 0xb9: createSymbolObject("convert_s/i64", "f64"),
  286. 0xba: createSymbolObject("convert_u/i64", "f64"),
  287. 0xbb: createSymbolObject("promote/f32", "f64"),
  288. 0xbc: createSymbolObject("reinterpret/f32", "i32"),
  289. 0xbd: createSymbolObject("reinterpret/f64", "i64"),
  290. 0xbe: createSymbolObject("reinterpret/i32", "f32"),
  291. 0xbf: createSymbolObject("reinterpret/i64", "f64")
  292. };
  293. var symbolsByName = invertMap(symbolsByByte, function (obj) {
  294. if (typeof obj.object === "string") {
  295. return "".concat(obj.object, ".").concat(obj.name);
  296. }
  297. return obj.name;
  298. });
  299. export default {
  300. symbolsByByte: symbolsByByte,
  301. sections: sections,
  302. magicModuleHeader: magicModuleHeader,
  303. moduleVersion: moduleVersion,
  304. types: types,
  305. valtypes: valtypes,
  306. exportTypes: exportTypes,
  307. blockTypes: blockTypes,
  308. tableTypes: tableTypes,
  309. globalTypes: globalTypes,
  310. importTypes: importTypes,
  311. valtypesByString: valtypesByString,
  312. globalTypesByString: globalTypesByString,
  313. exportTypesByName: exportTypesByName,
  314. symbolsByName: symbolsByName
  315. };
  316. export { getSectionForNode } from "./section";