Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
14 changes: 14 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ members = [
"crates/misc/run-examples",
"crates/misc/rust",
"crates/wiggle",
"crates/wiggle/generate",
"crates/wiggle/macro",
"crates/wiggle/wasmtime",
"crates/wasi-common",
"crates/wasi-common/cap-std-sync",
Expand Down
1 change: 0 additions & 1 deletion crates/wasi-common/src/snapshots/preview_0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use wiggle::GuestPtr;

wiggle::from_witx!({
witx: ["$WASI_ROOT/phases/old/snapshot_0/witx/wasi_unstable.witx"],
ctx: WasiCtx,
errors: { errno => Error },
});

Expand Down
1 change: 0 additions & 1 deletion crates/wasi-common/src/snapshots/preview_1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use wiggle::GuestPtr;

wiggle::from_witx!({
witx: ["$WASI_ROOT/phases/snapshot/witx/wasi_snapshot_preview1.witx"],
ctx: WasiCtx,
errors: { errno => Error },
});

Expand Down
1 change: 0 additions & 1 deletion crates/wasi-crypto/src/wiggle_interfaces/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ pub use wasi_crypto::CryptoCtx as WasiCryptoCtx;

wiggle::from_witx!({
witx: ["$CARGO_MANIFEST_DIR/spec/witx/wasi_ephemeral_crypto.witx"],
ctx: WasiCryptoCtx
});

pub mod wasi_modules {
Expand Down
1 change: 0 additions & 1 deletion crates/wasi-nn/src/witx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::ctx::WasiNnError;
// Generate the traits and types of wasi-nn in several Rust modules (e.g. `types`).
wiggle::from_witx!({
witx: ["$WASI_ROOT/phases/ephemeral/witx/wasi_ephemeral_nn.witx"],
ctx: WasiNnCtx,
errors: { nn_errno => WasiNnError }
});

Expand Down
10 changes: 10 additions & 0 deletions crates/wasmtime/src/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,16 @@ impl Linker {
Instance::new(&self.store, module, &imports)
}

/// Attempts to instantiate the `module` provided. This is the same as [`Linker::instantiate`],
/// except for async `Store`s.
#[cfg(feature = "async")]
#[cfg_attr(nightlydoc, doc(cfg(feature = "async")))]
pub async fn instantiate_async(&self, module: &Module) -> Result<Instance> {
let imports = self.compute_imports(module)?;

Instance::new_async(&self.store, module, &imports).await
}

fn compute_imports(&self, module: &Module) -> Result<Vec<Extern>> {
module
.imports()
Expand Down
1 change: 1 addition & 0 deletions crates/wiggle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ witx = { path = "../wasi-common/WASI/tools/witx", version = "0.9", optional = tr
wiggle-macro = { path = "macro", version = "0.23.0" }
tracing = "0.1.15"
bitflags = "1.2"
async-trait = "0.1.42"

[badges]
maintenance = { status = "actively-developed" }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
use crate::config::ErrorConf;
use crate::config::{AsyncConf, ErrorConf};
use anyhow::{anyhow, Error};
use proc_macro2::TokenStream;
use quote::quote;
use std::collections::HashMap;
use std::rc::Rc;
use witx::{Document, Id, NamedType, TypeRef};
use witx::{Document, Id, InterfaceFunc, Module, NamedType, TypeRef};

pub struct CodegenSettings {
pub errors: ErrorTransform,
async_: AsyncConf,
}
impl CodegenSettings {
pub fn new(error_conf: &ErrorConf, async_: &AsyncConf, doc: &Document) -> Result<Self, Error> {
let errors = ErrorTransform::new(error_conf, doc)?;
Ok(Self {
errors,
async_: async_.clone(),
})
}
pub fn is_async(&self, module: &Module, func: &InterfaceFunc) -> bool {
self.async_
.is_async(module.name.as_str(), func.name.as_str())
}
}

pub struct ErrorTransform {
m: Vec<UserErrorType>,
Expand Down
118 changes: 88 additions & 30 deletions crates/wiggle/generate/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,20 @@ use {
#[derive(Debug, Clone)]
pub struct Config {
pub witx: WitxConf,
pub ctx: CtxConf,
pub errors: ErrorConf,
pub async_: AsyncConf,
}

#[derive(Debug, Clone)]
pub enum ConfigField {
Witx(WitxConf),
Ctx(CtxConf),
Error(ErrorConf),
Async(AsyncConf),
}

mod kw {
syn::custom_keyword!(witx);
syn::custom_keyword!(witx_literal);
syn::custom_keyword!(ctx);
syn::custom_keyword!(errors);
}

Expand All @@ -41,14 +40,14 @@ impl Parse for ConfigField {
input.parse::<kw::witx_literal>()?;
input.parse::<Token![:]>()?;
Ok(ConfigField::Witx(WitxConf::Literal(input.parse()?)))
} else if lookahead.peek(kw::ctx) {
input.parse::<kw::ctx>()?;
input.parse::<Token![:]>()?;
Ok(ConfigField::Ctx(input.parse()?))
} else if lookahead.peek(kw::errors) {
input.parse::<kw::errors>()?;
input.parse::<Token![:]>()?;
Ok(ConfigField::Error(input.parse()?))
} else if lookahead.peek(Token![async]) {
input.parse::<Token![async]>()?;
input.parse::<Token![:]>()?;
Ok(ConfigField::Async(input.parse()?))
} else {
Err(lookahead.error())
}
Expand All @@ -58,8 +57,8 @@ impl Parse for ConfigField {
impl Config {
pub fn build(fields: impl Iterator<Item = ConfigField>, err_loc: Span) -> Result<Self> {
let mut witx = None;
let mut ctx = None;
let mut errors = None;
let mut async_ = None;
for f in fields {
match f {
ConfigField::Witx(c) => {
Expand All @@ -68,28 +67,26 @@ impl Config {
}
witx = Some(c);
}
ConfigField::Ctx(c) => {
if ctx.is_some() {
return Err(Error::new(err_loc, "duplicate `ctx` field"));
}
ctx = Some(c);
}
ConfigField::Error(c) => {
if errors.is_some() {
return Err(Error::new(err_loc, "duplicate `errors` field"));
}
errors = Some(c);
}
ConfigField::Async(c) => {
if async_.is_some() {
return Err(Error::new(err_loc, "duplicate `async` field"));
}
async_ = Some(c);
}
}
}
Ok(Config {
witx: witx
.take()
.ok_or_else(|| Error::new(err_loc, "`witx` field required"))?,
ctx: ctx
.take()
.ok_or_else(|| Error::new(err_loc, "`ctx` field required"))?,
errors: errors.take().unwrap_or_default(),
async_: async_.take().unwrap_or_default(),
})
}

Expand Down Expand Up @@ -216,19 +213,6 @@ impl Parse for Literal {
}
}

#[derive(Debug, Clone)]
pub struct CtxConf {
pub name: Ident,
}

impl Parse for CtxConf {
fn parse(input: ParseStream) -> Result<Self> {
Ok(CtxConf {
name: input.parse()?,
})
}
}

#[derive(Clone, Default, Debug)]
/// Map from abi error type to rich error type
pub struct ErrorConf(HashMap<Ident, ErrorConfField>);
Expand Down Expand Up @@ -294,3 +278,77 @@ impl Parse for ErrorConfField {
})
}
}

#[derive(Clone, Default, Debug)]
/// Modules and funcs that should be async
pub struct AsyncConf(HashMap<String, Vec<String>>);

impl AsyncConf {
pub fn is_async(&self, module: &str, function: &str) -> bool {
self.0
.get(module)
.and_then(|fs| fs.iter().find(|f| *f == function))
.is_some()
}
}

impl Parse for AsyncConf {
fn parse(input: ParseStream) -> Result<Self> {
let content;
let _ = braced!(content in input);
let items: Punctuated<AsyncConfField, Token![,]> =
content.parse_terminated(Parse::parse)?;
let mut m: HashMap<String, Vec<String>> = HashMap::new();
use std::collections::hash_map::Entry;
for i in items {
let function_names = i
.function_names
.iter()
.map(|i| i.to_string())
.collect::<Vec<String>>();
match m.entry(i.module_name.to_string()) {
Entry::Occupied(o) => o.into_mut().extend(function_names),
Entry::Vacant(v) => {
v.insert(function_names);
}
}
}
Ok(AsyncConf(m))
}
}

#[derive(Clone)]
pub struct AsyncConfField {
pub module_name: Ident,
pub function_names: Vec<Ident>,
pub err_loc: Span,
}

impl Parse for AsyncConfField {
fn parse(input: ParseStream) -> Result<Self> {
let err_loc = input.span();
let module_name = input.parse::<Ident>()?;
let _doublecolon: Token![::] = input.parse()?;
let lookahead = input.lookahead1();
if lookahead.peek(syn::token::Brace) {
let content;
let _ = braced!(content in input);
let function_names: Punctuated<Ident, Token![,]> =
content.parse_terminated(Parse::parse)?;
Ok(AsyncConfField {
module_name,
function_names: function_names.iter().cloned().collect(),
err_loc,
})
} else if lookahead.peek(Ident) {
let name = input.parse()?;
Ok(AsyncConfField {
module_name,
function_names: vec![name],
err_loc,
})
} else {
Err(lookahead.error())
}
}
}
Loading