definitions.js 11 KB

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