wasmtime-wiggle-macro: re-use Names functionality

rather than try to duplicate it in utils
This commit is contained in:
Pat Hickey
2020-06-22 18:47:21 -07:00
parent 185701df1b
commit 1050c6d99c
4 changed files with 25 additions and 16 deletions

View File

@@ -8,6 +8,7 @@ pub use wasi_common::{WasiCtx, WasiCtxBuilder};
// with all the various WASI exports.
wasmtime_wiggle::define_struct_for_wiggle!({
witx: ["../wasi-common/WASI/phases/snapshot/witx/wasi_snapshot_preview1.witx"],
ctx: WasiCtx,
});
pub fn is_wasi_module(name: &str) -> bool {

View File

@@ -6,22 +6,25 @@ use {
punctuated::Punctuated,
Error, Result, Token,
},
wiggle_generate::config::WitxConf,
wiggle_generate::config::{CtxConf, WitxConf},
};
#[derive(Debug, Clone)]
pub struct Config {
pub witx: WitxConf,
pub ctx: CtxConf,
}
#[derive(Debug, Clone)]
pub enum ConfigField {
Witx(WitxConf),
Ctx(CtxConf),
}
mod kw {
syn::custom_keyword!(witx);
syn::custom_keyword!(witx_literal);
syn::custom_keyword!(ctx);
}
impl Parse for ConfigField {
@@ -35,6 +38,10 @@ 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 {
Err(lookahead.error())
}
@@ -44,6 +51,7 @@ 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;
for f in fields {
match f {
ConfigField::Witx(c) => {
@@ -52,12 +60,21 @@ impl Config {
}
witx = Some(c);
}
ConfigField::Ctx(c) => {
if ctx.is_some() {
return Err(Error::new(err_loc, "duplicate `ctx` field"));
}
ctx = 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"))?,
})
}

View File

@@ -2,9 +2,9 @@ use proc_macro::TokenStream;
use proc_macro2::{Ident, Span, TokenStream as TokenStream2};
use quote::{format_ident, quote};
use syn::parse_macro_input;
use wiggle_generate::Names;
mod config;
mod utils;
#[proc_macro]
pub fn define_struct_for_wiggle(args: TokenStream) -> TokenStream {
@@ -13,7 +13,9 @@ pub fn define_struct_for_wiggle(args: TokenStream) -> TokenStream {
std::env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR env var"),
);
let doc = config.load_document();
generate(&doc).into()
let names = Names::new(&config.ctx.name, quote!(wasmtime_wiggle));
generate(&doc, &names).into()
}
enum Abi {
@@ -23,7 +25,7 @@ enum Abi {
F64,
}
fn generate(doc: &witx::Document) -> TokenStream2 {
fn generate(doc: &witx::Document, names: &Names) -> TokenStream2 {
let mut fields = Vec::new();
let mut get_exports = Vec::new();
let mut ctor_externs = Vec::new();
@@ -59,7 +61,7 @@ fn generate(doc: &witx::Document) -> TokenStream2 {
let mut hostcall_args = Vec::new();
for param in func.params.iter() {
let name = utils::param_name(param);
let name = names.func_param(&param.name);
// Registers a new parameter to the shim we're making with the
// given `name`, the `abi_ty` wasm type and `hex` defines

View File

@@ -1,11 +0,0 @@
use proc_macro2::Ident;
pub fn param_name(param: &witx::InterfaceFuncParam) -> Ident {
quote::format_ident!(
"{}",
match param.name.as_str() {
"in" | "type" => format!("r#{}", param.name.as_str()),
s => s.to_string(),
}
)
}