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

Add pseudo-streams. #29

Merged
merged 22 commits into from
Dec 23, 2022
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
cc6ef6b
Add pseudo-streams.
sunfishcode Dec 15, 2022
5ff595e
Implement `WasiStream` for the pipe types.
sunfishcode Dec 16, 2022
f29f2b7
Implement file streaming.
sunfishcode Dec 16, 2022
06cb75a
Implement `skip` and `write_repeated` for more things.
sunfishcode Dec 16, 2022
79ea367
Implement `drop_stream`.
sunfishcode Dec 16, 2022
b85cc6f
Minor simplification.
sunfishcode Dec 17, 2022
abbe490
Prevent `FileStream` from being misused.
sunfishcode Dec 17, 2022
0b2355b
Implement the host side of `poll_oneoff`.
sunfishcode Dec 17, 2022
b4460e5
Remove `cfg(allow(...))` directives that are no longer needed.
sunfishcode Dec 17, 2022
226d362
Implement clock subscriptions.
sunfishcode Dec 21, 2022
1f1d523
Add more comments and minor code cleanups.
sunfishcode Dec 22, 2022
ffcc3ab
Add more comments and code cleanups.
sunfishcode Dec 22, 2022
9d4c691
Handle overflow gracefully in the polyfill clock APIs.
sunfishcode Dec 22, 2022
e7099d4
Fix an unused-import warning.
sunfishcode Dec 22, 2022
b1d6bfb
Delete redundant implementations.
sunfishcode Dec 22, 2022
6bd0dda
`poll_oneoff` in the polyfill doesn't need mutable `State`.
sunfishcode Dec 22, 2022
65daab2
Simplify the host poll implementation.
sunfishcode Dec 22, 2022
484077b
Add more documentation comments.
sunfishcode Dec 22, 2022
195847a
Remove the `OFlags::APPEND` flag, which is no longer used.
sunfishcode Dec 23, 2022
ddd9632
Add documentation for the default-clock functions.
sunfishcode Dec 23, 2022
a12af00
Use `..`.
sunfishcode Dec 23, 2022
7dcf631
Clean up debugging code.
sunfishcode Dec 23, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ edition = "2021"
anyhow = "1.0.22"
thiserror = "1.0.15"
tracing = "0.1.26"
cap-std = "1.0.0"
cap-rand = "1.0.0"
cap-std = "1.0.2"
cap-rand = "1.0.2"
cap-fs-ext = "1.0.2"
bitflags = "1.2"
windows-sys = "0.42.0"
rustix = "0.36.0"
Expand Down
2 changes: 2 additions & 0 deletions host/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ tracing = { workspace = true }
wasmtime = { git = "https://github.com/bytecodealliance/wasmtime", features = ["component-model"] }
wasi-common = { path = "../wasi-common" }
wasi-cap-std-sync = { path = "../wasi-common/cap-std-sync" }
is-terminal = "0.4.1"
terminal_size = "0.2.3"

[dev-dependencies]
test-programs-macros = { path = "../test-programs/macros" }
Expand Down
89 changes: 21 additions & 68 deletions host/src/clocks.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::{wasi_clocks, wasi_default_clocks, WasiCtx};
use anyhow::Context;
use cap_std::time::SystemTime;
use wasi_common::clocks::{MonotonicClock, MonotonicTimer, WallClock};
use wasi_common::clocks::{TableMonotonicClockExt, TableWallClockExt};

