Skip to content

Commit 8068d06

Browse files
committed
Add mechanism for displaying the WAT signatures of WASI modules
1 parent 6e5800b commit 8068d06

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

tools/witx/examples/witx.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ enum Command {
4242
},
4343
/// Update documentation in WASI repository to reflect witx specs
4444
RepoDocs,
45+
/// Output the expected WAT signature of a witx module
46+
WatSignature {
47+
/// Path to root of witx document
48+
#[structopt(number_of_values = 1, value_name = "INPUT", parse(from_os_str))]
49+
input: Vec<PathBuf>,
50+
},
4551
/// Examine differences between interfaces
4652
Polyfill {
4753
/// Path to root of witx document
@@ -98,6 +104,35 @@ pub fn main() {
98104
write_docs(&doc, phases::docs_path(&phase));
99105
}
100106
}
107+
Command::WatSignature { input } => {
108+
let doc = load_witx(&input, "input", verbose);
109+
for module in doc.modules() {
110+
println!("(module");
111+
// For each function in the module, print something like:
112+
// (import "wasi" "load" (func (param i32 i32 i32) (result i32)))
113+
for func in module.funcs() {
114+
print!(
115+
" (import \"{}\" \"{}\" (func ",
116+
module.name.as_str(),
117+
func.name.as_str()
118+
);
119+
let signature = func.core_type();
120+
if !signature.args.is_empty() {
121+
let args: Vec<String> = signature
122+
.args
123+
.iter()
124+
.map(|arg| format!("{:?}", arg.repr()))
125+
.collect();
126+
print!("(param {})", args.join(" "));
127+
}
128+
if let Some(ret) = signature.ret {
129+
print!(" (result {:?})", ret.repr())
130+
}
131+
println!("))");
132+
}
133+
println!(")");
134+
}
135+
}
101136
Command::Polyfill {
102137
input,
103138
older_interface,

0 commit comments

Comments
 (0)