definitions.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. var definitions = {};
  2. function defineType(typeName, metadata) {
  3. definitions[typeName] = metadata;
  4. }
  5. defineType("Module", {
  6. spec: {
  7. wasm: "https://webassembly.github.io/spec/core/binary/modules.html#binary-module",
  8. wat: "https://webassembly.github.io/spec/core/text/modules.html#text-module"
  9. },
  10. doc: "A module consists of a sequence of sections (termed fields in the text format).",
  11. unionType: ["Node"],
  12. fields: {
  13. id: {
  14. maybe: true,
  15. type: "string"
  16. },
  17. fields: {
  18. array: true,
  19. type: "Node"
  20. },
  21. metadata: {
  22. optional: true,
  23. type: "ModuleMetadata"
  24. }
  25. }
  26. });
  27. defineType("ModuleMetadata", {
  28. unionType: ["Node"],
  29. fields: {
  30. sections: {
  31. array: true,
  32. type: "SectionMetadata"
  33. },
  34. functionNames: {
  35. optional: true,
  36. array: true,
  37. type: "FunctionNameMetadata"
  38. },
  39. localNames: {
  40. optional: true,
  41. array: true,
  42. type: "ModuleMetadata"
  43. }
  44. }
  45. });
  46. defineType("ModuleNameMetadata", {
  47. unionType: ["Node"],
  48. fields: {
  49. value: {
  50. type: "string"
  51. }
  52. }
  53. });
  54. defineType("FunctionNameMetadata", {
  55. unionType: ["Node"],
  56. fields: {
  57. value: {
  58. type: "string"
  59. },
  60. index: {
  61. type: "number"
  62. }
  63. }
  64. });
  65. defineType("LocalNameMetadata", {
  66. unionType: ["Node"],
  67. fields: {
  68. value: {
  69. type: "string"
  70. },
  71. localIndex: {
  72. type: "number"
  73. },
  74. functionIndex: {
  75. type: "number"
  76. }
  77. }
  78. });
  79. defineType("BinaryModule", {
  80. unionType: ["Node"],
  81. fields: {
  82. id: {
  83. maybe: true,
  84. type: "string"
  85. },
  86. blob: {
  87. array: true,
  88. type: "string"
  89. }
  90. }
  91. });
  92. defineType("QuoteModule", {
  93. unionType: ["Node"],
  94. fields: {
  95. id: {
  96. maybe: true,
  97. type: "string"
  98. },
  99. string: {
  100. array: true,
  101. type: "string"
  102. }
  103. }
  104. });
  105. defineType("SectionMetadata", {
  106. unionType: ["Node"],
  107. fields: {
  108. section: {
  109. type: "SectionName"
  110. },
  111. startOffset: {
  112. type: "number"
  113. },
  114. size: {
  115. type: "NumberLiteral"
  116. },
  117. vectorOfSize: {
  118. comment: "Size of the vector in the section (if any)",
  119. type: "NumberLiteral"
  120. }
  121. }
  122. });
  123. /*
  124. Instructions
  125. */
  126. defineType("LoopInstruction", {
  127. unionType: ["Node", "Block", "Instruction"],
  128. fields: {
  129. id: {
  130. constant: true,
  131. type: "string",
  132. value: "loop"
  133. },
  134. label: {
  135. maybe: true,
  136. type: "Identifier"
  137. },
  138. resulttype: {
  139. maybe: true,
  140. type: "Valtype"
  141. },
  142. instr: {
  143. array: true,
  144. type: "Instruction"
  145. }
  146. }
  147. });
  148. defineType("Instr", {
  149. unionType: ["Node", "Expression", "Instruction"],
  150. fields: {
  151. id: {
  152. type: "string"
  153. },
  154. object: {
  155. optional: true,
  156. type: "Valtype"
  157. },
  158. args: {
  159. array: true,
  160. type: "Expression"
  161. },
  162. namedArgs: {
  163. optional: true,
  164. type: "Object"
  165. }
  166. }
  167. });
  168. defineType("IfInstruction", {
  169. unionType: ["Node", "Instruction"],
  170. fields: {
  171. id: {
  172. constant: true,
  173. type: "string",
  174. value: "if"
  175. },
  176. testLabel: {
  177. comment: "only for WAST",
  178. type: "Identifier"
  179. },
  180. test: {
  181. array: true,
  182. type: "Instruction"
  183. },
  184. result: {
  185. maybe: true,
  186. type: "Valtype"
  187. },
  188. consequent: {
  189. array: true,
  190. type: "Instruction"
  191. },
  192. alternate: {
  193. array: true,
  194. type: "Instruction"
  195. }
  196. }
  197. });
  198. /*
  199. Concrete value types
  200. */
  201. defineType("StringLiteral", {
  202. unionType: ["Node", "Expression"],
  203. fields: {
  204. value: {
  205. type: "string"
  206. }
  207. }
  208. });
  209. defineType("NumberLiteral", {
  210. unionType: ["Node", "NumericLiteral", "Expression"],
  211. fields: {
  212. value: {
  213. type: "number"
  214. },
  215. raw: {
  216. type: "string"
  217. }
  218. }
  219. });
  220. defineType("LongNumberLiteral", {
  221. unionType: ["Node", "NumericLiteral", "Expression"],
  222. fields: {
  223. value: {
  224. type: "LongNumber"
  225. },
  226. raw: {
  227. type: "string"
  228. }
  229. }
  230. });
  231. defineType("FloatLiteral", {
  232. unionType: ["Node", "NumericLiteral", "Expression"],
  233. fields: {
  234. value: {
  235. type: "number"
  236. },
  237. nan: {
  238. optional: true,
  239. type: "boolean"
  240. },
  241. inf: {
  242. optional: true,
  243. type: "boolean"
  244. },
  245. raw: {
  246. type: "string"
  247. }
  248. }
  249. });
  250. defineType("Elem", {
  251. unionType: ["Node"],
  252. fields: {
  253. table: {
  254. type: "Index"
  255. },
  256. offset: {
  257. array: true,
  258. type: "Instruction"
  259. },
  260. funcs: {
  261. array: true,
  262. type: "Index"
  263. }
  264. }
  265. });
  266. defineType("IndexInFuncSection", {
  267. unionType: ["Node"],
  268. fields: {
  269. index: {
  270. type: "Index"
  271. }
  272. }
  273. });
  274. defineType("ValtypeLiteral", {
  275. unionType: ["Node", "Expression"],
  276. fields: {
  277. name: {
  278. type: "Valtype"
  279. }
  280. }
  281. });
  282. defineType("TypeInstruction", {
  283. unionType: ["Node", "Instruction"],
  284. fields: {
  285. id: {
  286. maybe: true,
  287. type: "Index"
  288. },
  289. functype: {
  290. type: "Signature"
  291. }
  292. }
  293. });
  294. defineType("Start", {
  295. unionType: ["Node"],
  296. fields: {
  297. index: {
  298. type: "Index"
  299. }
  300. }
  301. });
  302. defineType("GlobalType", {
  303. unionType: ["Node", "ImportDescr"],
  304. fields: {
  305. valtype: {
  306. type: "Valtype"
  307. },
  308. mutability: {
  309. type: "Mutability"
  310. }
  311. }
  312. });
  313. defineType("LeadingComment", {
  314. unionType: ["Node"],
  315. fields: {
  316. value: {
  317. type: "string"
  318. }
  319. }
  320. });
  321. defineType("BlockComment", {
  322. unionType: ["Node"],
  323. fields: {
  324. value: {
  325. type: "string"
  326. }
  327. }
  328. });
  329. defineType("Data", {
  330. unionType: ["Node"],
  331. fields: {
  332. memoryIndex: {
  333. type: "Memidx"
  334. },
  335. offset: {
  336. type: "Instruction"
  337. },
  338. init: {
  339. type: "ByteArray"
  340. }
  341. }
  342. });
  343. defineType("Global", {
  344. unionType: ["Node"],
  345. fields: {
  346. globalType: {
  347. type: "GlobalType"
  348. },
  349. init: {
  350. array: true,
  351. type: "Instruction"
  352. },
  353. name: {
  354. maybe: true,
  355. type: "Identifier"
  356. }
  357. }
  358. });
  359. defineType("Table", {
  360. unionType: ["Node", "ImportDescr"],
  361. fields: {
  362. elementType: {
  363. type: "TableElementType"
  364. },
  365. limits: {
  366. type: "Limit"
  367. },
  368. name: {
  369. maybe: true,
  370. type: "Identifier"
  371. },
  372. elements: {
  373. array: true,
  374. optional: true,
  375. type: "Index"
  376. }
  377. }
  378. });
  379. defineType("Memory", {
  380. unionType: ["Node", "ImportDescr"],
  381. fields: {
  382. limits: {
  383. type: "Limit"
  384. },
  385. id: {
  386. maybe: true,
  387. type: "Index"
  388. }
  389. }
  390. });
  391. defineType("FuncImportDescr", {
  392. unionType: ["Node", "ImportDescr"],
  393. fields: {
  394. id: {
  395. type: "Identifier"
  396. },
  397. signature: {
  398. type: "Signature"
  399. }
  400. }
  401. });
  402. defineType("ModuleImport", {
  403. unionType: ["Node"],
  404. fields: {
  405. module: {
  406. type: "string"
  407. },
  408. name: {
  409. type: "string"
  410. },
  411. descr: {
  412. type: "ImportDescr"
  413. }
  414. }
  415. });
  416. defineType("ModuleExportDescr", {
  417. unionType: ["Node"],
  418. fields: {
  419. exportType: {
  420. type: "ExportDescrType"
  421. },
  422. id: {
  423. type: "Index"
  424. }
  425. }
  426. });
  427. defineType("ModuleExport", {
  428. unionType: ["Node"],
  429. fields: {
  430. name: {
  431. type: "string"
  432. },
  433. descr: {
  434. type: "ModuleExportDescr"
  435. }
  436. }
  437. });
  438. defineType("Limit", {
  439. unionType: ["Node"],
  440. fields: {
  441. min: {
  442. type: "number"
  443. },
  444. max: {
  445. optional: true,
  446. type: "number"
  447. }
  448. }
  449. });
  450. defineType("Signature", {
  451. unionType: ["Node"],
  452. fields: {
  453. params: {
  454. array: true,
  455. type: "FuncParam"
  456. },
  457. results: {
  458. array: true,
  459. type: "Valtype"
  460. }
  461. }
  462. });
  463. defineType("Program", {
  464. unionType: ["Node"],
  465. fields: {
  466. body: {
  467. array: true,
  468. type: "Node"
  469. }
  470. }
  471. });
  472. defineType("Identifier", {
  473. unionType: ["Node", "Expression"],
  474. fields: {
  475. value: {
  476. type: "string"
  477. },
  478. raw: {
  479. optional: true,
  480. type: "string"
  481. }
  482. }
  483. });
  484. defineType("BlockInstruction", {
  485. unionType: ["Node", "Block", "Instruction"],
  486. fields: {
  487. id: {
  488. constant: true,
  489. type: "string",
  490. value: "block"
  491. },
  492. label: {
  493. maybe: true,
  494. type: "Identifier"
  495. },
  496. instr: {
  497. array: true,
  498. type: "Instruction"
  499. },
  500. result: {
  501. maybe: true,
  502. type: "Valtype"
  503. }
  504. }
  505. });
  506. defineType("CallInstruction", {
  507. unionType: ["Node", "Instruction"],
  508. fields: {
  509. id: {
  510. constant: true,
  511. type: "string",
  512. value: "call"
  513. },
  514. index: {
  515. type: "Index"
  516. },
  517. instrArgs: {
  518. array: true,
  519. optional: true,
  520. type: "Expression"
  521. }
  522. }
  523. });
  524. defineType("CallIndirectInstruction", {
  525. unionType: ["Node", "Instruction"],
  526. fields: {
  527. id: {
  528. constant: true,
  529. type: "string",
  530. value: "call_indirect"
  531. },
  532. signature: {
  533. type: "SignatureOrTypeRef"
  534. },
  535. intrs: {
  536. array: true,
  537. optional: true,
  538. type: "Expression"
  539. }
  540. }
  541. });
  542. defineType("ByteArray", {
  543. unionType: ["Node"],
  544. fields: {
  545. values: {
  546. array: true,
  547. type: "Byte"
  548. }
  549. }
  550. });
  551. defineType("Func", {
  552. unionType: ["Node", "Block"],
  553. fields: {
  554. name: {
  555. maybe: true,
  556. type: "Index"
  557. },
  558. signature: {
  559. type: "SignatureOrTypeRef"
  560. },
  561. body: {
  562. array: true,
  563. type: "Instruction"
  564. },
  565. isExternal: {
  566. comment: "means that it has been imported from the outside js",
  567. optional: true,
  568. type: "boolean"
  569. },
  570. metadata: {
  571. optional: true,
  572. type: "FuncMetadata"
  573. }
  574. }
  575. });
  576. module.exports = definitions;