impl TryFrom<SystemTime> for wasi_clocks::Datetime {
type Error = anyhow::Error;
Expand All @@ -19,102 +18,56 @@ impl TryFrom<SystemTime> for wasi_clocks::Datetime {

#[async_trait::async_trait]
impl wasi_default_clocks::WasiDefaultClocks for WasiCtx {
async fn default_monotonic_clock(&mut self) -> anyhow::Result<wasi_clocks::MonotonicClock> {
Ok(self.clocks.default_monotonic)
async fn default_wall_clock(&mut self) -> anyhow::Result<wasi_clocks::WallClock> {
// Create a new handle to the default wall clock.
let new = self.clocks.default_wall_clock.dup();
Ok(self.table_mut().push(Box::new(new))?)
sunfishcode marked this conversation as resolved.
Show resolved Hide resolved
}

async fn default_wall_clock(&mut self) -> anyhow::Result<wasi_clocks::WallClock> {
Ok(self.clocks.default_wall)
async fn default_monotonic_clock(&mut self) -> anyhow::Result<wasi_clocks::MonotonicClock> {
// Create a new handle to the default monotonic clock.
let new = self.clocks.default_monotonic_clock.dup();
Ok(self.table_mut().push(Box::new(new))?)
}
}

#[async_trait::async_trait]
impl wasi_clocks::WasiClocks for WasiCtx {
async fn subscribe_wall_clock(
&mut self,
when: wasi_clocks::Datetime,
absolute: bool,
) -> anyhow::Result<wasi_clocks::WasiFuture> {
drop((when, absolute));
todo!()
}

async fn subscribe_monotonic_clock(
&mut self,
when: wasi_clocks::Instant,
absolute: bool,
) -> anyhow::Result<wasi_clocks::WasiFuture> {
drop((when, absolute));
todo!()
}

async fn monotonic_clock_now(
&mut self,
fd: wasi_clocks::MonotonicClock,
) -> anyhow::Result<wasi_clocks::Instant> {
let clock = self.table.get::<MonotonicClock>(fd)?;
let now = clock.now(self.clocks.monotonic.as_ref());
Ok(now
.as_nanos()
.try_into()
.context("converting monotonic time to nanos u64")?)
Ok(self.table().get_monotonic_clock(fd)?.now())
}

async fn monotonic_clock_resolution(
&mut self,
fd: wasi_clocks::MonotonicClock,
) -> anyhow::Result<wasi_clocks::Instant> {
self.table.get::<MonotonicClock>(fd)?;
let res = self.clocks.monotonic.resolution();
Ok(res
.as_nanos()
.try_into()
.context("converting monotonic resolution to nanos u64")?)
}

async fn monotonic_clock_new_timer(
&mut self,
fd: wasi_clocks::MonotonicClock,
initial: wasi_clocks::Instant,
) -> anyhow::Result<wasi_clocks::MonotonicTimer> {
let clock = self.table.get::<MonotonicClock>(fd)?;
let timer = clock.new_timer(std::time::Duration::from_micros(initial));
drop(clock);
let timer_fd = self.table.push(Box::new(timer))?;
Ok(timer_fd)
Ok(self.table().get_monotonic_clock(fd)?.now())
}

async fn wall_clock_now(
&mut self,
fd: wasi_clocks::WallClock,
) -> anyhow::Result<wasi_clocks::Datetime> {
let clock = self.table.get::<WallClock>(fd)?;
Ok(clock.now(self.clocks.system.as_ref()).try_into()?)
let clock = self.table().get_wall_clock(fd)?;
let now = clock.now();
Ok(wasi_clocks::Datetime {
seconds: now.as_secs(),
nanoseconds: now.subsec_nanos(),
})
}

async fn wall_clock_resolution(
&mut self,
fd: wasi_clocks::WallClock,
) -> anyhow::Result<wasi_clocks::Datetime> {
self.table.get::<WallClock>(fd)?;
let nanos = self.clocks.system.resolution().as_nanos();
let clock = self.table().get_wall_clock(fd)?;
let res = clock.resolution();
Ok(wasi_clocks::Datetime {
seconds: (nanos / 1_000_000_000_u128)
.try_into()
.context("converting wall clock resolution to seconds u64")?,
nanoseconds: (nanos % 1_000_000_000_u128).try_into().unwrap(),
seconds: res.as_secs(),
nanoseconds: res.subsec_nanos(),
})
}

async fn monotonic_timer_current(
&mut self,
fd: wasi_clocks::MonotonicTimer,
) -> anyhow::Result<wasi_clocks::Instant> {
let timer = self.table.get::<MonotonicTimer>(fd)?;
Ok(timer
.current(self.clocks.monotonic.as_ref())
.as_nanos()
.try_into()
.context("converting monotonic timer to nanos u64")?)
}
}
Loading