wasi_api.odin 38 KB

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