1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
mod codegen_settings;
pub mod config;
mod funcs;
mod lifetimes;
mod module_trait;
mod names;
mod types;
pub mod wasmtime;
use heck::ToShoutySnakeCase;
use lifetimes::anon_lifetime;
use proc_macro2::{Literal, TokenStream};
use quote::quote;
pub use codegen_settings::{CodegenSettings, UserErrorType};
pub use config::{Config, WasmtimeConfig};
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, settings: &CodegenSettings) -> TokenStream {
let rt = names.runtime_mod();
let types = doc.typenames().map(|t| define_datatype(&names, &t));
let constants = doc.constants().map(|c| {
let name = quote::format_ident!(
"{}_{}",
c.ty.as_str().to_shouty_snake_case(),
c.name.as_str().to_shouty_snake_case()
);
let ty = names.type_(&c.ty);
let value = Literal::u64_unsuffixed(c.value);
quote! {
pub const #name: #ty = #value;
}
});
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);
quote!(fn #methodname(&mut self, e: super::#user_typename) -> Result<#abi_typename, #rt::Trap>;)
});
let user_error_conversion = quote! {
pub trait UserErrorConversion {
#(#user_error_methods)*
}
};
let modules = doc.modules().map(|module| {
let modname = names.module(&module.name);
let fs = module
.funcs()
.map(|f| define_func(&names, &module, &f, &settings));
let modtrait = define_module_trait(&names, &module, &settings);
let wasmtime = if settings.wasmtime {
crate::wasmtime::link_module(&module, &names, None, &settings)
} else {
quote! {}
};
quote!(
pub mod #modname {
use super::types::*;
pub use super::types::UserErrorConversion;
#(#fs)*
#modtrait
#wasmtime
}
)
});
quote!(
pub mod types {
use std::convert::TryFrom;
#(#types)*
#(#constants)*
#user_error_conversion
}
#(#modules)*
)
}
pub fn generate_metadata(doc: &witx::Document, names: &Names) -> TokenStream {
let rt = names.runtime_mod();
let doc_text = &format!("{}", doc);
quote! {
pub mod metadata {
pub const DOC_TEXT: &str = #doc_text;
pub fn document() -> #rt::witx::Document {
#rt::witx::parse(DOC_TEXT).unwrap()
}
}
}
}