definitions.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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. assertNodeType: true,
  367. type: "Limit"
  368. },
  369. name: {
  370. maybe: true,
  371. type: "Identifier"
  372. },
  373. elements: {
  374. array: true,
  375. optional: true,
  376. type: "Index"
  377. }
  378. }
  379. });
  380. defineType("Memory", {
  381. unionType: ["Node", "ImportDescr"],
  382. fields: {
  383. limits: {
  384. type: "Limit"
  385. },
  386. id: {
  387. maybe: true,
  388. type: "Index"
  389. }
  390. }
  391. });
  392. defineType("FuncImportDescr", {
  393. unionType: ["Node", "ImportDescr"],
  394. fields: {
  395. id: {
  396. type: "Identifier"
  397. },
  398. signature: {
  399. type: "Signature"
  400. }
  401. }
  402. });
  403. defineType("ModuleImport", {
  404. unionType: ["Node"],
  405. fields: {
  406. module: {
  407. type: "string"
  408. },
  409. name: {
  410. type: "string"
  411. },
  412. descr: {
  413. type: "ImportDescr"
  414. }
  415. }
  416. });
  417. defineType("ModuleExportDescr", {
  418. unionType: ["Node"],
  419. fields: {
  420. exportType: {
  421. type: "ExportDescrType"
  422. },
  423. id: {
  424. type: "Index"
  425. }
  426. }
  427. });
  428. defineType("ModuleExport", {
  429. unionType: ["Node"],
  430. fields: {
  431. name: {
  432. type: "string"
  433. },
  434. descr: {
  435. type: "ModuleExportDescr"
  436. }
  437. }
  438. });
  439. defineType("Limit", {
  440. unionType: ["Node"],
  441. fields: {
  442. min: {
  443. type: "number"
  444. },
  445. max: {
  446. optional: true,
  447. type: "number"
  448. }
  449. }
  450. });
  451. defineType("Signature", {
  452. unionType: ["Node"],
  453. fields: {
  454. params: {
  455. array: true,
  456. type: "FuncParam"
  457. },
  458. results: {
  459. array: true,
  460. type: "Valtype"
  461. }
  462. }
  463. });
  464. defineType("Program", {
  465. unionType: ["Node"],
  466. fields: {
  467. body: {
  468. array: true,
  469. type: "Node"
  470. }
  471. }
  472. });
  473. defineType("Identifier", {
  474. unionType: ["Node", "Expression"],
  475. fields: {
  476. value: {
  477. type: "string"
  478. },
  479. raw: {
  480. optional: true,
  481. type: "string"
  482. }
  483. }
  484. });
  485. defineType("BlockInstruction", {
  486. unionType: ["Node", "Block", "Instruction"],
  487. fields: {
  488. id: {
  489. constant: true,
  490. type: "string",
  491. value: "block"
  492. },
  493. label: {
  494. maybe: true,
  495. type: "Identifier"
  496. },
  497. instr: {
  498. array: true,
  499. type: "Instruction"
  500. },
  501. result: {
  502. maybe: true,
  503. type: "Valtype"
  504. }
  505. }
  506. });
  507. defineType("CallInstruction", {
  508. unionType: ["Node", "Instruction"],
  509. fields: {
  510. id: {
  511. constant: true,
  512. type: "string",
  513. value: "call"
  514. },
  515. index: {
  516. type: "Index"
  517. },
  518. instrArgs: {
  519. array: true,
  520. optional: true,
  521. type: "Expression"
  522. }
  523. }
  524. });
  525. defineType("CallIndirectInstruction", {
  526. unionType: ["Node", "Instruction"],
  527. fields: {
  528. id: {
  529. constant: true,
  530. type: "string",
  531. value: "call_indirect"
  532. },
  533. signature: {
  534. type: "SignatureOrTypeRef"
  535. },
  536. intrs: {
  537. array: true,
  538. optional: true,
  539. type: "Expression"
  540. }
  541. }
  542. });
  543. defineType("ByteArray", {
  544. unionType: ["Node"],
  545. fields: {
  546. values: {
  547. array: true,
  548. type: "Byte"
  549. }
  550. }
  551. });
  552. defineType("Func", {
  553. unionType: ["Node", "Block"],
  554. fields: {
  555. name: {
  556. maybe: true,
  557. type: "Index"
  558. },
  559. signature: {
  560. type: "SignatureOrTypeRef"
  561. },
  562. body: {
  563. array: true,
  564. type: "Instruction"
  565. },
  566. isExternal: {
  567. comment: "means that it has been imported from the outside js",
  568. optional: true,
  569. type: "boolean"
  570. },
  571. metadata: {
  572. optional: true,
  573. type: "FuncMetadata"
  574. }
  575. }
  576. });
  577. module.exports = definitions;