Skip to content

Commit b97531f

Browse files
committed
Modularize the WASI API.
This splits what was previously "preview" into separate modules. There's a lot of API evolution still to be done here, and the groupings presented here may change, but this should help get things started.
1 parent d07615f commit b97531f

12 files changed

+632
-2673
lines changed

phases/ephemeral/docs/wasi_ephemeral_preview.md

Lines changed: 0 additions & 2150 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
;; WASI Command-line Arguments.
2+
;;
3+
;; This is a `witx` file. See [here](https://github.com/WebAssembly/WASI/tree/master/docs/witx.md)
4+
;; for an explanation of what that means.
5+
6+
(use "typenames.witx")
7+
8+
(module $wasi_ephemeral_args
9+
;;; Linear memory to be accessed by WASI functions that need it.
10+
(import "memory" (memory))
11+
12+
;;; Read command-line argument data.
13+
;;; The size of the array should match that returned by `sizes_get`
14+
(@interface func (export "get")
15+
(param $argv (@witx pointer (@witx pointer u8)))
16+
(param $argv_buf (@witx pointer u8))
17+
(result $error $errno)
18+
)
19+
20+
;;; Return command-line argument data sizes.
21+
(@interface func (export "sizes_get")
22+
(result $error $errno)
23+
;;; The number of arguments.
24+
(result $argc $size)
25+
;;; The size of the argument string data.
26+
(result $argv_buf_size $size)
27+
)
28+
)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
;; WASI Clocks.
2+
;;
3+
;; Some content here is derived from [CloudABI](https://github.com/NuxiNL/cloudabi).
4+
;;
5+
;; This is a `witx` file. See [here](https://github.com/WebAssembly/WASI/tree/master/docs/witx.md)
6+
;; for an explanation of what that means.
7+
8+
(use "typenames.witx")
9+
10+
(module $wasi_ephemeral_clock
11+
;;; Linear memory to be accessed by WASI functions that need it.
12+
(import "memory" (memory))
13+
14+
;;; Return the resolution of a clock.
15+
;;; Implementations are required to provide a non-zero value for supported clocks. For unsupported clocks, return `WASI_EINVAL`
16+
;;; Note: This is similar to `clock_getres` in POSIX.
17+
(@interface func (export "res_get")
18+
(result $error $errno)
19+
;;; The clock for which to return the resolution.
20+
(param $id $clockid)
21+
;;; The resolution of the clock.
22+
(result $resolution $timestamp)
23+
)
24+
25+
;;; Return the time value of a clock.
26+
;;; Note: This is similar to `clock_gettime` in POSIX.
27+
(@interface func (export "time_get")
28+
;;; The clock for which to return the time.
29+
(param $id $clockid)
30+
;;; The maximum lag (exclusive) that the returned time value may have, compared to its actual value.
31+
(param $precision $timestamp)
32+
(result $error $errno)
33+
;;; The time value of the clock.
34+
(result $time $timestamp)
35+
)
36+
)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
;; WASI Environment Variables.
2+
;;
3+
;; This is a `witx` file. See [here](https://github.com/WebAssembly/WASI/tree/master/docs/witx.md)
4+
;; for an explanation of what that means.
5+
6+
(use "typenames.witx")
7+
8+
(module $wasi_ephemeral_environ
9+
;;; Linear memory to be accessed by WASI functions that need it.
10+
(import "memory" (memory))
11+
12+
;;; Read environment variable data.
13+
;;; The sizes of the buffers should match that returned by `sizes_get`.
14+
(@interface func (export "get")
15+
(param $environ (@witx pointer (@witx pointer u8)))
16+
(param $environ_buf (@witx pointer u8))
17+
(result $error $errno)
18+
)
19+
20+
;;; Return command-line argument data sizes.
21+
(@interface func (export "sizes_get")
22+
(result $error $errno)
23+
;;; The number of arguments.
24+
(result $argc $size)
25+
;;; The size of the argument string data.
26+
(result $argv_buf_size $size)
27+
)
28+
)
Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
;; WASI I/O.
2+
;;
3+
;; Some content here is derived from [CloudABI](https://github.com/NuxiNL/cloudabi).
4+
;;
5+
;; This is a `witx` file. See [here](https://github.com/WebAssembly/WASI/tree/master/docs/witx.md)
6+
;; for an explanation of what that means.
7+
8+
(use "typenames.witx")
9+
10+
(module $wasi_ephemeral_fd
11+
;;; Linear memory to be accessed by WASI functions that need it.
12+
(import "memory" (memory))
13+
14+
;;; Provide file advisory information on a file descriptor.
15+
;;; Note: This is similar to `posix_fadvise` in POSIX.
16+
(@interface func (export "advise")
17+
(param $fd $fd)
18+
;;; The offset within the file to which the advisory applies.
19+
(param $offset $filesize)
20+
;;; The length of the region to which the advisory applies.
21+
(param $len $filesize)
22+
;;; The advice.
23+
(param $advice $advice)
24+
(result $error $errno)
25+
)
26+
27+
;;; Force the allocation of space in a file.
28+
;;; Note: This is similar to `posix_fallocate` in POSIX.
29+
(@interface func (export "allocate")
30+
(param $fd $fd)
31+
;;; The offset at which to start the allocation.
32+
(param $offset $filesize)
33+
;;; The length of the area that is allocated.
34+
(param $len $filesize)
35+
(result $error $errno)
36+
)
37+
38+
;;; Close a file descriptor.
39+
;;; Note: This is similar to `close` in POSIX.
40+
(@interface func (export "close")
41+
(param $fd $fd)
42+
(result $error $errno)
43+
)
44+
45+
;;; Synchronize the data of a file to disk.
46+
;;; Note: This is similar to `fdatasync` in POSIX.
47+
(@interface func (export "datasync")
48+
(param $fd $fd)
49+
(result $error $errno)
50+
)
51+
52+
;;; Get the attributes of a file descriptor.
53+
;;; Note: This returns similar flags to `fsync(fd, F_GETFL)` in POSIX, as well as additional fields.
54+
(@interface func (export "fdstat_get")
55+
(param $fd $fd)
56+
(result $error $errno)
57+
;;; The buffer where the file descriptor's attributes are stored.
58+
(result $stat $fdstat)
59+
)
60+
61+
;;; Adjust the flags associated with a file descriptor.
62+
;;; Note: This is similar to `fcntl(fd, F_SETFL, flags)` in POSIX.
63+
(@interface func (export "fdstat_set_flags")
64+
(param $fd $fd)
65+
;;; The desired values of the file descriptor flags.
66+
(param $flags $fdflags)
67+
(result $error $errno)
68+
)
69+
70+
;;; Adjust the rights associated with a file descriptor.
71+
;;; This can only be used to remove rights, and returns `ENOTCAPABLE` if called in a way that would attempt to add rights
72+
(@interface func (export "fdstat_set_rights")
73+
(param $fd $fd)
74+
;;; The desired rights of the file descriptor.
75+
(param $fs_rights_base $rights)
76+
(param $fs_rights_inheriting $rights)
77+
(result $error $errno)
78+
)
79+
80+
;;; Return the attributes of an open file.
81+
(@interface func (export "filestat_get")
82+
(param $fd $fd)
83+
(result $error $errno)
84+
;;; The buffer where the file's attributes are stored.
85+
(result $buf $filestat)
86+
)
87+
88+
;;; Adjust the size of an open file. If this increases the file's size, the extra bytes are filled with zeros.
89+
;;; Note: This is similar to `ftruncate` in POSIX.
90+
(@interface func (export "filestat_set_size")
91+
(param $fd $fd)
92+
;;; The desired file size.
93+
(param $size $filesize)
94+
(result $error $errno)
95+
)
96+
97+
;;; Adjust the timestamps of an open file or directory.
98+
;;; Note: This is similar to `futimens` in POSIX.
99+
(@interface func (export "filestat_set_times")
100+
(param $fd $fd)
101+
;;; The desired values of the data access timestamp.
102+
(param $atim $timestamp)
103+
;;; The desired values of the data modification timestamp.
104+
(param $mtim $timestamp)
105+
;;; A bitmask indicating which timestamps to adjust.
106+
(param $fst_flags $fstflags)
107+
(result $error $errno)
108+
)
109+
110+
;;; Read from a file descriptor, without using and updating the file descriptor's offset.
111+
;;; Note: This is similar to `preadv` in POSIX.
112+
(@interface func (export "pread")
113+
(param $fd $fd)
114+
;;; List of scatter/gather vectors in which to store data.
115+
(param $iovs $iovec_array)
116+
;;; The offset within the file at which to read.
117+
(param $offset $filesize)
118+
(result $error $errno)
119+
;;; The number of bytes read.
120+
(result $nread $size)
121+
)
122+
123+
;;; Return a description of the given preopened file descriptor.
124+
(@interface func (export "prestat_get")
125+
(param $fd $fd)
126+
(result $error $errno)
127+
;;; The buffer where the description is stored.
128+
(result $buf $prestat)
129+
)
130+
131+
;;; Return a description of the given preopened file descriptor.
132+
(@interface func (export "prestat_dir_name")
133+
(param $fd $fd)
134+
;;; A buffer into which to write the preopened directory name.
135+
(param $path (@witx pointer u8))
136+
(param $path_len $size)
137+
(result $error $errno)
138+
)
139+
140+
;;; Write to a file descriptor, without using and updating the file descriptor's offset.
141+
;;; Note: This is similar to `pwritev` in POSIX.
142+
(@interface func (export "pwrite")
143+
(param $fd $fd)
144+
;;; List of scatter/gather vectors from which to retrieve data.
145+
(param $iovs $ciovec_array)
146+
;;; The offset within the file at which to write.
147+
(param $offset $filesize)
148+
(result $error $errno)
149+
;;; The number of bytes written.
150+
(result $nwritten $size)
151+
)
152+
153+
;;; Read from a file descriptor.
154+
;;; Note: This is similar to `readv` in POSIX.
155+
(@interface func (export "read")
156+
(param $fd $fd)
157+
;;; List of scatter/gather vectors to which to store data.
158+
(param $iovs $iovec_array)
159+
(result $error $errno)
160+
;;; The number of bytes read.
161+
(result $nread $size)
162+
)
163+
164+
;;; Read directory entries from a directory.
165+
;;; When successful, the contents of the output buffer consist of a sequence of
166+
;;; directory entries. Each directory entry consists of a dirent_t object,
167+
;;; followed by dirent_t::d_namlen bytes holding the name of the directory
168+
;;; entry.
169+
;;
170+
;;; This function fills the output buffer as much as possible, potentially
171+
;;; truncating the last directory entry. This allows the caller to grow its
172+
;;; read buffer size in case it's too small to fit a single large directory
173+
;;; entry, or skip the oversized directory entry.
174+
(@interface func (export "readdir")
175+
(param $fd $fd)
176+
;;; The buffer where directory entries are stored
177+
(param $buf (@witx pointer u8))
178+
(param $buf_len $size)
179+
;;; The location within the directory to start reading
180+
(param $cookie $dircookie)
181+
(result $error $errno)
182+
;;; 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.
183+
(result $bufused $size)
184+
)
185+
186+
;;; Atomically replace a file descriptor by renumbering another file descriptor.
187+
;;
188+
;;; Due to the strong focus on thread safety, this environment does not provide
189+
;;; a mechanism to duplicate or renumber a file descriptor to an arbitrary
190+
;;; number, like `dup2()`. This would be prone to race conditions, as an actual
191+
;;; file descriptor with the same number could be allocated by a different
192+
;;; thread at the same time.
193+
;;
194+
;;; This function provides a way to atomically renumber file descriptors, which
195+
;;; would disappear if `dup2()` were to be removed entirely.
196+
(@interface func (export "renumber")
197+
(param $fd $fd)
198+
;;; The file descriptor to overwrite.
199+
(param $to $fd)
200+
(result $error $errno)
201+
)
202+
203+
;;; Move the offset of a file descriptor.
204+
;;; Note: This is similar to `lseek` in POSIX.
205+
(@interface func (export "seek")
206+
(param $fd $fd)
207+
;;; The number of bytes to move.
208+
(param $offset $filedelta)
209+
;;; The base from which the offset is relative.
210+
(param $whence $whence)
211+
(result $error $errno)
212+
;;; The new offset of the file descriptor, relative to the start of the file.
213+
(result $newoffset $filesize)
214+
)
215+
216+
;;; Synchronize the data and metadata of a file to disk.
217+
;;; Note: This is similar to `fsync` in POSIX.
218+
(@interface func (export "sync")
219+
(param $fd $fd)
220+
(result $error $errno)
221+
)
222+
223+
;;; Return the current offset of a file descriptor.
224+
;;; Note: This is similar to `lseek(fd, 0, SEEK_CUR)` in POSIX.
225+
(@interface func (export "tell")
226+
(param $fd $fd)
227+
(result $error $errno)
228+
;;; The current offset of the file descriptor, relative to the start of the file.
229+
(result $offset $filesize)
230+
)
231+
232+
;;; Write to a file descriptor.
233+
;;; Note: This is similar to `writev` in POSIX.
234+
(@interface func (export "write")
235+
(param $fd $fd)
236+
;;; List of scatter/gather vectors from which to retrieve data.
237+
(param $iovs $ciovec_array)
238+
(result $error $errno)
239+
;;; The number of bytes written.
240+
(result $nwritten $size)
241+
)
242+
)

0 commit comments

Comments
 (0)