ctx builder: fix warnings, test harness
This commit is contained in:
@@ -15,7 +15,7 @@ pub fn instantiate(data: &[u8], bin_name: &str, workspace: Option<&Path>) -> any
|
||||
// Additionally register any preopened directories if we have them.
|
||||
let mut builder = wasi_c2::WasiCtx::builder();
|
||||
|
||||
builder
|
||||
builder = builder
|
||||
.arg(bin_name)?
|
||||
.arg(".")?
|
||||
.stdin(Box::new(ReadPipe::from(Vec::new())))
|
||||
@@ -27,7 +27,7 @@ pub fn instantiate(data: &[u8], bin_name: &str, workspace: Option<&Path>) -> any
|
||||
let dirfd =
|
||||
File::open(workspace).context(format!("error while preopening {:?}", workspace))?;
|
||||
let preopen_dir = unsafe { cap_std::fs::Dir::from_std_file(dirfd) };
|
||||
builder.preopened_dir(Box::new(preopen_dir), ".")?;
|
||||
builder = builder.preopened_dir(Box::new(preopen_dir), ".")?;
|
||||
}
|
||||
|
||||
let snapshot1 = wasi_c2_wasmtime::Wasi::new(&store, builder.build()?);
|
||||
|
||||
@@ -71,7 +71,7 @@ impl WasiCtxBuilder {
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn stdin(mut self, f: Box<dyn WasiFile>) -> Self {
|
||||
pub fn stdin(self, f: Box<dyn WasiFile>) -> Self {
|
||||
self.0.insert_file(
|
||||
0,
|
||||
f,
|
||||
@@ -80,7 +80,7 @@ impl WasiCtxBuilder {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn stdout(mut self, f: Box<dyn WasiFile>) -> Self {
|
||||
pub fn stdout(self, f: Box<dyn WasiFile>) -> Self {
|
||||
self.0.insert_file(
|
||||
1,
|
||||
f,
|
||||
@@ -89,7 +89,7 @@ impl WasiCtxBuilder {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn stderr(mut self, f: Box<dyn WasiFile>) -> Self {
|
||||
pub fn stderr(self, f: Box<dyn WasiFile>) -> Self {
|
||||
self.0.insert_file(
|
||||
2,
|
||||
f,
|
||||
@@ -105,7 +105,7 @@ impl WasiCtxBuilder {
|
||||
}
|
||||
|
||||
pub fn preopened_dir(
|
||||
mut self,
|
||||
self,
|
||||
dir: Box<dyn WasiDir>,
|
||||
path: impl AsRef<Path>,
|
||||
) -> Result<Self, Error> {
|
||||
@@ -120,7 +120,7 @@ impl WasiCtxBuilder {
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn random(mut self, random: Box<dyn RngCore>) -> Self {
|
||||
pub fn random(self, random: Box<dyn RngCore>) -> Self {
|
||||
self.0.random.replace(random);
|
||||
self
|
||||
}
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
use crate::clocks::WasiSystemClock;
|
||||
use crate::file::WasiFile;
|
||||
use crate::Error;
|
||||
use cap_std::time::SystemTime;
|
||||
use cap_std::time::{Duration, SystemTime};
|
||||
use std::cell::Ref;
|
||||
pub mod subscription;
|
||||
|
||||
use subscription::{
|
||||
RwSubscription, Subscription, SubscriptionResult, SubscriptionSet, TimerSubscription,
|
||||
};
|
||||
use subscription::{RwSubscription, Subscription, SubscriptionResult, SystemTimerSubscription};
|
||||
|
||||
pub trait WasiSched {
|
||||
fn poll_oneoff<'a>(&self, poll: &mut Poll<'a>) -> Result<(), Error>;
|
||||
@@ -47,9 +45,21 @@ impl<'a> Poll<'a> {
|
||||
pub fn new() -> Self {
|
||||
Self { subs: Vec::new() }
|
||||
}
|
||||
pub fn subscribe_timer(&mut self, deadline: SystemTime, ud: Userdata) {
|
||||
self.subs
|
||||
.push((Subscription::Timer(TimerSubscription { deadline }), ud));
|
||||
pub fn subscribe_system_timer(
|
||||
&mut self,
|
||||
clock: &'a dyn WasiSystemClock,
|
||||
deadline: SystemTime,
|
||||
precision: Duration,
|
||||
ud: Userdata,
|
||||
) {
|
||||
self.subs.push((
|
||||
Subscription::SystemTimer(SystemTimerSubscription {
|
||||
clock,
|
||||
deadline,
|
||||
precision,
|
||||
}),
|
||||
ud,
|
||||
));
|
||||
}
|
||||
pub fn subscribe_read(&mut self, file: Ref<'a, dyn WasiFile>, ud: Userdata) {
|
||||
self.subs
|
||||
@@ -59,15 +69,13 @@ impl<'a> Poll<'a> {
|
||||
self.subs
|
||||
.push((Subscription::Read(RwSubscription::new(file)), ud));
|
||||
}
|
||||
pub fn results(self, clock: &dyn WasiSystemClock) -> Vec<(SubscriptionResult, Userdata)> {
|
||||
pub fn results(self) -> Vec<(SubscriptionResult, Userdata)> {
|
||||
self.subs
|
||||
.into_iter()
|
||||
.filter_map(|(s, ud)| SubscriptionResult::from_subscription(s, clock).map(|r| (r, ud)))
|
||||
.filter_map(|(s, ud)| SubscriptionResult::from_subscription(s).map(|r| (r, ud)))
|
||||
.collect()
|
||||
}
|
||||
pub(crate) fn subscriptions(&'a mut self) -> SubscriptionSet<'a> {
|
||||
SubscriptionSet {
|
||||
subs: self.subs.iter().map(|(s, _ud)| s).collect(),
|
||||
}
|
||||
pub fn subscriptions(&'a mut self) -> impl Iterator<Item = &Subscription<'a>> {
|
||||
self.subs.iter().map(|(s, _ud)| s)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,15 +34,17 @@ impl<'a> RwSubscription<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct TimerSubscription {
|
||||
pub struct SystemTimerSubscription<'a> {
|
||||
pub clock: &'a dyn WasiSystemClock,
|
||||
pub deadline: SystemTime,
|
||||
pub precision: Duration,
|
||||
}
|
||||
|
||||
impl TimerSubscription {
|
||||
pub fn result(&self, clock: &dyn WasiSystemClock) -> Option<Result<(), Error>> {
|
||||
impl<'a> SystemTimerSubscription<'a> {
|
||||
pub fn result(&self) -> Option<Result<(), Error>> {
|
||||
if self
|
||||
.deadline
|
||||
.duration_since(clock.now(Duration::from_secs(0)))
|
||||
.duration_since(self.clock.now(self.precision))
|
||||
.is_ok()
|
||||
{
|
||||
Some(Ok(()))
|
||||
@@ -55,46 +57,21 @@ impl TimerSubscription {
|
||||
pub enum Subscription<'a> {
|
||||
Read(RwSubscription<'a>),
|
||||
Write(RwSubscription<'a>),
|
||||
Timer(TimerSubscription),
|
||||
}
|
||||
|
||||
pub struct SubscriptionSet<'a> {
|
||||
pub subs: Vec<&'a Subscription<'a>>,
|
||||
}
|
||||
|
||||
impl<'a> SubscriptionSet<'a> {
|
||||
pub fn earliest_deadline(&self) -> Option<SystemTime> {
|
||||
self.subs
|
||||
.iter()
|
||||
.filter_map(|s| match s {
|
||||
Subscription::Timer(ts) => Some(ts.deadline),
|
||||
_ => None,
|
||||
})
|
||||
.fold(None, |early, ts| {
|
||||
if let Some(early) = early {
|
||||
Some(early.min(ts))
|
||||
} else {
|
||||
Some(ts)
|
||||
}
|
||||
})
|
||||
}
|
||||
SystemTimer(SystemTimerSubscription<'a>),
|
||||
}
|
||||
|
||||
pub enum SubscriptionResult {
|
||||
Read(Result<(u64, RwEventFlags), Error>),
|
||||
Write(Result<(u64, RwEventFlags), Error>),
|
||||
Timer(Result<(), Error>),
|
||||
SystemTimer(Result<(), Error>),
|
||||
}
|
||||
|
||||
impl SubscriptionResult {
|
||||
pub fn from_subscription(
|
||||
s: Subscription,
|
||||
clock: &dyn WasiSystemClock,
|
||||
) -> Option<SubscriptionResult> {
|
||||
pub fn from_subscription(s: Subscription) -> Option<SubscriptionResult> {
|
||||
match s {
|
||||
Subscription::Read(s) => s.result().map(SubscriptionResult::Read),
|
||||
Subscription::Write(s) => s.result().map(SubscriptionResult::Write),
|
||||
Subscription::Timer(s) => s.result(clock).map(SubscriptionResult::Timer),
|
||||
Subscription::SystemTimer(s) => s.result().map(SubscriptionResult::SystemTimer),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -909,7 +909,7 @@ impl<'a> wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
|
||||
|
||||
self.sched.poll_oneoff(&mut poll)?;
|
||||
|
||||
let results = poll.results(self.clocks.system.deref());
|
||||
let results = poll.results();
|
||||
let num_results = results.len();
|
||||
assert!(
|
||||
num_results <= nsubscriptions as usize,
|
||||
@@ -964,7 +964,7 @@ impl<'a> wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
|
||||
},
|
||||
}
|
||||
}
|
||||
SubscriptionResult::Timer(r) => {
|
||||
SubscriptionResult::SystemTimer(r) => {
|
||||
let type_ = types::Eventtype::Clock;
|
||||
types::Event {
|
||||
userdata,
|
||||
|
||||
Reference in New Issue
Block a user