wasmtime-wasi: two distinct definitions of the Wasi struct, when sync vs async

This commit is contained in:
Pat Hickey
2021-04-13 12:38:49 -07:00
parent 201da20c63
commit 2b7a93c403

View File

@@ -7,10 +7,7 @@
//! Individual snapshots are available through //! Individual snapshots are available through
//! `wasmtime_wasi::snapshots::preview_{0, 1}::Wasi::new(&Store, Rc<RefCell<WasiCtx>>)`. //! `wasmtime_wasi::snapshots::preview_{0, 1}::Wasi::new(&Store, Rc<RefCell<WasiCtx>>)`.
use std::cell::RefCell;
use std::rc::Rc;
pub use wasi_common::{Error, WasiCtx, WasiCtxBuilder, WasiDir, WasiFile}; pub use wasi_common::{Error, WasiCtx, WasiCtxBuilder, WasiDir, WasiFile};
use wasmtime::{Config, Linker, Store};
/// Re-export the commonly used wasi-cap-std-sync crate here. This saves /// Re-export the commonly used wasi-cap-std-sync crate here. This saves
/// consumers of this library from having to keep additional dependencies /// consumers of this library from having to keep additional dependencies
@@ -18,6 +15,7 @@ use wasmtime::{Config, Linker, Store};
#[cfg(feature = "sync")] #[cfg(feature = "sync")]
pub mod sync { pub mod sync {
pub use wasi_cap_std_sync::*; pub use wasi_cap_std_sync::*;
super::define_wasi!(block_on);
} }
/// Re-export the wasi-cap-std-async crate here. This saves consumers of this library from having /// Re-export the wasi-cap-std-async crate here. This saves consumers of this library from having
@@ -25,8 +23,23 @@ pub mod sync {
#[cfg(feature = "async")] #[cfg(feature = "async")]
pub mod async_ { pub mod async_ {
pub use wasi_cap_std_async::*; pub use wasi_cap_std_async::*;
super::define_wasi!(async);
} }
// The only difference between these definitions for sync vs async is whether
// the wasmtime::Funcs generated are async (& therefore need an async Store and an executor to run)
// or whether they have an internal "dummy executor" that expects the implementation of all
// the async funcs to poll to Ready immediately.
#[doc(hidden)]
#[macro_export]
macro_rules! define_wasi {
($async_mode: tt) => {
use std::cell::RefCell;
use std::rc::Rc;
use wasmtime::{Config, Linker, Store};
use wasi_common::WasiCtx;
/// An instantiated instance of all available wasi exports. Presently includes /// An instantiated instance of all available wasi exports. Presently includes
/// both the "preview1" snapshot and the "unstable" (preview0) snapshot. /// both the "preview1" snapshot and the "unstable" (preview0) snapshot.
pub struct Wasi { pub struct Wasi {
@@ -86,7 +99,7 @@ necessary. Additionally [`Wasi::get_export`] can be used to do name-based
resolution.", resolution.",
}, },
}, },
async: { wasi_snapshot_preview1::{ poll_oneoff, sched_yield } } $async_mode: { wasi_snapshot_preview1::{ poll_oneoff, sched_yield } }
}); });
} }
pub mod preview_0 { pub mod preview_0 {
@@ -114,7 +127,9 @@ necessary. Additionally [`Wasi::get_export`] can be used to do name-based
resolution.", resolution.",
}, },
}, },
async: { wasi_unstable::{ poll_oneoff, sched_yield } } $async_mode: { wasi_unstable::{ poll_oneoff, sched_yield } }
}); });
} }
} }
}
}