wasi_api.odin 40 KB

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