Skip to content
This repository was archived by the owner on Oct 13, 2023. It is now read-only.

Commit fca305e

Browse files
committed
Split apart wasi.wit into multiple *.wit files
This commit refactors the monolithic `wasi.wit` file into multiple files. I've taken some liberties in naming here so suggestions are definitely welcome! I've resolved some TODO annotations as well about `use`-ing types between interfaces. There are two issues remaining, however: * The `wasi-command` world still hardcodes `u32` because `world` items don't support `use` just yet. That isn't a hard limitation of WIT, however, it's just a temporary limitation of the implementation. * The `wasi-clocks` interface defines the `wasi-future` type. This is due to a cyclic dependency where `wasi-future` is defined within `wasi-poll` which depends on `wasi-clocks` for types. I've left this to a future refactoring to probably have a `types` interface of some form somewhere.
1 parent 248cfe9 commit fca305e

13 files changed

Lines changed: 348 additions & 379 deletions

Cargo.lock

Lines changed: 2 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,6 @@ overflow-checks = false
6060

6161
[features]
6262
command = []
63+
64+
[patch.crates-io]
65+
wit-parser = { git = 'https://github.com/alexcrichton/wasm-tools', branch = 'fix-wit-parser' }

host/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type HostResult<T, E> = anyhow::Result<Result<T, E>>;
1212

1313
wasmtime::component::bindgen!({
1414
path: "../wit",
15-
world: "wasi",
15+
world: "wasi-command",
1616
tracing: true,
1717
async: true,
1818
});

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ mod macros;
2020

2121
mod bindings {
2222
wit_bindgen_guest_rust::generate!({
23-
world: "wasi",
23+
world: "wasi-command",
2424
no_std,
2525
raw_strings,
2626
unchecked,

wit/wasi-clocks.wit

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/// # WASI Clocks API
2+
///
3+
/// WASI Clocks is a clock API intended to let users query the current time and
4+
/// to measure elapsed time.
5+
///
6+
/// It is intended to be portable at least between Unix-family platforms and
7+
/// Windows.
8+
default interface wasi-clocks {
9+
/// A monotonic clock is a clock which has an unspecified initial value, and
10+
/// successive reads of the clock will produce non-decreasing values.
11+
///
12+
/// It is intended for measuring elapsed time.
13+
type monotonic-clock = u32
14+
15+
/// A wall clock is a clock which measures the date and time according to some
16+
/// external reference.
17+
///
18+
/// External references may be reset, so this clock is not necessarily
19+
/// monotonic, making it unsuitable for measuring elapsed time.
20+
///
21+
/// It is intended for reporting the current date and time for humans.
22+
type wall-clock = u32
23+
24+
/// A timestamp in nanoseconds.
25+
type instant = u64
26+
27+
/// A time and date in seconds plus nanoseconds.
28+
record datetime {
29+
seconds: u64,
30+
nanoseconds: u32,
31+
}
32+
33+
/// An asynchronous operation. See the comments on `wasi-future` in the
34+
/// `wasi-poll` interface for details.
35+
/// TODO: `use` the `wasi-poll` version
36+
type wasi-future = u32
37+
38+
/// Read the current value of the clock.
39+
///
40+
/// As this the clock is monotonic, calling this function repeatedly will produce
41+
/// a sequence of non-decreasing values.
42+
monotonic-clock-now: func(clock: monotonic-clock) -> instant
43+
44+
/// Query the resolution of the clock.
45+
monotonic-clock-resolution: func(clock: monotonic-clock) -> instant
46+
47+
/// Read the current value of the clock.
48+
///
49+
/// As this the clock is not monotonic, calling this function repeatedly will
50+
/// not necessarily produce a sequence of non-decreasing values.
51+
///
52+
/// The returned timestamps represent the number of seconds since
53+
/// 1970-01-01T00:00:00Z, also known as [POSIX's Seconds Since the Epoch], also
54+
/// known as [Unix Time].
55+
///
56+
/// The nanoseconds field of the output is always less than 1000000000.
57+
///
58+
/// [POSIX's Seconds Since the Epoch]: https://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_xbd_chap04.html#tag_21_04_16
59+
/// [Unix Time]: https://en.wikipedia.org/wiki/Unix_time
60+
wall-clock-now: func(clock: wall-clock) -> datetime
61+
62+
/// Query the resolution of the clock.
63+
///
64+
/// The nanoseconds field of the output is always less than 1000000000.
65+
wall-clock-resolution: func(clock: wall-clock) -> datetime
66+
67+
/// Closes a monotonic clock handle.
68+
close-monotonic-clock: func(clock: monotonic-clock)
69+
70+
/// Closes a wall clock handle.
71+
close-wall-clock: func(clock: wall-clock)
72+
}
73+
74+
/// # WASI Default Clocks API
75+
///
76+
/// WASI Default Clocks provides value-exports of clock handles for monotonic
77+
/// and a wall-clock time, suitable for general-purpose application needs.
78+
interface wasi-default-clocks {
79+
use self.wasi-clocks.{monotonic-clock, wall-clock}
80+
81+
/// Return a default monotonic clock, suitable for general-purpose application
82+
/// needs.
83+
///
84+
/// This allocates a new handle, so applications with frequent need of a clock
85+
/// handle should call this function once and reuse the handle instead of
86+
/// calling this function each time.
87+
default-monotonic-clock: func() -> monotonic-clock
88+
89+
/// Return a default wall clock, suitable for general-purpose application
90+
/// needs.
91+
///
92+
/// This allocates a new handle, so applications with frequent need of a clock
93+
/// handle should call this function once and reuse the handle instead of
94+
default-wall-clock: func() -> wall-clock
95+
}

wit/wasi-command.wit

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
default world wasi-command {
2+
import wasi-clocks: pkg.wasi-clocks
3+
import wasi-default-clocks: pkg.wasi-clocks.wasi-default-clocks
4+
import wasi-logging: pkg.wasi-logging
5+
import wasi-stderr: pkg.wasi-stderr
6+
import wasi-filesystem: pkg.wasi-filesystem
7+
import wasi-random: pkg.wasi-random
8+
import wasi-poll: pkg.wasi-poll
9+
import wasi-tcp: pkg.wasi-tcp
10+
import wasi-exit: pkg.wasi-exit
11+
12+
export command: func(
13+
stdin: u32, // TODO `use` from `wasi-poll`
14+
stdout: u32, // TODO: `use` from `wasi-poll`
15+
args: list<string>,
16+
env-vars: list<tuple<string, string>>,
17+
preopens: list<tuple<u32, string>> // TODO: `use` from `wasi-filesystem`
18+
) -> result
19+
}

wit/wasi-exit.wit

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
default interface wasi-exit {
2+
/// Exit the curerent instance and any linked instances.
3+
exit: func(status: result)
4+
}

0 commit comments

Comments
 (0)