Merge pull request #2701 from bytecodealliance/pch/wiggle_async
wiggle: support for Rust async
This commit is contained in:
@@ -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>,
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
@@ -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) => {
|
||||
@@ -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(),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -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>);
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::error_transform::ErrorTransform;
|
||||
use crate::codegen_settings::CodegenSettings;
|
||||
use crate::lifetimes::anon_lifetime;
|
||||
use crate::module_trait::passed_by_reference;
|
||||
use crate::names::Names;
|
||||
@@ -12,11 +12,10 @@ pub fn define_func(
|
||||
names: &Names,
|
||||
module: &witx::Module,
|
||||
func: &witx::InterfaceFunc,
|
||||
errxform: &ErrorTransform,
|
||||
settings: &CodegenSettings,
|
||||
) -> TokenStream {
|
||||
let rt = names.runtime_mod();
|
||||
let ident = names.func(&func.name);
|
||||
let ctx_type = names.ctx_type();
|
||||
|
||||
let (wasm_params, wasm_results) = func.wasm_signature();
|
||||
let param_names = (0..wasm_params.len())
|
||||
@@ -37,6 +36,7 @@ pub fn define_func(
|
||||
};
|
||||
|
||||
let mut body = TokenStream::new();
|
||||
let mut required_impls = vec![names.trait_name(&module.name)];
|
||||
func.call_interface(
|
||||
&module.name,
|
||||
&mut Rust {
|
||||
@@ -48,16 +48,22 @@ pub fn define_func(
|
||||
names,
|
||||
module,
|
||||
funcname: func.name.as_str(),
|
||||
errxform,
|
||||
settings,
|
||||
required_impls: &mut required_impls,
|
||||
},
|
||||
);
|
||||
|
||||
let asyncness = if settings.is_async(&module, &func) {
|
||||
quote!(async)
|
||||
} else {
|
||||
quote!()
|
||||
};
|
||||
let mod_name = &module.name.as_str();
|
||||
let func_name = &func.name.as_str();
|
||||
quote! {
|
||||
#[allow(unreachable_code)] // deals with warnings in noreturn functions
|
||||
pub fn #ident(
|
||||
ctx: &#ctx_type,
|
||||
pub #asyncness fn #ident(
|
||||
ctx: &(impl #(#required_impls)+*),
|
||||
memory: &dyn #rt::GuestMemory,
|
||||
#(#abi_params),*
|
||||
) -> Result<#abi_ret, #rt::Trap> {
|
||||
@@ -85,7 +91,16 @@ struct Rust<'a> {
|
||||
names: &'a Names,
|
||||
module: &'a witx::Module,
|
||||
funcname: &'a str,
|
||||
errxform: &'a ErrorTransform,
|
||||
settings: &'a CodegenSettings,
|
||||
required_impls: &'a mut Vec<Ident>,
|
||||
}
|
||||
|
||||
impl Rust<'_> {
|
||||
fn required_impl(&mut self, i: Ident) {
|
||||
if !self.required_impls.contains(&i) {
|
||||
self.required_impls.push(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl witx::Bindgen for Rust<'_> {
|
||||
@@ -205,8 +220,16 @@ impl witx::Bindgen for Rust<'_> {
|
||||
|
||||
let trait_name = self.names.trait_name(&self.module.name);
|
||||
let ident = self.names.func(&func.name);
|
||||
if self.settings.is_async(&self.module, &func) {
|
||||
self.src.extend(quote! {
|
||||
let ret = #trait_name::#ident(ctx, #(#args),*).await;
|
||||
})
|
||||
} else {
|
||||
self.src.extend(quote! {
|
||||
let ret = #trait_name::#ident(ctx, #(#args),*);
|
||||
})
|
||||
};
|
||||
self.src.extend(quote! {
|
||||
let ret = #trait_name::#ident(ctx, #(#args),*);
|
||||
#rt::tracing::event!(
|
||||
#rt::tracing::Level::TRACE,
|
||||
result = #rt::tracing::field::debug(&ret),
|
||||
@@ -226,9 +249,10 @@ impl witx::Bindgen for Rust<'_> {
|
||||
// enum, and *then* we lower to an i32.
|
||||
Instruction::EnumLower { ty } => {
|
||||
let val = operands.pop().unwrap();
|
||||
let val = match self.errxform.for_name(ty) {
|
||||
let val = match self.settings.errors.for_name(ty) {
|
||||
Some(custom) => {
|
||||
let method = self.names.user_error_conversion_method(&custom);
|
||||
self.required_impl(quote::format_ident!("UserErrorConversion"));
|
||||
quote!(UserErrorConversion::#method(ctx, #val)?)
|
||||
}
|
||||
None => val,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
mod codegen_settings;
|
||||
pub mod config;
|
||||
mod error_transform;
|
||||
mod funcs;
|
||||
mod lifetimes;
|
||||
mod module_trait;
|
||||
@@ -11,14 +11,14 @@ use lifetimes::anon_lifetime;
|
||||
use proc_macro2::{Literal, TokenStream};
|
||||
use quote::quote;
|
||||
|
||||
pub use codegen_settings::{CodegenSettings, UserErrorType};
|
||||
pub use config::Config;
|
||||
pub use error_transform::{ErrorTransform, UserErrorType};
|
||||
pub use funcs::define_func;
|
||||
pub use module_trait::define_module_trait;
|
||||
pub use names::Names;
|
||||
pub use types::define_datatype;
|
||||
|
||||
pub fn generate(doc: &witx::Document, names: &Names, errs: &ErrorTransform) -> TokenStream {
|
||||
pub fn generate(doc: &witx::Document, names: &Names, settings: &CodegenSettings) -> TokenStream {
|
||||
// TODO at some point config should grow more ability to configure name
|
||||
// overrides.
|
||||
let rt = names.runtime_mod();
|
||||
@@ -49,7 +49,7 @@ pub fn generate(doc: &witx::Document, names: &Names, errs: &ErrorTransform) -> T
|
||||
}
|
||||
};
|
||||
|
||||
let user_error_methods = errs.iter().map(|errtype| {
|
||||
let user_error_methods = settings.errors.iter().map(|errtype| {
|
||||
let abi_typename = names.type_ref(&errtype.abi_type(), anon_lifetime());
|
||||
let user_typename = errtype.typename();
|
||||
let methodname = names.user_error_conversion_method(&errtype);
|
||||
@@ -64,12 +64,10 @@ pub fn generate(doc: &witx::Document, names: &Names, errs: &ErrorTransform) -> T
|
||||
let modname = names.module(&module.name);
|
||||
let fs = module
|
||||
.funcs()
|
||||
.map(|f| define_func(&names, &module, &f, &errs));
|
||||
let modtrait = define_module_trait(&names, &module, &errs);
|
||||
let ctx_type = names.ctx_type();
|
||||
.map(|f| define_func(&names, &module, &f, &settings));
|
||||
let modtrait = define_module_trait(&names, &module, &settings);
|
||||
quote!(
|
||||
pub mod #modname {
|
||||
use super::#ctx_type;
|
||||
use super::types::*;
|
||||
#(#fs)*
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use proc_macro2::TokenStream;
|
||||
use quote::quote;
|
||||
|
||||
use crate::error_transform::ErrorTransform;
|
||||
use crate::codegen_settings::CodegenSettings;
|
||||
use crate::lifetimes::{anon_lifetime, LifetimeExt};
|
||||
use crate::names::Names;
|
||||
use witx::Module;
|
||||
@@ -15,8 +15,9 @@ pub fn passed_by_reference(ty: &witx::Type) -> bool {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn define_module_trait(names: &Names, m: &Module, errxform: &ErrorTransform) -> TokenStream {
|
||||
pub fn define_module_trait(names: &Names, m: &Module, settings: &CodegenSettings) -> TokenStream {
|
||||
let traitname = names.trait_name(&m.name);
|
||||
let rt = names.runtime_mod();
|
||||
let traitmethods = m.funcs().map(|f| {
|
||||
// Check if we're returning an entity anotated with a lifetime,
|
||||
// in which case, we'll need to annotate the function itself, and
|
||||
@@ -43,7 +44,6 @@ pub fn define_module_trait(names: &Names, m: &Module, errxform: &ErrorTransform)
|
||||
quote!(#arg_name: #arg_type)
|
||||
});
|
||||
|
||||
let rt = names.runtime_mod();
|
||||
let result = match f.results.len() {
|
||||
0 if f.noreturn => quote!(#rt::Trap),
|
||||
0 => quote!(()),
|
||||
@@ -61,7 +61,7 @@ pub fn define_module_trait(names: &Names, m: &Module, errxform: &ErrorTransform)
|
||||
None => quote!(()),
|
||||
};
|
||||
let err = match err {
|
||||
Some(ty) => match errxform.for_abi_error(ty) {
|
||||
Some(ty) => match settings.errors.for_abi_error(ty) {
|
||||
Some(custom) => {
|
||||
let tn = custom.typename();
|
||||
quote!(super::#tn)
|
||||
@@ -75,13 +75,22 @@ pub fn define_module_trait(names: &Names, m: &Module, errxform: &ErrorTransform)
|
||||
_ => unimplemented!(),
|
||||
};
|
||||
|
||||
if is_anonymous {
|
||||
quote!(fn #funcname(&self, #(#args),*) -> #result; )
|
||||
let asyncness = if settings.is_async(&m, &f) {
|
||||
quote!(async)
|
||||
} else {
|
||||
quote!(fn #funcname<#lifetime>(&self, #(#args),*) -> #result;)
|
||||
quote!()
|
||||
};
|
||||
|
||||
if is_anonymous {
|
||||
quote!(#asyncness fn #funcname(&self, #(#args),*) -> #result; )
|
||||
} else {
|
||||
quote!(#asyncness fn #funcname<#lifetime>(&self, #(#args),*) -> #result;)
|
||||
}
|
||||
});
|
||||
|
||||
quote! {
|
||||
use #rt::async_trait;
|
||||
#[async_trait(?Send)]
|
||||
pub trait #traitname {
|
||||
#(#traitmethods)*
|
||||
}
|
||||
|
||||
@@ -7,20 +7,12 @@ use witx::{BuiltinType, Id, Type, TypeRef, WasmType};
|
||||
use crate::{lifetimes::LifetimeExt, UserErrorType};
|
||||
|
||||
pub struct Names {
|
||||
ctx_type: Ident,
|
||||
runtime_mod: TokenStream,
|
||||
}
|
||||
|
||||
impl Names {
|
||||
pub fn new(ctx_type: &Ident, runtime_mod: TokenStream) -> Names {
|
||||
Names {
|
||||
ctx_type: ctx_type.clone(),
|
||||
runtime_mod,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn ctx_type(&self) -> Ident {
|
||||
self.ctx_type.clone()
|
||||
pub fn new(runtime_mod: TokenStream) -> Names {
|
||||
Names { runtime_mod }
|
||||
}
|
||||
|
||||
pub fn runtime_mod(&self) -> TokenStream {
|
||||
|
||||
Reference in New Issue
Block a user