Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/hyperlight_component_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
//! The generated code can be examined by setting the environment
//! variable `$HYPERLIGHT_COMPONENT_MACRO_DEBUG=/path/to/file.rs`,
//! which will result in the generated code being written to that
//! file, which is then included back into the Rust source.
//! file. If the path contains `#`, it is replaced with the world
//! name. The file is written for inspection only: the macro still
//! expands to the tokens it generated, so multiple invocations in
//! one crate do not overwrite each other's expansions.
//!
//! The macros also can be configured to output a great deal of debug
//! information about the internal elaboration and codegen
Expand Down
55 changes: 43 additions & 12 deletions src/hyperlight_component_util/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,18 +142,49 @@ pub fn read_wit_type_from_file<R, F: FnMut(String, &etypes::Component) -> R>(
/// macroexpansion) to the debug file and then return the token stream
pub fn emit_decls(decls: proc_macro2::TokenStream, for_kebab: &str) -> proc_macro2::TokenStream {
if let Ok(dbg_out) = std::env::var("HYPERLIGHT_COMPONENT_MACRO_DEBUG") {
let fs_safe = for_kebab.replace("/", "_");
#[cfg(windows)]
let fs_safe = fs_safe.replace(":", "+");
let dbg_out = dbg_out.replace("#", &fs_safe);
if let Ok(file) = syn::parse2(decls.clone()) {
std::fs::write(&dbg_out, prettyplease::unparse(&file)).unwrap();
} else {
let decls = format!("{}", decls);
std::fs::write(&dbg_out, &decls).unwrap();
}
quote::quote! { include!(#dbg_out); }
write_debug_file(&dbg_out, &decls, for_kebab);
}
decls
}

/// Write `decls` to the file named by the
/// `$HYPERLIGHT_COMPONENT_MACRO_DEBUG` template, replacing `#` with
/// `for_kebab` (made safe for the filesystem).
fn write_debug_file(template: &str, decls: &proc_macro2::TokenStream, for_kebab: &str) {
let fs_safe = for_kebab.replace("/", "_");
#[cfg(windows)]
let fs_safe = fs_safe.replace(":", "+");
let dbg_out = template.replace("#", &fs_safe);
if let Ok(file) = syn::parse2(decls.clone()) {
std::fs::write(&dbg_out, prettyplease::unparse(&file)).unwrap();
} else {
decls
let decls = format!("{}", decls);
std::fs::write(&dbg_out, &decls).unwrap();
}
}

#[cfg(test)]
mod tests {
use proc_macro2::TokenStream;

use super::{emit_decls, write_debug_file};

#[test]
fn emit_decls_returns_its_input() {
let decls: TokenStream = "fn generated() -> u32 { 1 }".parse().unwrap();
let out = emit_decls(decls.clone(), "test:world/thing");
assert_eq!(out.to_string(), decls.to_string());
}

#[test]
fn write_debug_file_substitutes_kebab() {
let dir = std::env::temp_dir().join(format!("hyperlight-debug-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let template = dir.join("#.rs");
let decls: TokenStream = "fn generated() -> u32 { 1 }".parse().unwrap();
write_debug_file(&template.to_string_lossy(), &decls, "test/world_thing");
let written = std::fs::read_to_string(dir.join("test_world_thing.rs")).unwrap();
std::fs::remove_dir_all(&dir).unwrap();
assert!(written.contains("fn generated()"));
}
}