wasi_api.odin 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812
  1. //+build wasm32
  2. package sys_wasi
  3. foreign import wasi "wasi_snapshot_preview1"
  4. DIRCOOKIE_START :: u64(0)
  5. size_t :: uint
  6. filesize_t :: distinct u64
  7. timestamp_t :: distinct u64
  8. clockid_t :: distinct u32
  9. CLOCK_REALTIME :: clockid_t(0)
  10. CLOCK_MONOTONIC :: clockid_t(1)
  11. CLOCK_PROCESS_CPUTIME_ID :: clockid_t(2)
  12. CLOCK_THREAD_CPUTIME_ID :: clockid_t(3)
  13. errno_t :: enum u16 {
  14. NONE = 0,
  15. // No error occurred. System call completed successfully.
  16. SUCCESS = 0,
  17. // Argument list too long.
  18. TOOBIG = 1,
  19. // Permission denied.
  20. ACCESS = 2,
  21. // Address in use.
  22. ADDRINUSE = 3,
  23. // Address not available.
  24. ADDRNOTAVAIL = 4,
  25. // Address family not supported.
  26. AFNOSUPPORT = 5,
  27. // Resource unavailable, or operation would block.
  28. AGAIN = 6,
  29. // Connection already in progress.
  30. ALREADY = 7,
  31. // Bad file descriptor.
  32. BADF = 8,
  33. // Bad message.
  34. BADMSG = 9,
  35. // Device or resource busy.
  36. BUSY = 10,
  37. // Operation canceled.
  38. CANCELED = 11,
  39. // No child processes.
  40. CHILD = 12,
  41. // Connection aborted.
  42. CONNABORTED = 13,
  43. // Connection refused.
  44. CONNREFUSED = 14,
  45. // Connection reset.
  46. CONNRESET = 15,
  47. // Resource deadlock would occur.
  48. DEADLK = 16,
  49. // Destination address required.
  50. DESTADDRREQ = 17,
  51. // Mathematics argument out of domain of function.
  52. DOM = 18,
  53. // Reserved.
  54. DQUOT = 19,
  55. // File exists.
  56. EXIST = 20,
  57. // Bad address.
  58. FAULT = 21,
  59. // File too large.
  60. FBIG = 22,
  61. // Host is unreachable.
  62. HOSTUNREACH = 23,
  63. // Identifier removed.
  64. IDRM = 24,
  65. // Illegal byte sequence.
  66. ILSEQ = 25,
  67. // Operation in progress.
  68. INPROGRESS = 26,
  69. // Interrupted function.
  70. INTR = 27,
  71. // Invalid argument.
  72. INVAL = 28,
  73. // I/O error.
  74. IO = 29,
  75. // Socket is connected.
  76. ISCONN = 30,
  77. // Is a directory.
  78. ISDIR = 31,
  79. // Too many levels of symbolic links.
  80. LOOP = 32,
  81. // File descriptor value too large.
  82. MFILE = 33,
  83. // Too many links.
  84. MLINK = 34,
  85. // Message too large.
  86. MSGSIZE = 35,
  87. // Reserved.
  88. MULTIHOP = 36,
  89. // Filename too long.
  90. NAMETOOLONG = 37,
  91. // Network is down.
  92. NETDOWN = 38,
  93. // Connection aborted by network.
  94. NETRESET = 39,
  95. // Network unreachable.
  96. NETUNREACH = 40,
  97. // Too many files open in system.
  98. NFILE = 41,
  99. // No buffer space available.
  100. NOBUFS = 42,
  101. // No such device.
  102. NODEV = 43,
  103. // No such file or directory.
  104. NOENT = 44,
  105. // Executable file format error.
  106. NOEXEC = 45,
  107. // No locks available.
  108. NOLCK = 46,
  109. // Reserved.
  110. NOLINK = 47,
  111. // Not enough space.
  112. NOMEM = 48,
  113. // No message of the desired type.
  114. NOMSG = 49,
  115. // Protocol not available.
  116. NOPROTOOPT = 50,
  117. // No space left on device.
  118. NOSPC = 51,
  119. // Function not supported.
  120. NOSYS = 52,
  121. // The socket is not connected.
  122. NOTCONN = 53,
  123. // Not a directory or a symbolic link to a directory.
  124. NOTDIR = 54,
  125. // Directory not empty.
  126. NOTEMPTY = 55,
  127. // State not recoverable.
  128. NOTRECOVERABLE = 56,
  129. // Not a socket.
  130. NOTSOCK = 57,
  131. // Not supported, or operation not supported on socket.
  132. NOTSUP = 58,
  133. // Inappropriate I/O control operation.
  134. NOTTY = 59,
  135. // No such device or address.
  136. NXIO = 60,
  137. // Value too large to be stored in data type.
  138. OVERFLOW = 61,
  139. // Previous owner died.
  140. OWNERDEAD = 62,
  141. // Operation not permitted.
  142. PERM = 63,
  143. // Broken pipe.
  144. PIPE = 64,
  145. // Protocol error.
  146. PROTO = 65,
  147. // Protocol not supported.
  148. PROTONOSUPPORT = 66,
  149. // Protocol wrong type for socket.
  150. PROTOTYPE = 67,
  151. // Result too large.
  152. RANGE = 68,
  153. // Read-only file system.
  154. ROFS = 69,
  155. // Invalid seek.
  156. SPIPE = 70,
  157. // No such process.
  158. SRCH = 71,
  159. // Reserved.
  160. STALE = 72,
  161. // Connection timed out.
  162. TIMEDOUT = 73,
  163. // Text file busy.
  164. TXTBSY = 74,
  165. // Cross-device link.
  166. XDEV = 75,
  167. // Extension: Capabilities insufficient.
  168. NOTCAPABLE = 76,
  169. }
  170. rights_t :: distinct bit_set[rights_flag_t; u64]
  171. rights_flag_t :: enum u64 {
  172. /**
  173. * The right to invoke `fd_datasync`.
  174. * If `path_open` is set, includes the right to invoke
  175. * `path_open` with `fdflags::dsync`.
  176. */
  177. FD_DATASYNC = 0,
  178. /**
  179. * The right to invoke `fd_read` and `sock_recv`.
  180. * If `rights::fd_seek` is set, includes the right to invoke `fd_pread`.
  181. */
  182. FD_READ = 1,
  183. /**
  184. * The right to invoke `fd_seek`. This flag implies `rights::fd_tell`.
  185. */
  186. FD_SEEK = 2,
  187. /**
  188. * The right to invoke `fd_fdstat_set_flags`.
  189. */
  190. FD_FDSTAT_SET_FLAGS = 3,
  191. /**
  192. * The right to invoke `fd_sync`.
  193. * If `path_open` is set, includes the right to invoke
  194. * `path_open` with `fdflags::rsync` and `fdflags::dsync`.
  195. */
  196. FD_SYNC = 4,
  197. /**
  198. * The right to invoke `fd_seek` in such a way that the file offset
  199. * remains unaltered (i.e., `whence::cur` with offset zero), or to
  200. * invoke `fd_tell`.
  201. */
  202. FD_TELL = 5,
  203. /**
  204. * The right to invoke `fd_write` and `sock_send`.
  205. * If `rights::fd_seek` is set, includes the right to invoke `fd_pwrite`.
  206. */
  207. FD_WRITE = 6,
  208. /**
  209. * The right to invoke `fd_advise`.
  210. */
  211. FD_ADVISE = 7,
  212. /**
  213. * The right to invoke `fd_allocate`.
  214. */
  215. FD_ALLOCATE = 8,
  216. /**
  217. * The right to invoke `path_create_directory`.
  218. */
  219. PATH_CREATE_DIRECTORY = 9,
  220. /**
  221. * If `path_open` is set, the right to invoke `path_open` with `oflags::creat`.
  222. */
  223. PATH_CREATE_FILE = 10,
  224. /**
  225. * The right to invoke `path_link` with the file descriptor as the
  226. * source directory.
  227. */
  228. PATH_LINK_SOURCE = 11,
  229. /**
  230. * The right to invoke `path_link` with the file descriptor as the
  231. * target directory.
  232. */
  233. PATH_LINK_TARGET = 12,
  234. /**
  235. * The right to invoke `path_open`.
  236. */
  237. PATH_OPEN = 13,
  238. /**
  239. * The right to invoke `fd_readdir`.
  240. */
  241. FD_READDIR = 14,
  242. /**
  243. * The right to invoke `path_readlink`.
  244. */
  245. PATH_READLINK = 15,
  246. /**
  247. * The right to invoke `path_rename` with the file descriptor as the source directory.
  248. */
  249. PATH_RENAME_SOURCE = 16,
  250. /**
  251. * The right to invoke `path_rename` with the file descriptor as the target directory.
  252. */
  253. PATH_RENAME_TARGET = 17,
  254. /**
  255. * The right to invoke `path_filestat_get`.
  256. */
  257. PATH_FILESTAT_GET = 18,
  258. /**
  259. * The right to change a file's size (there is no `path_filestat_set_size`).
  260. * If `path_open` is set, includes the right to invoke `path_open` with `oflags::trunc`.
  261. */
  262. PATH_FILESTAT_SET_SIZE = 19,
  263. /**
  264. * The right to invoke `path_filestat_set_times`.
  265. */
  266. PATH_FILESTAT_SET_TIMES = 20,
  267. /**
  268. * The right to invoke `fd_filestat_get`.
  269. */
  270. FD_FILESTAT_GET = 21,
  271. /**
  272. * The right to invoke `fd_filestat_set_size`.
  273. */
  274. FD_FILESTAT_SET_SIZE = 22,
  275. /**
  276. * The right to invoke `fd_filestat_set_times`.
  277. */
  278. FD_FILESTAT_SET_TIMES = 23,
  279. /**
  280. * The right to invoke `path_symlink`.
  281. */
  282. PATH_SYMLINK = 24,
  283. /**
  284. * The right to invoke `path_remove_directory`.
  285. */
  286. PATH_REMOVE_DIRECTORY = 25,
  287. /**
  288. * The right to invoke `path_unlink_file`.
  289. */
  290. PATH_UNLINK_FILE = 26,
  291. /**
  292. * If `rights::fd_read` is set, includes the right to invoke `poll_oneoff` to subscribe to `eventtype::fd_read`.
  293. * If `rights::fd_write` is set, includes the right to invoke `poll_oneoff` to subscribe to `eventtype::fd_write`.
  294. */
  295. POLL_FD_READWRITE = 27,
  296. /**
  297. * The right to invoke `sock_shutdown`.
  298. */
  299. SOCK_SHUTDOWN = 28,
  300. }
  301. fd_t :: distinct i32
  302. // iovec_t :: struct {
  303. // buf: [^]u8,
  304. // buf_len: size_t,
  305. // }
  306. // ciovec_t :: struct {
  307. // buf: [^]u8,
  308. // buf_len: size_t,
  309. // }
  310. iovec_t :: []byte
  311. ciovec_t :: []byte
  312. filedelta_t :: distinct i64
  313. whence_t :: enum u8 {
  314. SET = 0,
  315. CUR = 1,
  316. END = 2,
  317. }
  318. dircookie_t :: distinct u64
  319. dirnamlen_t :: distinct u32
  320. inode_t :: distinct u64
  321. filetype_t :: enum u8 {
  322. /**
  323. * The type of the file descriptor or file is unknown or is different from any of the other types specified.
  324. */
  325. UNKNOWN = 0,
  326. /**
  327. * The file descriptor or file refers to a block device inode.
  328. */
  329. BLOCK_DEVICE = 1,
  330. /**
  331. * The file descriptor or file refers to a character device inode.
  332. */
  333. CHARACTER_DEVICE = 2,
  334. /**
  335. * The file descriptor or file refers to a directory inode.
  336. */
  337. DIRECTORY = 3,
  338. /**
  339. * The file descriptor or file refers to a regular file inode.
  340. */
  341. REGULAR_FILE = 4,
  342. /**
  343. * The file descriptor or file refers to a datagram socket.
  344. */
  345. SOCKET_DGRAM = 5,
  346. /**
  347. * The file descriptor or file refers to a byte-stream socket.
  348. */
  349. SOCKET_STREAM = 6,
  350. /**
  351. * The file refers to a symbolic link inode.
  352. */
  353. SYMBOLIC_LINK = 7,
  354. }
  355. dirent_t :: struct {
  356. d_next: dircookie_t,
  357. d_ino: inode_t,
  358. d_namlen: dirnamlen_t,
  359. d_type: filetype_t,
  360. }
  361. advice_t :: enum u8 {
  362. /**
  363. * The application has no advice to give on its behavior with respect to the specified data.
  364. */
  365. NORMAL = 0,
  366. /**
  367. * The application expects to access the specified data sequentially from lower offsets to higher offsets.
  368. */
  369. SEQUENTIAL = 1,
  370. /**
  371. * The application expects to access the specified data in a random order.
  372. */
  373. RANDOM = 2,
  374. /**
  375. * The application expects to access the specified data in the near future.
  376. */
  377. WILLNEED = 3,
  378. /**
  379. * The application expects that it will not access the specified data in the near future.
  380. */
  381. DONTNEED = 4,
  382. /**
  383. * The application expects to access the specified data once and then not reuse it thereafter.
  384. */
  385. NOREUSE = 5,
  386. }
  387. fdflags_t :: distinct bit_set[fdflag_t; u16]
  388. fdflag_t :: enum u16 {
  389. /**
  390. * Append mode: Data written to the file is always appended to the file's end.
  391. */
  392. APPEND = 0,
  393. /**
  394. * Write according to synchronized I/O data integrity completion. Only the data stored in the file is synchronized.
  395. */
  396. DSYNC = 1,
  397. /**
  398. * Non-blocking mode.
  399. */
  400. NONBLOCK = 2,
  401. /**
  402. * Synchronized read I/O operations.
  403. */
  404. RSYNC = 3,
  405. /**
  406. * Write according to synchronized I/O file integrity completion. In
  407. * addition to synchronizing the data stored in the file, the implementation
  408. * may also synchronously update the file's metadata.
  409. */
  410. SYNC = 4,
  411. }
  412. fdstat_t :: struct {
  413. /**
  414. * File type.
  415. */
  416. fs_filetype: filetype_t,
  417. /**
  418. * File descriptor flags.
  419. */
  420. fs_flags: fdflags_t,
  421. /**
  422. * Rights that apply to this file descriptor.
  423. */
  424. fs_rights_base: rights_t,
  425. /**
  426. * Maximum set of rights that may be installed on new file descriptors that
  427. * are created through this file descriptor, e.g., through `path_open`.
  428. */
  429. fs_rights_inheriting: rights_t,
  430. }
  431. device_t :: distinct u64
  432. fstflags_t :: distinct bit_set[fstflag_t; u16]
  433. fstflag_t :: enum u16 {
  434. /**
  435. * Adjust the last data access timestamp to the value stored in `filestat::atim`.
  436. */
  437. ATIM = 0,
  438. /**
  439. * Adjust the last data access timestamp to the time of clock `clockid::realtime`.
  440. */
  441. ATIM_NOW = 1,
  442. /**
  443. * Adjust the last data modification timestamp to the value stored in `filestat::mtim`.
  444. */
  445. MTIM = 2,
  446. /**
  447. * Adjust the last data modification timestamp to the time of clock `clockid::realtime`.
  448. */
  449. MTIM_NOW = 3,
  450. }
  451. lookupflags_t :: distinct bit_set[lookupflag_t; u32]
  452. lookupflag_t :: enum u32 {
  453. /**
  454. * As long as the resolved path corresponds to a symbolic link, it is expanded.
  455. */
  456. SYMLINK_FOLLOW = 0,
  457. }
  458. oflags_t :: distinct bit_set[oflag_t; u16]
  459. oflag_t :: enum u16 {
  460. /**
  461. * Create file if it does not exist.
  462. */
  463. CREATE = 0,
  464. /**
  465. * Fail if not a directory.
  466. */
  467. DIRECTORY = 1,
  468. /**
  469. * Fail if file already exists.
  470. */
  471. EXCL = 2,
  472. /**
  473. * Truncate file to size 0.
  474. */
  475. TRUNC = 3,
  476. }
  477. linkcount_t :: distinct u64
  478. filestat_t :: struct {
  479. /**
  480. * Device ID of device containing the file.
  481. */
  482. dev: device_t,
  483. /**
  484. * File serial number.
  485. */
  486. ino: inode_t,
  487. /**
  488. * File type.
  489. */
  490. filetype: filetype_t,
  491. /**
  492. * Number of hard links to the file.
  493. */
  494. nlink: linkcount_t,
  495. /**
  496. * For regular files, the file size in bytes. For symbolic links, the length in bytes of the pathname contained in the symbolic link.
  497. */
  498. size: filesize_t,
  499. /**
  500. * Last data access timestamp.
  501. */
  502. atim: timestamp_t,
  503. /**
  504. * Last data modification timestamp.
  505. */
  506. mtim: timestamp_t,
  507. /**
  508. * Last file status change timestamp.
  509. */
  510. ctim: timestamp_t,
  511. }
  512. userdata_t :: distinct u64
  513. eventtype_t :: enum u8 {
  514. /**
  515. * The time value of clock `subscription_clock::id` has
  516. * reached timestamp `subscription_clock::timeout`.
  517. */
  518. CLOCK = 0,
  519. /**
  520. * File descriptor `subscription_fd_readwrite::file_descriptor` has data
  521. * available for reading. This event always triggers for regular files.
  522. */
  523. FD_READ = 1,
  524. /**
  525. * File descriptor `subscription_fd_readwrite::file_descriptor` has capacity
  526. * available for writing. This event always triggers for regular files.
  527. */
  528. FD_WRITE = 2,
  529. }
  530. eventrwflags_t :: distinct bit_set[eventrwflag_t; u16]
  531. eventrwflag_t :: enum u16 {
  532. /**
  533. * The peer of this socket has closed or disconnected.
  534. */
  535. FD_READWRITE_HANGUP = 0,
  536. }
  537. event_fd_readwrite_t :: struct {
  538. /**
  539. * The number of bytes available for reading or writing.
  540. */
  541. nbytes: filesize_t,
  542. /**
  543. * The state of the file descriptor.
  544. */
  545. flags: eventrwflags_t,
  546. }
  547. event_t :: struct {
  548. /**
  549. * User-provided value that got attached to `subscription::userdata`.
  550. */
  551. userdata: userdata_t,
  552. /**
  553. * If non-zero, an error that occurred while processing the subscription request.
  554. */
  555. error: errno_t,
  556. /**
  557. * The type of event that occured
  558. */
  559. type: eventtype_t,
  560. /**
  561. * The contents of the event, if it is an `eventtype::fd_read` or
  562. * `eventtype::fd_write`. `eventtype::clock` events ignore this field.
  563. */
  564. fd_readwrite: event_fd_readwrite_t,
  565. }
  566. subclockflags_t :: distinct bit_set[subclockflag_t; u16]
  567. subclockflag_t :: enum u16 {
  568. /**
  569. * If set, treat the timestamp provided in
  570. * `subscription_clock::timeout` as an absolute timestamp of clock
  571. * `subscription_clock::id`. If clear, treat the timestamp
  572. * provided in `subscription_clock::timeout` relative to the
  573. * current time value of clock `subscription_clock::id`.
  574. */
  575. SUBSCRIPTION_CLOCK_ABSTIME = 0,
  576. }
  577. subscription_clock_t :: struct {
  578. /**
  579. * The clock against which to compare the timestamp.
  580. */
  581. id: clockid_t,
  582. /**
  583. * The absolute or relative timestamp.
  584. */
  585. timeout: timestamp_t,
  586. /**
  587. * The amount of time that the implementation may wait additionally
  588. * to coalesce with other events.
  589. */
  590. precision: timestamp_t,
  591. /**
  592. * Flags specifying whether the timeout is absolute or relative
  593. */
  594. flags: subclockflags_t,
  595. }
  596. subscription_fd_readwrite_t :: struct {
  597. /**
  598. * The file descriptor on which to wait for it to become ready for reading or writing.
  599. */
  600. file_descriptor: fd_t,
  601. }
  602. subscription_t :: struct {
  603. /**
  604. * User-provided value that is attached to the subscription in the
  605. * implementation and returned through `event::userdata`.
  606. */
  607. userdata: userdata_t,
  608. /**
  609. * The type of the event to which to subscribe, and its contents
  610. */
  611. using contents: struct {
  612. tag: eventtype_t,
  613. using u: struct #raw_union {
  614. clock: subscription_clock_t,
  615. fd_read: subscription_fd_readwrite_t,
  616. fd_write: subscription_fd_readwrite_t,
  617. },
  618. },
  619. }
  620. exitcode_t :: distinct u32
  621. signal_t :: enum u8 {
  622. /**
  623. * No signal. Note that POSIX has special semantics for `kill(pid, 0)`,
  624. * so this value is reserved.
  625. */
  626. NONE = 0,
  627. /**
  628. * Hangup.
  629. * Action: Terminates the process.
  630. */
  631. HUP = 1,
  632. /**
  633. * Terminate interrupt signal.
  634. * Action: Terminates the process.
  635. */
  636. INT = 2,
  637. /**
  638. * Terminal quit signal.
  639. * Action: Terminates the process.
  640. */
  641. QUIT = 3,
  642. /**
  643. * Illegal instruction.
  644. * Action: Terminates the process.
  645. */
  646. ILL = 4,
  647. /**
  648. * Trace/breakpoint trap.
  649. * Action: Terminates the process.
  650. */
  651. TRAP = 5,
  652. /**
  653. * Process abort signal.
  654. * Action: Terminates the process.
  655. */
  656. ABRT = 6,
  657. /**
  658. * Access to an undefined portion of a memory object.
  659. * Action: Terminates the process.
  660. */
  661. BUS = 7,
  662. /**
  663. * Erroneous arithmetic operation.
  664. * Action: Terminates the process.
  665. */
  666. FPE = 8,
  667. /**
  668. * Kill.
  669. * Action: Terminates the process.
  670. */
  671. KILL = 9,
  672. /**
  673. * User-defined signal 1.
  674. * Action: Terminates the process.
  675. */
  676. USR1 = 10,
  677. /**
  678. * Invalid memory reference.
  679. * Action: Terminates the process.
  680. */
  681. SEGV = 11,
  682. /**
  683. * User-defined signal 2.
  684. * Action: Terminates the process.
  685. */
  686. USR2 = 12,
  687. /**
  688. * Write on a pipe with no one to read it.
  689. * Action: Ignored.
  690. */
  691. PIPE = 13,
  692. /**
  693. * Alarm clock.
  694. * Action: Terminates the process.
  695. */
  696. ALRM = 14,
  697. /**
  698. * Termination signal.
  699. * Action: Terminates the process.
  700. */
  701. TERM = 15,
  702. /**
  703. * Child process terminated, stopped, or continued.
  704. * Action: Ignored.
  705. */
  706. CHLD = 16,
  707. /**
  708. * Continue executing, if stopped.
  709. * Action: Continues executing, if stopped.
  710. */
  711. CONT = 17,
  712. /**
  713. * Stop executing.
  714. * Action: Stops executing.
  715. */
  716. STOP = 18,
  717. /**
  718. * Terminal stop signal.
  719. * Action: Stops executing.
  720. */
  721. TSTP = 19,
  722. /**
  723. * Background process attempting read.
  724. * Action: Stops executing.
  725. */
  726. TTIN = 20,
  727. /**
  728. * Background process attempting write.
  729. * Action: Stops executing.
  730. */
  731. TTOU = 21,
  732. /**
  733. * High bandwidth data is available at a socket.
  734. * Action: Ignored.
  735. */
  736. URG = 22,
  737. /**
  738. * CPU time limit exceeded.
  739. * Action: Terminates the process.
  740. */
  741. XCPU = 23,
  742. /**
  743. * File size limit exceeded.
  744. * Action: Terminates the process.
  745. */
  746. XFSZ = 24,
  747. /**
  748. * Virtual timer expired.
  749. * Action: Terminates the process.
  750. */
  751. VTALRM = 25,
  752. /**
  753. * Profiling timer expired.
  754. * Action: Terminates the process.
  755. */
  756. PROF = 26,
  757. /**
  758. * Window changed.
  759. * Action: Ignored.
  760. */
  761. WINCH = 27,
  762. /**
  763. * I/O possible.
  764. * Action: Terminates the process.
  765. */
  766. POLL = 28,
  767. /**
  768. * Power failure.
  769. * Action: Terminates the process.
  770. */
  771. PWR = 29,
  772. /**
  773. * Bad system call.
  774. * Action: Terminates the process.
  775. */
  776. SYS = 30,
  777. }
  778. riflags_t :: distinct bit_set[riflag_t; u16]
  779. riflag_t :: enum u16 {
  780. /**
  781. * Returns the message without removing it from the socket's receive queue.
  782. */
  783. RECV_PEEK = 0,
  784. /**
  785. * On byte-stream sockets, block until the full amount of data can be returned.
  786. */
  787. RECV_WAITALL = 1,
  788. }
  789. roflags_t :: distinct bit_set[roflag_t; u16]
  790. roflag_t :: enum u16 {
  791. /**
  792. * Returned by `sock_recv`: Message data has been truncated.
  793. */
  794. RECV_DATA_TRUNCATED = 0,
  795. }
  796. siflags_t :: distinct bit_set[siflag_t; u16]
  797. siflag_t :: enum u16 {
  798. }
  799. sdflags_t :: distinct bit_set[sdflag_t; u8]
  800. sdflag_t :: enum u8 {
  801. /**
  802. * Disables further receive operations.
  803. */
  804. RD = 0,
  805. /**
  806. * Disables further send operations.
  807. */
  808. WR = 1,
  809. }
  810. preopentype_t :: enum u8 {
  811. DIR = 0,
  812. }
  813. prestat_dir_t :: struct {
  814. pr_name_len: size_t,
  815. }
  816. prestat_t :: struct {
  817. tag: preopentype_t,
  818. using u: struct {
  819. dir: prestat_dir_t,
  820. },
  821. }
  822. @(default_calling_convention="contextless")
  823. foreign wasi {
  824. /**
  825. * Read command-line argument data.
  826. * The size of the array should match that returned by `args_sizes_get`
  827. */
  828. args_get :: proc(
  829. argv: [^]cstring,
  830. argv_buf: [^]byte,
  831. ) -> errno_t ---
  832. /**
  833. * Read environment variable data.
  834. * The sizes of the buffers should match that returned by `environ_sizes_get`.
  835. */
  836. environ_get :: proc(
  837. environ: [^]cstring,
  838. environ_buf: [^]byte,
  839. ) -> errno_t ---
  840. /**
  841. * Provide file advisory information on a file descriptor.
  842. * Note: This is similar to `posix_fadvise` in POSIX.
  843. */
  844. fd_advise :: proc(
  845. fd: fd_t,
  846. /**
  847. * The offset within the file to which the advisory applies.
  848. */
  849. offset: filesize_t,
  850. /**
  851. * The length of the region to which the advisory applies.
  852. */
  853. len: filesize_t,
  854. /**
  855. * The advice.
  856. */
  857. advice: advice_t,
  858. ) -> errno_t ---
  859. /**
  860. * Force the allocation of space in a file.
  861. * Note: This is similar to `posix_fallocate` in POSIX.
  862. */
  863. fd_allocate :: proc(
  864. fd: fd_t,
  865. /**
  866. * The offset at which to start the allocation.
  867. */
  868. offset: filesize_t,
  869. /**
  870. * The length of the area that is allocated.
  871. */
  872. len: filesize_t,
  873. ) -> errno_t ---
  874. /**
  875. * Close a file descriptor.
  876. * Note: This is similar to `close` in POSIX.
  877. */
  878. fd_close :: proc(
  879. fd: fd_t,
  880. ) -> errno_t ---
  881. /**
  882. * Synchronize the data of a file to disk.
  883. * Note: This is similar to `fdatasync` in POSIX.
  884. */
  885. fd_datasync :: proc(
  886. fd: fd_t,
  887. ) -> errno_t ---
  888. /**
  889. * Adjust the flags associated with a file descriptor.
  890. * Note: This is similar to `fcntl(fd, F_SETFL, flags)` in POSIX.
  891. */
  892. fd_fdstat_set_flags :: proc(
  893. fd: fd_t,
  894. /**
  895. * The desired values of the file descriptor flags.
  896. */
  897. flags: fdflags_t,
  898. ) -> errno_t ---
  899. /**
  900. * Adjust the rights associated with a file descriptor.
  901. * This can only be used to remove rights, and returns `errno::notcapable` if called in a way that would attempt to add rights
  902. */
  903. fd_fdstat_set_rights :: proc(
  904. fd: fd_t,
  905. /**
  906. * The desired rights of the file descriptor.
  907. */
  908. fs_rights_base: rights_t,
  909. fs_rights_inheritin: rights_t,
  910. ) -> errno_t ---
  911. /**
  912. * Adjust the size of an open file. If this increases the file's size, the extra bytes are filled with zeros.
  913. * Note: This is similar to `ftruncate` in POSIX.
  914. */
  915. fd_filestat_set_size :: proc(
  916. fd: fd_t,
  917. /**
  918. * The desired file size.
  919. */
  920. size: filesize_t,
  921. ) -> errno_t ---
  922. /**
  923. * Adjust the timestamps of an open file or directory.
  924. * Note: This is similar to `futimens` in POSIX.
  925. */
  926. fd_filestat_set_times :: proc(
  927. fd: fd_t,
  928. /**
  929. * The desired values of the data access timestamp.
  930. */
  931. atim: timestamp_t,
  932. /**
  933. * The desired values of the data modification timestamp.
  934. */
  935. mtim: timestamp_t,
  936. /**
  937. * A bitmask indicating which timestamps to adjust.
  938. */
  939. fst_flags: fstflags_t,
  940. ) -> errno_t ---
  941. /**
  942. * Atomically replace a file descriptor by renumbering another file descriptor.
  943. * Due to the strong focus on thread safety, this environment does not provide
  944. * a mechanism to duplicate or renumber a file descriptor to an arbitrary
  945. * number, like `dup2()`. This would be prone to race conditions, as an actual
  946. * file descriptor with the same number could be allocated by a different
  947. * thread at the same time.
  948. * This function provides a way to atomically renumber file descriptors, which
  949. * would disappear if `dup2()` were to be removed entirely.
  950. */
  951. fd_renumber :: proc(
  952. fd: fd_t,
  953. /**
  954. * The file descriptor to overwrite.
  955. */
  956. to: fd_t,
  957. ) -> errno_t ---
  958. /**
  959. * Synchronize the data and metadata of a file to disk.
  960. * Note: This is similar to `fsync` in POSIX.
  961. */
  962. fd_sync :: proc(
  963. f: fd_t,
  964. ) -> errno_t ---
  965. /**
  966. * Terminate the process normally. An exit code of 0 indicates successful
  967. * termination of the program. The meanings of other values is dependent on
  968. * the environment.
  969. */
  970. proc_exit :: proc(
  971. /**
  972. * The exit code returned by the process.
  973. */
  974. rval: exitcode_t,
  975. ) -> ! ---
  976. /**
  977. * Send a signal to the process of the calling thread.
  978. * Note: This is similar to `raise` in POSIX.
  979. */
  980. proc_raise :: proc(
  981. /**
  982. * The signal condition to trigger.
  983. */
  984. sig: signal_t,
  985. ) -> errno_t ---
  986. /**
  987. * Temporarily yield execution of the calling thread.
  988. * Note: This is similar to `sched_yield` in POSIX.
  989. */
  990. sched_yield :: proc() -> errno_t ---
  991. /**
  992. * Shut down socket send and receive channels.
  993. * Note: This is similar to `shutdown` in POSIX.
  994. */
  995. sock_shutdown :: proc(
  996. fd: fd_t,
  997. /**
  998. * Which channels on the socket to shut down.
  999. */
  1000. how: sdflags_t,
  1001. ) -> errno_t ---
  1002. /**
  1003. * Return a description of the given preopened file descriptor.
  1004. */
  1005. fd_prestat_dir_name :: proc(
  1006. fd: fd_t,
  1007. /**
  1008. * A buffer into which to write the preopened directory name.
  1009. */
  1010. path: []byte,
  1011. ) -> errno_t ---
  1012. /**
  1013. * Create a directory.
  1014. * Note: This is similar to `mkdirat` in POSIX.
  1015. */
  1016. path_create_directory :: proc(
  1017. fd: fd_t,
  1018. /**
  1019. * The path at which to create the directory.
  1020. */
  1021. path: string,
  1022. ) -> errno_t ---
  1023. /**
  1024. * Adjust the timestamps of a file or directory.
  1025. * Note: This is similar to `utimensat` in POSIX.
  1026. */
  1027. path_filestat_set_times :: proc(
  1028. fd: fd_t,
  1029. /**
  1030. * Flags determining the method of how the path is resolved.
  1031. */
  1032. flags: lookupflags_t,
  1033. /**
  1034. * The path of the file or directory to operate on.
  1035. */
  1036. path: string,
  1037. /**
  1038. * The desired values of the data access timestamp.
  1039. */
  1040. atim: timestamp_t,
  1041. /**
  1042. * The desired values of the data modification timestamp.
  1043. */
  1044. mtim: timestamp_t,
  1045. /**
  1046. * A bitmask indicating which timestamps to adjust.
  1047. */
  1048. fst_flags: fstflags_t,
  1049. ) -> errno_t ---
  1050. /**
  1051. * Remove a directory.
  1052. * Return `errno::notempty` if the directory is not empty.
  1053. * Note: This is similar to `unlinkat(fd, path, AT_REMOVEDIR)` in POSIX.
  1054. */
  1055. path_remove_directory :: proc(
  1056. fd: fd_t,
  1057. /**
  1058. * The path to a directory to remove.
  1059. */
  1060. path: string,
  1061. ) -> errno_t ---
  1062. /**
  1063. * Create a hard link.
  1064. * Note: This is similar to `linkat` in POSIX.
  1065. */
  1066. path_link :: proc(
  1067. old_fd: fd_t,
  1068. /**
  1069. * Flags determining the method of how the path is resolved.
  1070. */
  1071. old_flags: lookupflags_t,
  1072. /**
  1073. * The source path from which to link.
  1074. */
  1075. old_path: string,
  1076. /**
  1077. * The working directory at which the resolution of the new path starts.
  1078. */
  1079. new_fd: fd_t,
  1080. /**
  1081. * The destination path at which to create the hard link.
  1082. */
  1083. new_path: string,
  1084. ) -> errno_t ---
  1085. /**
  1086. * Rename a file or directory.
  1087. * Note: This is similar to `renameat` in POSIX.
  1088. */
  1089. path_rename :: proc(
  1090. fd: fd_t,
  1091. /**
  1092. * The source path of the file or directory to rename.
  1093. */
  1094. old_path: string,
  1095. /**
  1096. * The working directory at which the resolution of the new path starts.
  1097. */
  1098. new_fd: fd_t,
  1099. /**
  1100. * The destination path to which to rename the file or directory.
  1101. */
  1102. new_path: string,
  1103. ) -> errno_t ---
  1104. /**
  1105. * Create a symbolic link.
  1106. * Note: This is similar to `symlinkat` in POSIX.
  1107. */
  1108. path_symlink :: proc(
  1109. /**
  1110. * The contents of the symbolic link.
  1111. */
  1112. old_path: string,
  1113. fd: fd_t,
  1114. /**
  1115. * The destination path at which to create the symbolic link.
  1116. */
  1117. new_path: string,
  1118. ) -> errno_t ---
  1119. /**
  1120. * Unlink a file.
  1121. * Return `errno::isdir` if the path refers to a directory.
  1122. * Note: This is similar to `unlinkat(fd, path, 0)` in POSIX.
  1123. */
  1124. path_unlink_file :: proc(
  1125. fd: fd_t,
  1126. /**
  1127. * The path to a file to unlink.
  1128. */
  1129. path: string,
  1130. ) -> errno_t ---
  1131. /**
  1132. * Write high-quality random data into a buffer.
  1133. * This function blocks when the implementation is unable to immediately
  1134. * provide sufficient high-quality random data.
  1135. * This function may execute slowly, so when large mounts of random data are
  1136. * required, it's advisable to use this function to seed a pseudo-random
  1137. * number generator, rather than to provide the random data directly.
  1138. */
  1139. random_get :: proc(
  1140. /**
  1141. * The buffer to fill with random data.
  1142. */
  1143. buf: []u8,
  1144. ) -> errno_t ---
  1145. }
  1146. /**
  1147. * Return command-line argument data sizes.
  1148. * @return
  1149. * Returns the number of arguments and the size of the argument string
  1150. * data, or an error.
  1151. */
  1152. args_sizes_get :: proc "contextless" () -> (num_args, size_of_args: size_t, err: errno_t) {
  1153. err = wasi_args_sizes_get(&num_args, &size_of_args)
  1154. return
  1155. }
  1156. /**
  1157. * Return environment variable data sizes.
  1158. * @return
  1159. * Returns the number of environment variable arguments and the size of the
  1160. * environment variable data.
  1161. */
  1162. environ_sizes_get :: proc "contextless" () -> (num_envs, size_of_envs: size_t, err: errno_t) {
  1163. err = wasi_environ_sizes_get(&num_envs, &size_of_envs)
  1164. return
  1165. }
  1166. /**
  1167. * Return the resolution of a clock.
  1168. * Implementations are required to provide a non-zero value for supported clocks. For unsupported clocks,
  1169. * return `errno::inval`.
  1170. * Note: This is similar to `clock_getres` in POSIX.
  1171. * @return
  1172. * The resolution of the clock, or an error if one happened.
  1173. */
  1174. clock_res_get :: proc "contextless" (
  1175. /**
  1176. * The clock for which to return the resolution.
  1177. */
  1178. id: clockid_t,
  1179. ) -> (ts: timestamp_t, err: errno_t) {
  1180. err = wasi_clock_res_get(id, &ts)
  1181. return
  1182. }
  1183. /**
  1184. * Return the time value of a clock.
  1185. * Note: This is similar to `clock_gettime` in POSIX.
  1186. * @return
  1187. * The time value of the clock.
  1188. */
  1189. clock_time_get :: proc "contextless" (
  1190. /**
  1191. * The clock for which to return the time.
  1192. */
  1193. id: clockid_t,
  1194. /**
  1195. * The maximum lag (exclusive) that the returned time value may have, compared to its actual value.
  1196. */
  1197. precision: timestamp_t,
  1198. ) -> (ts: timestamp_t, err: errno_t) {
  1199. err = wasi_clock_time_get(id, precision, &ts)
  1200. return
  1201. }
  1202. /**
  1203. * Get the attributes of a file descriptor.
  1204. * Note: This returns similar flags to `fsync(fd, F_GETFL)` in POSIX, as well as additional fields.
  1205. * @return
  1206. * The buffer where the file descriptor's attributes are stored.
  1207. */
  1208. fd_fdstat_get :: proc "contextless" (
  1209. fd: fd_t,
  1210. ) -> (stat: fdstat_t, err: errno_t) {
  1211. err = wasi_fd_fdstat_get(fd, &stat)
  1212. return
  1213. }
  1214. /**
  1215. * Return the attributes of an open file.
  1216. * @return
  1217. * The buffer where the file's attributes are stored.
  1218. */
  1219. fd_filestat_get :: proc "contextless" (
  1220. fd: fd_t,
  1221. ) -> (stat: filestat_t, err: errno_t) {
  1222. err = wasi_fd_filestat_get(fd, &stat)
  1223. return
  1224. }
  1225. /**
  1226. * Read from a file descriptor, without using and updating the file descriptor's offset.
  1227. * Note: This is similar to `preadv` in POSIX.
  1228. * @return
  1229. * The number of bytes read.
  1230. */
  1231. fd_pread :: proc "contextless" (
  1232. fd: fd_t,
  1233. /**
  1234. * List of scatter/gather vectors in which to store data.
  1235. */
  1236. iovs: []iovec_t,
  1237. /**
  1238. * The offset within the file at which to read.
  1239. */
  1240. offset: filesize_t,
  1241. ) -> (n: size_t, err: errno_t) {
  1242. err = wasi_fd_pread(fd, iovs, offset, &n)
  1243. return
  1244. }
  1245. /**
  1246. * Return a description of the given preopened file descriptor.
  1247. * @return
  1248. * The buffer where the description is stored.
  1249. */
  1250. fd_prestat_get :: proc "contextless" (
  1251. fd: fd_t,
  1252. ) -> (desc: prestat_t, err: errno_t) {
  1253. err = wasi_fd_prestat_get(fd, &desc)
  1254. return
  1255. }
  1256. /**
  1257. * Write to a file descriptor, without using and updating the file descriptor's offset.
  1258. * Note: This is similar to `pwritev` in POSIX.
  1259. * @return
  1260. * The number of bytes written.
  1261. */
  1262. fd_pwrite :: proc "contextless" (
  1263. fd: fd_t,
  1264. /**
  1265. * List of scatter/gather vectors from which to retrieve data.
  1266. */
  1267. iovs: []ciovec_t,
  1268. /**
  1269. * The offset within the file at which to write.
  1270. */
  1271. offset: filesize_t,
  1272. ) -> (n: size_t, err: errno_t) {
  1273. err = wasi_fd_pwrite(fd, iovs, offset, &n)
  1274. return
  1275. }
  1276. /**
  1277. * Read from a file descriptor.
  1278. * Note: This is similar to `readv` in POSIX.
  1279. * @return
  1280. * The number of bytes read.
  1281. */
  1282. fd_read :: proc "contextless" (
  1283. fd: fd_t,
  1284. /**
  1285. * List of scatter/gather vectors to which to store data.
  1286. */
  1287. iovs: []iovec_t,
  1288. ) -> (n: size_t, err: errno_t) {
  1289. err = wasi_fd_read(fd, iovs, &n)
  1290. return
  1291. }
  1292. /**
  1293. * Read directory entries from a directory.
  1294. * When successful, the contents of the output buffer consist of a sequence of
  1295. * directory entries. Each directory entry consists of a `dirent` object,
  1296. * followed by `dirent::d_namlen` bytes holding the name of the directory
  1297. * entry.
  1298. * This function fills the output buffer as much as possible, potentially
  1299. * truncating the last directory entry. This allows the caller to grow its
  1300. * read buffer size in case it's too small to fit a single large directory
  1301. * entry, or skip the oversized directory entry.
  1302. * @return
  1303. * The number of bytes stored in the read buffer. If less than the size of the read buffer, the end of the directory has been reached.
  1304. */
  1305. fd_readdir :: proc "contextless" (
  1306. fd: fd_t,
  1307. /**
  1308. * The buffer where directory entries are stored
  1309. */
  1310. buf: []byte,
  1311. /**
  1312. * The location within the directory to start reading
  1313. */
  1314. cookie: dircookie_t,
  1315. ) -> (n: size_t, err: errno_t) {
  1316. err = wasi_fd_readdir(fd, buf, cookie, &n)
  1317. return
  1318. }
  1319. /**
  1320. * Move the offset of a file descriptor.
  1321. * Note: This is similar to `lseek` in POSIX.
  1322. * @return
  1323. * The new offset of the file descriptor, relative to the start of the file.
  1324. */
  1325. fd_seek :: proc "contextless" (
  1326. fd: fd_t,
  1327. /**
  1328. * The number of bytes to move.
  1329. */
  1330. offset: filedelta_t,
  1331. /**
  1332. * The base from which the offset is relative.
  1333. */
  1334. whence: whence_t,
  1335. ) -> (new_offset: filesize_t, err: errno_t) {
  1336. err = wasi_fd_seek(fd, offset, whence, &new_offset)
  1337. return
  1338. }
  1339. /**
  1340. * Return the current offset of a file descriptor.
  1341. * Note: This is similar to `lseek(fd, 0, SEEK_CUR)` in POSIX.
  1342. * @return
  1343. * The current offset of the file descriptor, relative to the start of the file.
  1344. */
  1345. fd_tell :: proc "contextless" (
  1346. fd: fd_t,
  1347. ) -> (offset: filesize_t, err: errno_t) {
  1348. err = wasi_fd_tell(fd, &offset)
  1349. return
  1350. }
  1351. /**
  1352. * Write to a file descriptor.
  1353. * Note: This is similar to `writev` in POSIX.
  1354. */
  1355. fd_write :: proc "contextless" (
  1356. fd: fd_t,
  1357. /**
  1358. * List of scatter/gather vectors from which to retrieve data.
  1359. */
  1360. iovs: []ciovec_t,
  1361. ) -> (n: size_t, err: errno_t) {
  1362. err = wasi_fd_write(fd, iovs, &n)
  1363. return
  1364. }
  1365. /**
  1366. * Return the attributes of a file or directory.
  1367. * Note: This is similar to `stat` in POSIX.
  1368. * @return
  1369. * The buffer where the file's attributes are stored.
  1370. */
  1371. path_filestat_get :: proc "contextless" (
  1372. fd: fd_t,
  1373. /**
  1374. * Flags determining the method of how the path is resolved.
  1375. */
  1376. flags: lookupflags_t,
  1377. /**
  1378. * The path of the file or directory to inspect.
  1379. */
  1380. path: string,
  1381. ) -> (offset: filestat_t, err: errno_t) {
  1382. err = wasi_path_filestat_get(fd, flags, path, &offset)
  1383. return
  1384. }
  1385. /**
  1386. * Open a file or directory.
  1387. * The returned file descriptor is not guaranteed to be the lowest-numbered
  1388. * file descriptor not currently open; it is randomized to prevent
  1389. * applications from depending on making assumptions about indexes, since this
  1390. * is error-prone in multi-threaded contexts. The returned file descriptor is
  1391. * guaranteed to be less than 2**31.
  1392. * Note: This is similar to `openat` in POSIX.
  1393. * @return
  1394. * The file descriptor of the file that has been opened.
  1395. */
  1396. path_open :: proc "contextless" (
  1397. fd: fd_t,
  1398. /**
  1399. * Flags determining the method of how the path is resolved.
  1400. */
  1401. dirflags: lookupflags_t,
  1402. /**
  1403. * The relative path of the file or directory to open, relative to the
  1404. * `path_open::fd` directory.
  1405. */
  1406. path: string,
  1407. /**
  1408. * The method by which to open the file.
  1409. */
  1410. oflags: oflags_t,
  1411. /**
  1412. * The initial rights of the newly created file descriptor. The
  1413. * implementation is allowed to return a file descriptor with fewer rights
  1414. * than specified, if and only if those rights do not apply to the type of
  1415. * file being opened.
  1416. * The *base* rights are rights that will apply to operations using the file
  1417. * descriptor itself, while the *inheriting* rights are rights that apply to
  1418. * file descriptors derived from it.
  1419. */
  1420. fs_rights_base: rights_t,
  1421. fs_rights_inheriting: rights_t,
  1422. fdflags: fdflags_t,
  1423. ) -> (file: fd_t, err: errno_t) {
  1424. err = wasi_path_open(fd, dirflags, path, oflags, fs_rights_base, fs_rights_inheriting, fdflags, &file)
  1425. return
  1426. }
  1427. /**
  1428. * Read the contents of a symbolic link.
  1429. * Note: This is similar to `readlinkat` in POSIX.
  1430. * @return
  1431. * The number of bytes placed in the buffer.
  1432. */
  1433. path_readlink :: proc "contextless" (
  1434. fd: fd_t,
  1435. /**
  1436. * The path of the symbolic link from which to read.
  1437. */
  1438. path: string,
  1439. /**
  1440. * The buffer to which to write the contents of the symbolic link.
  1441. */
  1442. buf: []u8,
  1443. ) -> (n: size_t, err: errno_t) {
  1444. err = wasi_path_readlink(fd, path, buf, &n)
  1445. return
  1446. }
  1447. /**
  1448. * Concurrently poll for the occurrence of a set of events.
  1449. * @return
  1450. * The number of events stored.
  1451. */
  1452. poll_oneoff :: proc "contextless" (
  1453. /**
  1454. * The events to which to subscribe.
  1455. */
  1456. subscription_in: ^subscription_t,
  1457. /**
  1458. * The events that have occurred.
  1459. */
  1460. event_out: ^event_t,
  1461. /**
  1462. * Both the number of subscriptions and events.
  1463. */
  1464. nsubscriptions: size_t,
  1465. ) -> (n: size_t, err: errno_t) {
  1466. err = wasi_poll_oneoff(subscription_in, event_out, nsubscriptions, &n)
  1467. return
  1468. }
  1469. /**
  1470. * Receive a message from a socket.
  1471. * Note: This is similar to `recv` in POSIX, though it also supports reading
  1472. * the data into multiple buffers in the manner of `readv`.
  1473. * @return
  1474. * Number of bytes stored in ri_data and message flags.
  1475. */
  1476. sock_recv :: proc "contextless" (
  1477. fd: fd_t,
  1478. /**
  1479. * List of scatter/gather vectors to which to store data.
  1480. */
  1481. ri_data: []iovec_t,
  1482. /**
  1483. * Message flags.
  1484. */
  1485. ri_flags: riflags_t,
  1486. ) -> (n: size_t, flags: roflags_t, err: errno_t) {
  1487. err = wasi_sock_recv(fd, ri_data, ri_flags, &n, &flags)
  1488. return
  1489. }
  1490. /**
  1491. * Send a message on a socket.
  1492. * Note: This is similar to `send` in POSIX, though it also supports writing
  1493. * the data from multiple buffers in the manner of `writev`.
  1494. * @return
  1495. * Number of bytes transmitted.
  1496. */
  1497. sock_send :: proc "contextless" (
  1498. fd: fd_t,
  1499. /**
  1500. * List of scatter/gather vectors to which to retrieve data
  1501. */
  1502. si_data: []ciovec_t,
  1503. /**
  1504. * Message flags.
  1505. */
  1506. si_flags: siflags_t,
  1507. ) -> (n: size_t, err: errno_t) {
  1508. err = wasi_sock_send(fd, si_data, si_flags, &n)
  1509. return
  1510. }
  1511. @(default_calling_convention="contextless")
  1512. foreign wasi {
  1513. @(link_name="args_sizes_get")
  1514. wasi_args_sizes_get :: proc(
  1515. retptr0: ^size_t,
  1516. retptr1: ^size_t,
  1517. ) -> errno_t ---
  1518. @(link_name="environ_sizes_get")
  1519. wasi_environ_sizes_get :: proc(
  1520. retptr0: ^size_t,
  1521. retptr1: ^size_t,
  1522. ) -> errno_t ---
  1523. @(link_name="clock_res_get")
  1524. wasi_clock_res_get :: proc(
  1525. id: clockid_t,
  1526. retptr0: ^timestamp_t,
  1527. ) -> errno_t ---
  1528. @(link_name="clock_time_get")
  1529. wasi_clock_time_get :: proc(
  1530. id: clockid_t,
  1531. precision: timestamp_t,
  1532. retptr0: ^timestamp_t,
  1533. ) -> errno_t ---
  1534. @(link_name="fd_fdstat_get")
  1535. wasi_fd_fdstat_get :: proc(
  1536. fd: fd_t,
  1537. retptr0: ^fdstat_t,
  1538. ) -> errno_t ---
  1539. @(link_name="fd_filestat_get")
  1540. wasi_fd_filestat_get :: proc(
  1541. fd: fd_t,
  1542. retptr0: ^filestat_t,
  1543. ) -> errno_t ---
  1544. @(link_name="fd_pread")
  1545. wasi_fd_pread :: proc(
  1546. fd: fd_t,
  1547. iovs: []iovec_t,
  1548. offset: filesize_t,
  1549. retptr0: ^size_t,
  1550. ) -> errno_t ---
  1551. @(link_name="fd_prestat_get")
  1552. wasi_fd_prestat_get :: proc(
  1553. fd: fd_t,
  1554. retptr0: ^prestat_t,
  1555. ) -> errno_t ---
  1556. @(link_name="fd_pwrite")
  1557. wasi_fd_pwrite :: proc(
  1558. fd: fd_t,
  1559. iovs: []ciovec_t,
  1560. offset: filesize_t,
  1561. retptr0: ^size_t,
  1562. ) -> errno_t ---
  1563. @(link_name="fd_read")
  1564. wasi_fd_read :: proc(
  1565. fd: fd_t,
  1566. iovs: []iovec_t,
  1567. retptr0: ^size_t,
  1568. ) -> errno_t ---
  1569. @(link_name="fd_readdir")
  1570. wasi_fd_readdir :: proc(
  1571. fd: fd_t,
  1572. buf: []u8,
  1573. cookie: dircookie_t,
  1574. retptr0: ^size_t,
  1575. ) -> errno_t ---
  1576. @(link_name="fd_seek")
  1577. wasi_fd_seek :: proc(
  1578. fd: fd_t,
  1579. offset: filedelta_t,
  1580. whence: whence_t,
  1581. retptr0: ^filesize_t,
  1582. ) -> errno_t ---
  1583. @(link_name="fd_tell")
  1584. wasi_fd_tell :: proc(
  1585. fd: fd_t,
  1586. retptr0: ^filesize_t,
  1587. ) -> errno_t ---
  1588. @(link_name="fd_write")
  1589. wasi_fd_write :: proc(
  1590. fd: fd_t,
  1591. iovs: []ciovec_t,
  1592. retptr0: ^size_t,
  1593. ) -> errno_t ---
  1594. @(link_name="path_filestat_get")
  1595. wasi_path_filestat_get :: proc(
  1596. fd: fd_t,
  1597. flags: lookupflags_t,
  1598. /**
  1599. * The path of the file or directory to inspect.
  1600. */
  1601. path: string,
  1602. retptr0: ^filestat_t,
  1603. ) -> errno_t ---
  1604. @(link_name="path_open")
  1605. wasi_path_open :: proc(
  1606. fd: fd_t,
  1607. dirflags: lookupflags_t,
  1608. path: string,
  1609. oflags: oflags_t,
  1610. fs_rights_base: rights_t,
  1611. fs_rights_inheriting: rights_t,
  1612. fdflags: fdflags_t,
  1613. retptr: ^fd_t,
  1614. ) -> errno_t ---
  1615. @(link_name="path_readlink")
  1616. wasi_path_readlink :: proc(
  1617. fd: fd_t,
  1618. path: string,
  1619. buf: []u8,
  1620. retptr0: ^size_t,
  1621. ) -> errno_t ---
  1622. @(link_name="poll_oneoff")
  1623. wasi_poll_oneoff :: proc(
  1624. subscription_in: ^subscription_t,
  1625. event_out: ^event_t,
  1626. nsubscriptions: size_t,
  1627. retptr0: ^size_t,
  1628. ) -> errno_t ---
  1629. @(link_name="sock_recv")
  1630. wasi_sock_recv :: proc(
  1631. fd: fd_t,
  1632. ri_data: []iovec_t,
  1633. ri_flags: riflags_t,
  1634. retptr0: ^size_t,
  1635. retptr1: ^roflags_t,
  1636. ) -> errno_t ---
  1637. @(link_name="sock_send")
  1638. wasi_sock_send :: proc(
  1639. fd: fd_t,
  1640. si_data: []ciovec_t,
  1641. si_flags: siflags_t,
  1642. retptr0: ^size_t,
  1643. ) -> errno_t ---
  1644. }