Skip to content

Commit 689a9a4

Browse files
committed
Add 'WIT by Example' section
1 parent 571f250 commit 689a9a4

6 files changed

Lines changed: 518 additions & 0 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package wasi:clocks@0.2.6;
2+
/// WASI Monotonic Clock is a clock API intended to let users measure elapsed
3+
/// time.
4+
///
5+
/// It is intended to be portable at least between Unix-family platforms and
6+
/// Windows.
7+
///
8+
/// A monotonic clock is a clock which has an unspecified initial value, and
9+
/// successive reads of the clock will produce non-decreasing values.
10+
interface monotonic-clock {
11+
12+
/// An instant in time, in nanoseconds. An instant is relative to an
13+
/// unspecified initial value, and can only be compared to instances from
14+
/// the same monotonic-clock.
15+
type instant = u64;
16+
17+
/// A duration of time, in nanoseconds.
18+
type duration = u64;
19+
20+
/// Read the current value of the clock.
21+
///
22+
/// The clock is monotonic, therefore calling this function repeatedly will
23+
/// produce a sequence of non-decreasing values.
24+
now: func() -> instant;
25+
26+
/// Query the resolution of the clock. Returns the duration of time
27+
/// corresponding to a clock tick.
28+
resolution: func() -> duration;
29+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package wasi:clocks;
2+
/// WASI Wall Clock is a clock API intended to let users query the current
3+
/// time.
4+
interface wall-clock {
5+
/// A time and date in seconds plus nanoseconds.
6+
record datetime {
7+
seconds: u64,
8+
nanoseconds: u32,
9+
}
10+
11+
/// Read the current value of the clock.
12+
///
13+
/// This clock is not monotonic, therefore calling this function repeatedly
14+
/// will not necessarily produce a sequence of non-decreasing values.
15+
///
16+
/// The returned timestamps represent the number of seconds since
17+
/// 1970-01-01T00:00:00Z, also known as [POSIX's Seconds Since the Epoch],
18+
/// also known as [Unix Time].
19+
///
20+
/// The nanoseconds field of the output is always less than 1000000000.
21+
///
22+
/// [POSIX's Seconds Since the Epoch]: https://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_xbd_chap04.html#tag_21_04_16
23+
/// [Unix Time]: https://en.wikipedia.org/wiki/Unix_time
24+
now: func() -> datetime;
25+
26+
/// Query the resolution of the clock.
27+
///
28+
/// The nanoseconds field of the output is always less than 1000000000.
29+
resolution: func() -> datetime;
30+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package wasi:clocks;
2+
3+
world imports {
4+
import monotonic-clock;
5+
import wall-clock;
6+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package wasi:filesystem;
2+
interface types {
3+
use wasi:clocks/wall-clock.{datetime};
4+
5+
/// File size or length of a region within a file.
6+
type filesize = u64;
7+
8+
/// The type of a filesystem object referenced by a descriptor.
9+
enum descriptor-type {
10+
/// The descriptor refers to a directory inode.
11+
directory,
12+
/// The descriptor refers to a regular file inode.
13+
regular-file,
14+
}
15+
16+
/// File attributes.
17+
record descriptor-stat {
18+
/// File type.
19+
%type: descriptor-type,
20+
/// File size in bytes.
21+
size: filesize,
22+
/// Last data access timestamp (optional).
23+
data-access-timestamp: option<datetime>,
24+
}
25+
26+
/// Open flags used by `open-at`.
27+
flags open-flags {
28+
/// Create file if it does not exist, similar to `O_CREAT` in POSIX.
29+
create,
30+
/// Fail if not a directory, similar to `O_DIRECTORY` in POSIX.
31+
directory,
32+
}
33+
34+
/// When setting a timestamp, this gives the value to set it to.
35+
variant new-timestamp {
36+
/// Leave the timestamp set to its previous value.
37+
no-change,
38+
/// Set the timestamp to the current time of the system clock associated
39+
/// with the filesystem.
40+
now,
41+
/// Set the timestamp to the given value.
42+
timestamp(datetime),
43+
}
44+
45+
/// Error codes returned by functions, similar to `errno` in POSIX.
46+
enum error-code {
47+
/// Permission denied, similar to `EACCES` in POSIX.
48+
access,
49+
/// Resource unavailable, or operation would block, similar to `EAGAIN` and `EWOULDBLOCK` in POSIX.
50+
would-block,
51+
/// Connection already in progress, similar to `EALREADY` in POSIX.
52+
already,
53+
/// Bad descriptor, similar to `EBADF` in POSIX.
54+
bad-descriptor,
55+
/// Device or resource busy, similar to `EBUSY` in POSIX.
56+
busy,
57+
}
58+
59+
/// A descriptor is a reference to a filesystem object, which may be a
60+
/// file or directory.
61+
resource descriptor {
62+
/// Read from a descriptor, without using and updating the descriptor's offset.
63+
///
64+
/// This function returns a list of bytes containing the data that was
65+
/// read, along with a bool which, when true, indicates that the end of the
66+
/// file was reached.
67+
read: func(
68+
/// The maximum number of bytes to read.
69+
length: filesize,
70+
/// The offset within the file at which to read.
71+
offset: filesize,
72+
) -> result<tuple<list<u8>, bool>, error-code>;
73+
74+
/// Return the attributes of an open file or directory.
75+
stat: func() -> result<descriptor-stat, error-code>;
76+
77+
/// Adjust the timestamps of a file or directory.
78+
set-times-at: func(
79+
/// The relative path of the file or directory to operate on.
80+
path: string,
81+
/// The desired values of the data access timestamp.
82+
data-access-timestamp: new-timestamp,
83+
) -> result<_, error-code>;
84+
85+
/// Open a file or directory.
86+
open-at: func(
87+
/// The relative path of the object to open.
88+
path: string,
89+
/// The method by which to open the file.
90+
open-flags: open-flags,
91+
) -> result<descriptor, error-code>;
92+
93+
}
94+
}

component-model/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- [Interfaces](./design/interfaces.md)
1212
- [Worlds](./design/worlds.md)
1313
- [Packages](./design/packages.md)
14+
- [WIT By Example](./design/wit-example.md)
1415
- [WIT Reference](./design/wit.md)
1516

1617
# Using WebAssembly Components

0 commit comments

Comments
 (0)