wasi_api.odin 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811
  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="contextless")
  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. * Return a description of the given preopened file descriptor.
  1003. */
  1004. fd_prestat_dir_name :: proc(
  1005. fd: fd_t,
  1006. /**
  1007. * A buffer into which to write the preopened directory name.
  1008. */
  1009. path: string,
  1010. ) -> errno_t ---
  1011. /**
  1012. * Create a directory.
  1013. * Note: This is similar to `mkdirat` in POSIX.
  1014. */
  1015. path_create_directory :: proc(
  1016. fd: fd_t,
  1017. /**
  1018. * The path at which to create the directory.
  1019. */
  1020. path: string,
  1021. ) -> errno_t ---
  1022. /**
  1023. * Adjust the timestamps of a file or directory.
  1024. * Note: This is similar to `utimensat` in POSIX.
  1025. */
  1026. path_filestat_set_times :: proc(
  1027. fd: fd_t,
  1028. /**
  1029. * Flags determining the method of how the path is resolved.
  1030. */
  1031. flags: lookupflags_t,
  1032. /**
  1033. * The path of the file or directory to operate on.
  1034. */
  1035. path: string,
  1036. /**
  1037. * The desired values of the data access timestamp.
  1038. */
  1039. atim: timestamp_t,
  1040. /**
  1041. * The desired values of the data modification timestamp.
  1042. */
  1043. mtim: timestamp_t,
  1044. /**
  1045. * A bitmask indicating which timestamps to adjust.
  1046. */
  1047. fst_flags: fstflags_t,
  1048. ) -> errno_t ---
  1049. /**
  1050. * Remove a directory.
  1051. * Return `errno::notempty` if the directory is not empty.
  1052. * Note: This is similar to `unlinkat(fd, path, AT_REMOVEDIR)` in POSIX.
  1053. */
  1054. path_remove_directory :: proc(
  1055. fd: fd_t,
  1056. /**
  1057. * The path to a directory to remove.
  1058. */
  1059. path: string,
  1060. ) -> errno_t ---
  1061. /**
  1062. * Create a hard link.
  1063. * Note: This is similar to `linkat` in POSIX.
  1064. */
  1065. path_link :: proc(
  1066. old_fd: fd_t,
  1067. /**
  1068. * Flags determining the method of how the path is resolved.
  1069. */
  1070. old_flags: lookupflags_t,
  1071. /**
  1072. * The source path from which to link.
  1073. */
  1074. old_path: string,
  1075. /**
  1076. * The working directory at which the resolution of the new path starts.
  1077. */
  1078. new_fd: fd_t,
  1079. /**
  1080. * The destination path at which to create the hard link.
  1081. */
  1082. new_path: string,
  1083. ) -> errno_t ---
  1084. /**
  1085. * Rename a file or directory.
  1086. * Note: This is similar to `renameat` in POSIX.
  1087. */
  1088. path_rename :: proc(
  1089. fd: fd_t,
  1090. /**
  1091. * The source path of the file or directory to rename.
  1092. */
  1093. old_path: string,
  1094. /**
  1095. * The working directory at which the resolution of the new path starts.
  1096. */
  1097. new_fd: fd_t,
  1098. /**
  1099. * The destination path to which to rename the file or directory.
  1100. */
  1101. new_path: string,
  1102. ) -> errno_t ---
  1103. /**
  1104. * Create a symbolic link.
  1105. * Note: This is similar to `symlinkat` in POSIX.
  1106. */
  1107. path_symlink :: proc(
  1108. /**
  1109. * The contents of the symbolic link.
  1110. */
  1111. old_path: string,
  1112. fd: fd_t,
  1113. /**
  1114. * The destination path at which to create the symbolic link.
  1115. */
  1116. new_path: string,
  1117. ) -> errno_t ---
  1118. /**
  1119. * Unlink a file.
  1120. * Return `errno::isdir` if the path refers to a directory.
  1121. * Note: This is similar to `unlinkat(fd, path, 0)` in POSIX.
  1122. */
  1123. path_unlink_file :: proc(
  1124. fd: fd_t,
  1125. /**
  1126. * The path to a file to unlink.
  1127. */
  1128. path: string,
  1129. ) -> errno_t ---
  1130. /**
  1131. * Write high-quality random data into a buffer.
  1132. * This function blocks when the implementation is unable to immediately
  1133. * provide sufficient high-quality random data.
  1134. * This function may execute slowly, so when large mounts of random data are
  1135. * required, it's advisable to use this function to seed a pseudo-random
  1136. * number generator, rather than to provide the random data directly.
  1137. */
  1138. random_get :: proc(
  1139. /**
  1140. * The buffer to fill with random data.
  1141. */
  1142. buf: []u8,
  1143. ) -> errno_t ---
  1144. }
  1145. /**
  1146. * Return command-line argument data sizes.
  1147. * @return
  1148. * Returns the number of arguments and the size of the argument string
  1149. * data, or an error.
  1150. */
  1151. args_sizes_get :: proc "contextless" () -> (num_args, size_of_args: size_t, err: errno_t) {
  1152. err = wasi_args_sizes_get(&num_args, &size_of_args)
  1153. return
  1154. }
  1155. /**
  1156. * Return environment variable data sizes.
  1157. * @return
  1158. * Returns the number of environment variable arguments and the size of the
  1159. * environment variable data.
  1160. */
  1161. environ_sizes_get :: proc "contextless" () -> (num_envs, size_of_envs: size_t, err: errno_t) {
  1162. err = wasi_environ_sizes_get(&num_envs, &size_of_envs)
  1163. return
  1164. }
  1165. /**
  1166. * Return the resolution of a clock.
  1167. * Implementations are required to provide a non-zero value for supported clocks. For unsupported clocks,
  1168. * return `errno::inval`.
  1169. * Note: This is similar to `clock_getres` in POSIX.
  1170. * @return
  1171. * The resolution of the clock, or an error if one happened.
  1172. */
  1173. clock_res_get :: proc "contextless" (
  1174. /**
  1175. * The clock for which to return the resolution.
  1176. */
  1177. id: clockid_t,
  1178. ) -> (ts: timestamp_t, err: errno_t) {
  1179. err = wasi_clock_res_get(id, &ts)
  1180. return
  1181. }
  1182. /**
  1183. * Return the time value of a clock.
  1184. * Note: This is similar to `clock_gettime` in POSIX.
  1185. * @return
  1186. * The time value of the clock.
  1187. */
  1188. clock_time_get :: proc "contextless" (
  1189. /**
  1190. * The clock for which to return the time.
  1191. */
  1192. id: clockid_t,
  1193. /**
  1194. * The maximum lag (exclusive) that the returned time value may have, compared to its actual value.
  1195. */
  1196. precision: timestamp_t,
  1197. ) -> (ts: timestamp_t, err: errno_t) {
  1198. err = wasi_clock_time_get(id, precision, &ts)
  1199. return
  1200. }
  1201. /**
  1202. * Get the attributes of a file descriptor.
  1203. * Note: This returns similar flags to `fsync(fd, F_GETFL)` in POSIX, as well as additional fields.
  1204. * @return
  1205. * The buffer where the file descriptor's attributes are stored.
  1206. */
  1207. fd_fdstat_get :: proc "contextless" (
  1208. fd: fd_t,
  1209. ) -> (stat: fdstat_t, err: errno_t) {
  1210. err = wasi_fd_fdstat_get(fd, &stat)
  1211. return
  1212. }
  1213. /**
  1214. * Return the attributes of an open file.
  1215. * @return
  1216. * The buffer where the file's attributes are stored.
  1217. */
  1218. fd_filestat_get :: proc "contextless" (
  1219. fd: fd_t,
  1220. ) -> (stat: filestat_t, err: errno_t) {
  1221. err = wasi_fd_filestat_get(fd, &stat)
  1222. return
  1223. }
  1224. /**
  1225. * Read from a file descriptor, without using and updating the file descriptor's offset.
  1226. * Note: This is similar to `preadv` in POSIX.
  1227. * @return
  1228. * The number of bytes read.
  1229. */
  1230. fd_pread :: proc "contextless" (
  1231. fd: fd_t,
  1232. /**
  1233. * List of scatter/gather vectors in which to store data.
  1234. */
  1235. iovs: []iovec_t,
  1236. /**
  1237. * The offset within the file at which to read.
  1238. */
  1239. offset: filesize_t,
  1240. ) -> (n: size_t, err: errno_t) {
  1241. err = wasi_fd_pread(fd, iovs, offset, &n)
  1242. return
  1243. }
  1244. /**
  1245. * Return a description of the given preopened file descriptor.
  1246. * @return
  1247. * The buffer where the description is stored.
  1248. */
  1249. fd_prestat_get :: proc "contextless" (
  1250. fd: fd_t,
  1251. ) -> (desc: prestat_t, err: errno_t) {
  1252. err = wasi_fd_prestat_get(fd, &desc)
  1253. return
  1254. }
  1255. /**
  1256. * Write to a file descriptor, without using and updating the file descriptor's offset.
  1257. * Note: This is similar to `pwritev` in POSIX.
  1258. * @return
  1259. * The number of bytes written.
  1260. */
  1261. fd_pwrite :: proc "contextless" (
  1262. fd: fd_t,
  1263. /**
  1264. * List of scatter/gather vectors from which to retrieve data.
  1265. */
  1266. iovs: []ciovec_t,
  1267. /**
  1268. * The offset within the file at which to write.
  1269. */
  1270. offset: filesize_t,
  1271. ) -> (n: size_t, err: errno_t) {
  1272. err = wasi_fd_pwrite(fd, iovs, offset, &n)
  1273. return
  1274. }
  1275. /**
  1276. * Read from a file descriptor.
  1277. * Note: This is similar to `readv` in POSIX.
  1278. * @return
  1279. * The number of bytes read.
  1280. */
  1281. fd_read :: proc "contextless" (
  1282. fd: fd_t,
  1283. /**
  1284. * List of scatter/gather vectors to which to store data.
  1285. */
  1286. iovs: []iovec_t,
  1287. ) -> (n: size_t, err: errno_t) {
  1288. err = wasi_fd_read(fd, iovs, &n)
  1289. return
  1290. }
  1291. /**
  1292. * Read directory entries from a directory.
  1293. * When successful, the contents of the output buffer consist of a sequence of
  1294. * directory entries. Each directory entry consists of a `dirent` object,
  1295. * followed by `dirent::d_namlen` bytes holding the name of the directory
  1296. * entry.
  1297. * This function fills the output buffer as much as possible, potentially
  1298. * truncating the last directory entry. This allows the caller to grow its
  1299. * read buffer size in case it's too small to fit a single large directory
  1300. * entry, or skip the oversized directory entry.
  1301. * @return
  1302. * 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.
  1303. */
  1304. fd_readdir :: proc "contextless" (
  1305. fd: fd_t,
  1306. /**
  1307. * The buffer where directory entries are stored
  1308. */
  1309. buf: []byte,
  1310. /**
  1311. * The location within the directory to start reading
  1312. */
  1313. cookie: dircookie_t,
  1314. ) -> (n: size_t, err: errno_t) {
  1315. err = wasi_fd_readdir(fd, buf, cookie, &n)
  1316. return
  1317. }
  1318. /**
  1319. * Move the offset of a file descriptor.
  1320. * Note: This is similar to `lseek` in POSIX.
  1321. * @return
  1322. * The new offset of the file descriptor, relative to the start of the file.
  1323. */
  1324. fd_seek :: proc "contextless" (
  1325. fd: fd_t,
  1326. /**
  1327. * The number of bytes to move.
  1328. */
  1329. offset: filedelta_t,
  1330. /**
  1331. * The base from which the offset is relative.
  1332. */
  1333. whence: whence_t,
  1334. ) -> (new_offset: filesize_t, err: errno_t) {
  1335. err = wasi_fd_seek(fd, offset, whence, &new_offset)
  1336. return
  1337. }
  1338. /**
  1339. * Return the current offset of a file descriptor.
  1340. * Note: This is similar to `lseek(fd, 0, SEEK_CUR)` in POSIX.
  1341. * @return
  1342. * The current offset of the file descriptor, relative to the start of the file.
  1343. */
  1344. fd_tell :: proc "contextless" (
  1345. fd: fd_t,
  1346. ) -> (offset: filesize_t, err: errno_t) {
  1347. err = wasi_fd_tell(fd, &offset)
  1348. return
  1349. }
  1350. /**
  1351. * Write to a file descriptor.
  1352. * Note: This is similar to `writev` in POSIX.
  1353. */
  1354. fd_write :: proc "contextless" (
  1355. fd: fd_t,
  1356. /**
  1357. * List of scatter/gather vectors from which to retrieve data.
  1358. */
  1359. iovs: []ciovec_t,
  1360. ) -> (n: size_t, err: errno_t) {
  1361. err = wasi_fd_write(fd, iovs, &n)
  1362. return
  1363. }
  1364. /**
  1365. * Return the attributes of a file or directory.
  1366. * Note: This is similar to `stat` in POSIX.
  1367. * @return
  1368. * The buffer where the file's attributes are stored.
  1369. */
  1370. path_filestat_get :: proc "contextless" (
  1371. fd: fd_t,
  1372. /**
  1373. * Flags determining the method of how the path is resolved.
  1374. */
  1375. flags: lookupflags_t,
  1376. /**
  1377. * The path of the file or directory to inspect.
  1378. */
  1379. path: string,
  1380. ) -> (offset: filestat_t, err: errno_t) {
  1381. err = wasi_path_filestat_get(fd, flags, path, &offset)
  1382. return
  1383. }
  1384. /**
  1385. * Open a file or directory.
  1386. * The returned file descriptor is not guaranteed to be the lowest-numbered
  1387. * file descriptor not currently open; it is randomized to prevent
  1388. * applications from depending on making assumptions about indexes, since this
  1389. * is error-prone in multi-threaded contexts. The returned file descriptor is
  1390. * guaranteed to be less than 2**31.
  1391. * Note: This is similar to `openat` in POSIX.
  1392. * @return
  1393. * The file descriptor of the file that has been opened.
  1394. */
  1395. path_open :: proc "contextless" (
  1396. fd: fd_t,
  1397. /**
  1398. * Flags determining the method of how the path is resolved.
  1399. */
  1400. dirflags: lookupflags_t,
  1401. /**
  1402. * The relative path of the file or directory to open, relative to the
  1403. * `path_open::fd` directory.
  1404. */
  1405. path: string,
  1406. /**
  1407. * The method by which to open the file.
  1408. */
  1409. oflags: oflags_t,
  1410. /**
  1411. * The initial rights of the newly created file descriptor. The
  1412. * implementation is allowed to return a file descriptor with fewer rights
  1413. * than specified, if and only if those rights do not apply to the type of
  1414. * file being opened.
  1415. * The *base* rights are rights that will apply to operations using the file
  1416. * descriptor itself, while the *inheriting* rights are rights that apply to
  1417. * file descriptors derived from it.
  1418. */
  1419. fs_rights_base: rights_t,
  1420. fs_rights_inheriting: rights_t,
  1421. fdflags: fdflags_t,
  1422. ) -> (file: fd_t, err: errno_t) {
  1423. err = wasi_path_open(fd, dirflags, path, oflags, fs_rights_base, fs_rights_inheriting, fdflags, &file)
  1424. return
  1425. }
  1426. /**
  1427. * Read the contents of a symbolic link.
  1428. * Note: This is similar to `readlinkat` in POSIX.
  1429. * @return
  1430. * The number of bytes placed in the buffer.
  1431. */
  1432. path_readlink :: proc "contextless" (
  1433. fd: fd_t,
  1434. /**
  1435. * The path of the symbolic link from which to read.
  1436. */
  1437. path: string,
  1438. /**
  1439. * The buffer to which to write the contents of the symbolic link.
  1440. */
  1441. buf: []u8,
  1442. ) -> (n: size_t, err: errno_t) {
  1443. err = wasi_path_readlink(fd, path, buf, &n)
  1444. return
  1445. }
  1446. /**
  1447. * Concurrently poll for the occurrence of a set of events.
  1448. * @return
  1449. * The number of events stored.
  1450. */
  1451. poll_oneoff :: proc "contextless" (
  1452. /**
  1453. * The events to which to subscribe.
  1454. */
  1455. subscription_in: ^subscription_t,
  1456. /**
  1457. * The events that have occurred.
  1458. */
  1459. event_out: ^event_t,
  1460. /**
  1461. * Both the number of subscriptions and events.
  1462. */
  1463. nsubscriptions: size_t,
  1464. ) -> (n: size_t, err: errno_t) {
  1465. err = wasi_poll_oneoff(subscription_in, event_out, nsubscriptions, &n)
  1466. return
  1467. }
  1468. /**
  1469. * Receive a message from a socket.
  1470. * Note: This is similar to `recv` in POSIX, though it also supports reading
  1471. * the data into multiple buffers in the manner of `readv`.
  1472. * @return
  1473. * Number of bytes stored in ri_data and message flags.
  1474. */
  1475. sock_recv :: proc "contextless" (
  1476. fd: fd_t,
  1477. /**
  1478. * List of scatter/gather vectors to which to store data.
  1479. */
  1480. ri_data: []iovec_t,
  1481. /**
  1482. * Message flags.
  1483. */
  1484. ri_flags: riflags_t,
  1485. ) -> (n: size_t, flags: roflags_t, err: errno_t) {
  1486. err = wasi_sock_recv(fd, ri_data, ri_flags, &n, &flags)
  1487. return
  1488. }
  1489. /**
  1490. * Send a message on a socket.
  1491. * Note: This is similar to `send` in POSIX, though it also supports writing
  1492. * the data from multiple buffers in the manner of `writev`.
  1493. * @return
  1494. * Number of bytes transmitted.
  1495. */
  1496. sock_send :: proc "contextless" (
  1497. fd: fd_t,
  1498. /**
  1499. * List of scatter/gather vectors to which to retrieve data
  1500. */
  1501. si_data: []ciovec_t,
  1502. /**
  1503. * Message flags.
  1504. */
  1505. si_flags: siflags_t,
  1506. ) -> (n: size_t, err: errno_t) {
  1507. err = wasi_sock_send(fd, si_data, si_flags, &n)
  1508. return
  1509. }
  1510. @(default_calling_convention="contextless")
  1511. foreign wasi {
  1512. @(link_name="args_sizes_get")
  1513. wasi_args_sizes_get :: proc(
  1514. retptr0: ^size_t,
  1515. retptr1: ^size_t,
  1516. ) -> errno_t ---
  1517. @(link_name="environ_sizes_get")
  1518. wasi_environ_sizes_get :: proc(
  1519. retptr0: ^size_t,
  1520. retptr1: ^size_t,
  1521. ) -> errno_t ---
  1522. @(link_name="clock_res_get")
  1523. wasi_clock_res_get :: proc(
  1524. id: clockid_t,
  1525. retptr0: ^timestamp_t,
  1526. ) -> errno_t ---
  1527. @(link_name="clock_time_get")
  1528. wasi_clock_time_get :: proc(
  1529. id: clockid_t,
  1530. precision: timestamp_t,
  1531. retptr0: ^timestamp_t,
  1532. ) -> errno_t ---
  1533. @(link_name="fd_fdstat_get")
  1534. wasi_fd_fdstat_get :: proc(
  1535. fd: fd_t,
  1536. retptr0: ^fdstat_t,
  1537. ) -> errno_t ---
  1538. @(link_name="fd_filestat_get")
  1539. wasi_fd_filestat_get :: proc(
  1540. fd: fd_t,
  1541. retptr0: ^filestat_t,
  1542. ) -> errno_t ---
  1543. @(link_name="fd_pread")
  1544. wasi_fd_pread :: proc(
  1545. fd: fd_t,
  1546. iovs: []iovec_t,
  1547. offset: filesize_t,
  1548. retptr0: ^size_t,
  1549. ) -> errno_t ---
  1550. @(link_name="fd_prestat_get")
  1551. wasi_fd_prestat_get :: proc(
  1552. fd: fd_t,
  1553. retptr0: ^prestat_t,
  1554. ) -> errno_t ---
  1555. @(link_name="fd_pwrite")
  1556. wasi_fd_pwrite :: proc(
  1557. fd: fd_t,
  1558. iovs: []ciovec_t,
  1559. offset: filesize_t,
  1560. retptr0: ^size_t,
  1561. ) -> errno_t ---
  1562. @(link_name="fd_read")
  1563. wasi_fd_read :: proc(
  1564. fd: fd_t,
  1565. iovs: []iovec_t,
  1566. retptr0: ^size_t,
  1567. ) -> errno_t ---
  1568. @(link_name="fd_readdir")
  1569. wasi_fd_readdir :: proc(
  1570. fd: fd_t,
  1571. buf: []u8,
  1572. cookie: dircookie_t,
  1573. retptr0: ^size_t,
  1574. ) -> errno_t ---
  1575. @(link_name="fd_seek")
  1576. wasi_fd_seek :: proc(
  1577. fd: fd_t,
  1578. offset: filedelta_t,
  1579. whence: whence_t,
  1580. retptr0: ^filesize_t,
  1581. ) -> errno_t ---
  1582. @(link_name="fd_tell")
  1583. wasi_fd_tell :: proc(
  1584. fd: fd_t,
  1585. retptr0: ^filesize_t,
  1586. ) -> errno_t ---
  1587. @(link_name="fd_write")
  1588. wasi_fd_write :: proc(
  1589. fd: fd_t,
  1590. iovs: []ciovec_t,
  1591. retptr0: ^size_t,
  1592. ) -> errno_t ---
  1593. @(link_name="path_filestat_get")
  1594. wasi_path_filestat_get :: proc(
  1595. fd: fd_t,
  1596. flags: lookupflags_t,
  1597. /**
  1598. * The path of the file or directory to inspect.
  1599. */
  1600. path: string,
  1601. retptr0: ^filestat_t,
  1602. ) -> errno_t ---
  1603. @(link_name="path_open")
  1604. wasi_path_open :: proc(
  1605. fd: fd_t,
  1606. dirflags: lookupflags_t,
  1607. path: string,
  1608. oflags: oflags_t,
  1609. fs_rights_base: rights_t,
  1610. fs_rights_inheriting: rights_t,
  1611. fdflags: fdflags_t,
  1612. retptr: ^fd_t,
  1613. ) -> errno_t ---
  1614. @(link_name="path_readlink")
  1615. wasi_path_readlink :: proc(
  1616. fd: fd_t,
  1617. path: string,
  1618. buf: []u8,
  1619. retptr0: ^size_t,
  1620. ) -> errno_t ---
  1621. @(link_name="poll_oneoff")
  1622. wasi_poll_oneoff :: proc(
  1623. subscription_in: ^subscription_t,
  1624. event_out: ^event_t,
  1625. nsubscriptions: size_t,
  1626. retptr0: ^size_t,
  1627. ) -> errno_t ---
  1628. @(link_name="sock_recv")
  1629. wasi_sock_recv :: proc(
  1630. fd: fd_t,
  1631. ri_data: []iovec_t,
  1632. ri_flags: riflags_t,
  1633. retptr0: ^size_t,
  1634. retptr1: ^roflags_t,
  1635. ) -> errno_t ---
  1636. @(link_name="sock_send")
  1637. wasi_sock_send :: proc(
  1638. fd: fd_t,
  1639. si_data: []ciovec_t,
  1640. si_flags: siflags_t,
  1641. retptr0: ^size_t,
  1642. ) -> errno_t ---
  1643. }