Files
wasmtime/crates/wasi-common/wig/src/utils.rs
Alex Crichton 5953215bac Auto-generate the hostcalls module of wasi-common (#846)
* Auto-generate shims for old `wasi_unstable` module

This commit is effectively just doing what #707 already did, but
applying it to the `snapshot_0` module as well. The end result is the
same, where we cut down on all the boilerplate in `snapshot_0` and bring
it in line with the main `wasi_snapshot_preview1` implementation. The
goal here is to make it easier to change the two in tandem since they're
both doing the same thing.

* Migrate `wasi_common::hostcalls` to a macro

This commit migrates the `hostcalls` module to being auto-generated by a
macro rather than duplicating a handwritten signature for each wasi
syscall.

* Auto-generate snapshot_0's `hostcalls` module

Similar to the previous commit, but for `snapshot_0`

* Delete the `wasi-common-cbindgen` crate

This is no longer needed with the hostcalls macro now, we can easily
fold the definition of the cbindgen macro into the same crate.

* Rustfmt

* Fix windows build errors

* Rustfmt

* Remove now no-longer-necessary code

* rustfmt
2020-01-22 14:54:39 -06:00

66 lines
1.8 KiB
Rust

use proc_macro2::{Ident, Literal, TokenStream, TokenTree};
/// Given the input tokens to a macro invocation, return the path to the
/// witx file to process.
pub(crate) fn witx_path_from_args(args: TokenStream) -> (String, String) {
let mut strings = Vec::new();
for arg in args {
if let TokenTree::Literal(literal) = arg {
let parsed = parse_string_literal(literal);
strings.push(parsed);
} else {
panic!("arguments must be string literals");
}
}
if strings.len() != 2 {
panic!("expected two string literals");
}
let phase = &strings[0];
let id = &strings[1];
let path = witx_path(phase, id);
(path, phase.clone())
}
fn witx_path(phase: &str, id: &str) -> String {
let root = env!("CARGO_MANIFEST_DIR");
format!("{}/WASI/phases/{}/witx/{}.witx", root, phase, id)
}
// Convert a `Literal` holding a string literal into the `String`.
//
// FIXME: It feels like there should be an easier way to do this.
fn parse_string_literal(literal: Literal) -> String {
let s = literal.to_string();
assert!(
s.starts_with('"') && s.ends_with('"'),
"string literal must be enclosed in double-quotes"
);
let trimmed = s[1..s.len() - 1].to_owned();
assert!(
!trimmed.contains('"'),
"string literal must not contain embedded quotes for now"
);
assert!(
!trimmed.contains('\\'),
"string literal must not contain embedded backslashes for now"
);
trimmed
}
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(),
}
)
}