Minor refactors to make wiggle-generate reusable externally (#34)
* wiggle-generate: pass witx doc in explicitly * wiggle-generate: Names takes &Config, minor refactor for reuse
This commit is contained in:
@@ -9,6 +9,7 @@ proc-macro = true
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
wiggle-generate = { path = "crates/generate" }
|
wiggle-generate = { path = "crates/generate" }
|
||||||
|
witx = "0.8.3"
|
||||||
syn = { version = "1.0", features = ["full"] }
|
syn = { version = "1.0", features = ["full"] }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
|
|||||||
@@ -11,22 +11,10 @@ pub fn define_func(names: &Names, func: &witx::InterfaceFunc) -> TokenStream {
|
|||||||
let ctx_type = names.ctx_type();
|
let ctx_type = names.ctx_type();
|
||||||
let coretype = func.core_type();
|
let coretype = func.core_type();
|
||||||
|
|
||||||
let params = coretype.args.iter().map(|arg| match arg.signifies {
|
let params = coretype.args.iter().map(|arg| {
|
||||||
witx::CoreParamSignifies::Value(atom) => {
|
let name = names.func_core_arg(arg);
|
||||||
let atom = names.atom_type(atom);
|
let atom = names.atom_type(arg.repr());
|
||||||
let name = names.func_param(&arg.param.name);
|
|
||||||
quote!(#name : #atom)
|
quote!(#name : #atom)
|
||||||
}
|
|
||||||
witx::CoreParamSignifies::PointerTo => {
|
|
||||||
let atom = names.atom_type(witx::AtomType::I32);
|
|
||||||
let name = names.func_ptr_binding(&arg.param.name);
|
|
||||||
quote!(#name: #atom)
|
|
||||||
}
|
|
||||||
witx::CoreParamSignifies::LengthOf => {
|
|
||||||
let atom = names.atom_type(witx::AtomType::I32);
|
|
||||||
let name = names.func_len_binding(&arg.param.name);
|
|
||||||
quote!(#name: #atom)
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let abi_args = quote!(
|
let abi_args = quote!(
|
||||||
|
|||||||
@@ -14,9 +14,7 @@ pub use module_trait::define_module_trait;
|
|||||||
pub use names::Names;
|
pub use names::Names;
|
||||||
pub use types::define_datatype;
|
pub use types::define_datatype;
|
||||||
|
|
||||||
pub fn generate(config: Config) -> TokenStream {
|
pub fn generate(doc: &witx::Document, config: &Config) -> TokenStream {
|
||||||
let doc = witx::load(&config.witx.paths).expect("loading witx");
|
|
||||||
|
|
||||||
let names = Names::new(config); // TODO parse the names from the invocation of the macro, or from a file?
|
let names = Names::new(config); // TODO parse the names from the invocation of the macro, or from a file?
|
||||||
|
|
||||||
let types = doc.typenames().map(|t| define_datatype(&names, &t));
|
let types = doc.typenames().map(|t| define_datatype(&names, &t));
|
||||||
|
|||||||
@@ -12,8 +12,10 @@ pub struct Names {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Names {
|
impl Names {
|
||||||
pub fn new(config: Config) -> Names {
|
pub fn new(config: &Config) -> Names {
|
||||||
Names { config }
|
Names {
|
||||||
|
config: config.clone(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
pub fn ctx_type(&self) -> Ident {
|
pub fn ctx_type(&self) -> Ident {
|
||||||
self.config.ctx.name.clone()
|
self.config.ctx.name.clone()
|
||||||
@@ -118,6 +120,14 @@ impl Names {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn func_core_arg(&self, arg: &witx::CoreParamType) -> Ident {
|
||||||
|
match arg.signifies {
|
||||||
|
witx::CoreParamSignifies::Value { .. } => self.func_param(&arg.param.name),
|
||||||
|
witx::CoreParamSignifies::PointerTo => self.func_ptr_binding(&arg.param.name),
|
||||||
|
witx::CoreParamSignifies::LengthOf => self.func_len_binding(&arg.param.name),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// For when you need a {name}_ptr binding for passing a value by reference:
|
/// For when you need a {name}_ptr binding for passing a value by reference:
|
||||||
pub fn func_ptr_binding(&self, id: &Id) -> Ident {
|
pub fn func_ptr_binding(&self, id: &Id) -> Ident {
|
||||||
format_ident!("{}_ptr", id.as_str().to_snake_case())
|
format_ident!("{}_ptr", id.as_str().to_snake_case())
|
||||||
|
|||||||
@@ -6,5 +6,6 @@ use syn::parse_macro_input;
|
|||||||
#[proc_macro]
|
#[proc_macro]
|
||||||
pub fn from_witx(args: TokenStream) -> TokenStream {
|
pub fn from_witx(args: TokenStream) -> TokenStream {
|
||||||
let config = parse_macro_input!(args as wiggle_generate::Config);
|
let config = parse_macro_input!(args as wiggle_generate::Config);
|
||||||
TokenStream::from(wiggle_generate::generate(config))
|
let doc = witx::load(&config.witx.paths).expect("loading witx");
|
||||||
|
TokenStream::from(wiggle_generate::generate(&doc, &config))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user