definitions.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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. producers: {
  45. optional: true,
  46. array: true,
  47. type: "ProducersSectionMetadata"
  48. }
  49. }
  50. });
  51. defineType("ModuleNameMetadata", {
  52. unionType: ["Node"],
  53. fields: {
  54. value: {
  55. type: "string"
  56. }
  57. }
  58. });
  59. defineType("FunctionNameMetadata", {
  60. unionType: ["Node"],
  61. fields: {
  62. value: {
  63. type: "string"
  64. },
  65. index: {
  66. type: "number"
  67. }
  68. }
  69. });
  70. defineType("LocalNameMetadata", {
  71. unionType: ["Node"],
  72. fields: {
  73. value: {
  74. type: "string"
  75. },
  76. localIndex: {
  77. type: "number"
  78. },
  79. functionIndex: {
  80. type: "number"
  81. }
  82. }
  83. });
  84. defineType("BinaryModule", {
  85. unionType: ["Node"],
  86. fields: {
  87. id: {
  88. maybe: true,
  89. type: "string"
  90. },
  91. blob: {
  92. array: true,
  93. type: "string"
  94. }
  95. }
  96. });
  97. defineType("QuoteModule", {
  98. unionType: ["Node"],
  99. fields: {
  100. id: {
  101. maybe: true,
  102. type: "string"
  103. },
  104. string: {
  105. array: true,
  106. type: "string"
  107. }
  108. }
  109. });
  110. defineType("SectionMetadata", {
  111. unionType: ["Node"],
  112. fields: {
  113. section: {
  114. type: "SectionName"
  115. },
  116. startOffset: {
  117. type: "number"
  118. },
  119. size: {
  120. type: "NumberLiteral"
  121. },
  122. vectorOfSize: {
  123. comment: "Size of the vector in the section (if any)",
  124. type: "NumberLiteral"
  125. }
  126. }
  127. });
  128. defineType("ProducersSectionMetadata", {
  129. unionType: ["Node"],
  130. fields: {
  131. producers: {
  132. array: true,
  133. type: "ProducerMetadata"
  134. }
  135. }
  136. });
  137. defineType("ProducerMetadata", {
  138. unionType: ["Node"],
  139. fields: {
  140. language: {
  141. type: "ProducerMetadataVersionedName",
  142. array: true
  143. },
  144. processedBy: {
  145. type: "ProducerMetadataVersionedName",
  146. array: true
  147. },
  148. sdk: {
  149. type: "ProducerMetadataVersionedName",
  150. array: true
  151. }
  152. }
  153. });
  154. defineType("ProducerMetadataVersionedName", {
  155. unionType: ["Node"],
  156. fields: {
  157. name: {
  158. type: "string"
  159. },
  160. version: {
  161. type: "string"
  162. }
  163. }
  164. });
  165. /*
  166. Instructions
  167. */
  168. defineType("LoopInstruction", {
  169. unionType: ["Node", "Block", "Instruction"],
  170. fields: {
  171. id: {
  172. constant: true,
  173. type: "string",
  174. value: "loop"
  175. },
  176. label: {
  177. maybe: true,
  178. type: "Identifier"
  179. },
  180. resulttype: {
  181. maybe: true,
  182. type: "Valtype"
  183. },
  184. instr: {
  185. array: true,
  186. type: "Instruction"
  187. }
  188. }
  189. });
  190. defineType("Instr", {
  191. unionType: ["Node", "Expression", "Instruction"],
  192. fields: {
  193. id: {
  194. type: "string"
  195. },
  196. object: {
  197. optional: true,
  198. type: "Valtype"
  199. },
  200. args: {
  201. array: true,
  202. type: "Expression"
  203. },
  204. namedArgs: {
  205. optional: true,
  206. type: "Object"
  207. }
  208. }
  209. });
  210. defineType("IfInstruction", {
  211. unionType: ["Node", "Instruction"],
  212. fields: {
  213. id: {
  214. constant: true,
  215. type: "string",
  216. value: "if"
  217. },
  218. testLabel: {
  219. comment: "only for WAST",
  220. type: "Identifier"
  221. },
  222. test: {
  223. array: true,
  224. type: "Instruction"
  225. },
  226. result: {
  227. maybe: true,
  228. type: "Valtype"
  229. },
  230. consequent: {
  231. array: true,
  232. type: "Instruction"
  233. },
  234. alternate: {
  235. array: true,
  236. type: "Instruction"
  237. }
  238. }
  239. });
  240. /*
  241. Concrete value types
  242. */
  243. defineType("StringLiteral", {
  244. unionType: ["Node", "Expression"],
  245. fields: {
  246. value: {
  247. type: "string"
  248. }
  249. }
  250. });
  251. defineType("NumberLiteral", {
  252. unionType: ["Node", "NumericLiteral", "Expression"],
  253. fields: {
  254. value: {
  255. type: "number"
  256. },
  257. raw: {
  258. type: "string"
  259. }
  260. }
  261. });
  262. defineType("LongNumberLiteral", {
  263. unionType: ["Node", "NumericLiteral", "Expression"],
  264. fields: {
  265. value: {
  266. type: "LongNumber"
  267. },
  268. raw: {
  269. type: "string"
  270. }
  271. }
  272. });
  273. defineType("FloatLiteral", {
  274. unionType: ["Node", "NumericLiteral", "Expression"],
  275. fields: {
  276. value: {
  277. type: "number"
  278. },
  279. nan: {
  280. optional: true,
  281. type: "boolean"
  282. },
  283. inf: {
  284. optional: true,
  285. type: "boolean"
  286. },
  287. raw: {
  288. type: "string"
  289. }
  290. }
  291. });
  292. defineType("Elem", {
  293. unionType: ["Node"],
  294. fields: {
  295. table: {
  296. type: "Index"
  297. },
  298. offset: {
  299. array: true,
  300. type: "Instruction"
  301. },
  302. funcs: {
  303. array: true,
  304. type: "Index"
  305. }
  306. }
  307. });
  308. defineType("IndexInFuncSection", {
  309. unionType: ["Node"],
  310. fields: {
  311. index: {
  312. type: "Index"
  313. }
  314. }
  315. });
  316. defineType("ValtypeLiteral", {
  317. unionType: ["Node", "Expression"],
  318. fields: {
  319. name: {
  320. type: "Valtype"
  321. }
  322. }
  323. });
  324. defineType("TypeInstruction", {
  325. unionType: ["Node", "Instruction"],
  326. fields: {
  327. id: {
  328. maybe: true,
  329. type: "Index"
  330. },
  331. functype: {
  332. type: "Signature"
  333. }
  334. }
  335. });
  336. defineType("Start", {
  337. unionType: ["Node"],
  338. fields: {
  339. index: {
  340. type: "Index"
  341. }
  342. }
  343. });
  344. defineType("GlobalType", {
  345. unionType: ["Node", "ImportDescr"],
  346. fields: {
  347. valtype: {
  348. type: "Valtype"
  349. },
  350. mutability: {
  351. type: "Mutability"
  352. }
  353. }
  354. });
  355. defineType("LeadingComment", {
  356. unionType: ["Node"],
  357. fields: {
  358. value: {
  359. type: "string"
  360. }
  361. }
  362. });
  363. defineType("BlockComment", {
  364. unionType: ["Node"],
  365. fields: {
  366. value: {
  367. type: "string"
  368. }
  369. }
  370. });
  371. defineType("Data", {
  372. unionType: ["Node"],
  373. fields: {
  374. memoryIndex: {
  375. type: "Memidx"
  376. },
  377. offset: {
  378. type: "Instruction"
  379. },
  380. init: {
  381. type: "ByteArray"
  382. }
  383. }
  384. });
  385. defineType("Global", {
  386. unionType: ["Node"],
  387. fields: {
  388. globalType: {
  389. type: "GlobalType"
  390. },
  391. init: {
  392. array: true,
  393. type: "Instruction"
  394. },
  395. name: {
  396. maybe: true,
  397. type: "Identifier"
  398. }
  399. }
  400. });
  401. defineType("Table", {
  402. unionType: ["Node", "ImportDescr"],
  403. fields: {
  404. elementType: {
  405. type: "TableElementType"
  406. },
  407. limits: {
  408. assertNodeType: true,
  409. type: "Limit"
  410. },
  411. name: {
  412. maybe: true,
  413. type: "Identifier"
  414. },
  415. elements: {
  416. array: true,
  417. optional: true,
  418. type: "Index"
  419. }
  420. }
  421. });
  422. defineType("Memory", {
  423. unionType: ["Node", "ImportDescr"],
  424. fields: {
  425. limits: {
  426. type: "Limit"
  427. },
  428. id: {
  429. maybe: true,
  430. type: "Index"
  431. }
  432. }
  433. });
  434. defineType("FuncImportDescr", {
  435. unionType: ["Node", "ImportDescr"],
  436. fields: {
  437. id: {
  438. type: "Identifier"
  439. },
  440. signature: {
  441. type: "Signature"
  442. }
  443. }
  444. });
  445. defineType("ModuleImport", {
  446. unionType: ["Node"],
  447. fields: {
  448. module: {
  449. type: "string"
  450. },
  451. name: {
  452. type: "string"
  453. },
  454. descr: {
  455. type: "ImportDescr"
  456. }
  457. }
  458. });
  459. defineType("ModuleExportDescr", {
  460. unionType: ["Node"],
  461. fields: {
  462. exportType: {
  463. type: "ExportDescrType"
  464. },
  465. id: {
  466. type: "Index"
  467. }
  468. }
  469. });
  470. defineType("ModuleExport", {
  471. unionType: ["Node"],
  472. fields: {
  473. name: {
  474. type: "string"
  475. },
  476. descr: {
  477. type: "ModuleExportDescr"
  478. }
  479. }
  480. });
  481. defineType("Limit", {
  482. unionType: ["Node"],
  483. fields: {
  484. min: {
  485. type: "number"
  486. },
  487. max: {
  488. optional: true,
  489. type: "number"
  490. },
  491. // Threads proposal, shared memory
  492. shared: {
  493. optional: true,
  494. type: "boolean"
  495. }
  496. }
  497. });
  498. defineType("Signature", {
  499. unionType: ["Node"],
  500. fields: {
  501. params: {
  502. array: true,
  503. type: "FuncParam"
  504. },
  505. results: {
  506. array: true,
  507. type: "Valtype"
  508. }
  509. }
  510. });
  511. defineType("Program", {
  512. unionType: ["Node"],
  513. fields: {
  514. body: {
  515. array: true,
  516. type: "Node"
  517. }
  518. }
  519. });
  520. defineType("Identifier", {
  521. unionType: ["Node", "Expression"],
  522. fields: {
  523. value: {
  524. type: "string"
  525. },
  526. raw: {
  527. optional: true,
  528. type: "string"
  529. }
  530. }
  531. });
  532. defineType("BlockInstruction", {
  533. unionType: ["Node", "Block", "Instruction"],
  534. fields: {
  535. id: {
  536. constant: true,
  537. type: "string",
  538. value: "block"
  539. },
  540. label: {
  541. maybe: true,
  542. type: "Identifier"
  543. },
  544. instr: {
  545. array: true,
  546. type: "Instruction"
  547. },
  548. result: {
  549. maybe: true,
  550. type: "Valtype"
  551. }
  552. }
  553. });
  554. defineType("CallInstruction", {
  555. unionType: ["Node", "Instruction"],
  556. fields: {
  557. id: {
  558. constant: true,
  559. type: "string",
  560. value: "call"
  561. },
  562. index: {
  563. type: "Index"
  564. },
  565. instrArgs: {
  566. array: true,
  567. optional: true,
  568. type: "Expression"
  569. },
  570. numeric: {
  571. type: "Index",
  572. optional: true
  573. }
  574. }
  575. });
  576. defineType("CallIndirectInstruction", {
  577. unionType: ["Node", "Instruction"],
  578. fields: {
  579. id: {
  580. constant: true,
  581. type: "string",
  582. value: "call_indirect"
  583. },
  584. signature: {
  585. type: "SignatureOrTypeRef"
  586. },
  587. intrs: {
  588. array: true,
  589. optional: true,
  590. type: "Expression"
  591. }
  592. }
  593. });
  594. defineType("ByteArray", {
  595. unionType: ["Node"],
  596. fields: {
  597. values: {
  598. array: true,
  599. type: "Byte"
  600. }
  601. }
  602. });
  603. defineType("Func", {
  604. unionType: ["Node", "Block"],
  605. fields: {
  606. name: {
  607. maybe: true,
  608. type: "Index"
  609. },
  610. signature: {
  611. type: "SignatureOrTypeRef"
  612. },
  613. body: {
  614. array: true,
  615. type: "Instruction"
  616. },
  617. isExternal: {
  618. comment: "means that it has been imported from the outside js",
  619. optional: true,
  620. type: "boolean"
  621. },
  622. metadata: {
  623. optional: true,
  624. type: "FuncMetadata"
  625. }
  626. }
  627. });
  628. /**
  629. * Intrinsics
  630. */
  631. defineType("InternalBrUnless", {
  632. unionType: ["Node", "Intrinsic"],
  633. fields: {
  634. target: {
  635. type: "number"
  636. }
  637. }
  638. });
  639. defineType("InternalGoto", {
  640. unionType: ["Node", "Intrinsic"],
  641. fields: {
  642. target: {
  643. type: "number"
  644. }
  645. }
  646. });
  647. defineType("InternalCallExtern", {
  648. unionType: ["Node", "Intrinsic"],
  649. fields: {
  650. target: {
  651. type: "number"
  652. }
  653. }
  654. }); // function bodies are terminated by an `end` instruction but are missing a
  655. // return instruction
  656. //
  657. // Since we can't inject a new instruction we are injecting a new instruction.
  658. defineType("InternalEndAndReturn", {
  659. unionType: ["Node", "Intrinsic"],
  660. fields: {}
  661. });
  662. module.exports = definitions;