Skip to content
Merged
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
64 changes: 57 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions src/hyperlight_component_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub fn host_bindgen(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
rtypes::emit_toplevel(s, &kebab_name, ct);
host::emit_toplevel(s, &kebab_name, ct);
});
util::emit_decls(decls).into()
util::emit_decls(decls, &kebab_name).into()
})
}

Expand Down Expand Up @@ -106,7 +106,7 @@ pub fn guest_bindgen(input: proc_macro::TokenStream) -> proc_macro::TokenStream
// stream directly and emitting an include!() pointing at a
// temporary file, depending on whether the user has requested
// a debug temporary file be created.
util::emit_decls(decls).into()
util::emit_decls(decls, &kebab_name).into()
})
}

Expand All @@ -129,7 +129,7 @@ fn unknown_key_error(key: &Ident) -> syn::Error {
syn::Error::new(
key.span(),
format!(
"unknown parameter '{}'; expected 'path', 'wit', 'wasm', 'inline', 'world', or 'world_name'",
"unknown parameter '{}'; expected 'path', 'wit', 'wasm', 'wat', 'inline', 'world', or 'world_name'",
key
),
)
Expand All @@ -146,6 +146,9 @@ fn source_from_key(key: &Ident, value: LitStr) -> Result<util::WitSource> {
"wasm" => Ok(util::WitSource::Wasm(std::path::PathBuf::from(
value.value(),
))),
"wat" => Ok(util::WitSource::Wat(std::path::PathBuf::from(
value.value(),
))),
"inline" => Ok(util::WitSource::Inline(value.value())),
_ => Err(unknown_key_error(key)),
}
Expand All @@ -170,7 +173,7 @@ impl Parse for BindgenInputParams {
let value: LitStr = content.parse()?;
world_name = Some(value.value());
}
"path" | "wit" | "wasm" | "inline" => {
"path" | "wit" | "wasm" | "wat" | "inline" => {
let value: LitStr = content.parse()?;
if source.is_some() {
return Err(syn::Error::new(
Expand All @@ -193,7 +196,7 @@ impl Parse for BindgenInputParams {
let value: LitStr = input.parse()?;
match key.to_string().as_str() {
"world" | "world_name" => world_name = Some(value.value()),
"path" | "wit" | "wasm" | "inline" => {
"path" | "wit" | "wasm" | "wat" | "inline" => {
source = Some(source_from_key(&key, value)?);
}
_ => return Err(unknown_key_error(&key)),
Expand Down
1 change: 1 addition & 0 deletions src/hyperlight_component_util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ name = "hyperlight_component_util"

[dependencies]
wasmparser = { version = "0.255.0" }
wat = { version = "1.256.0" }
wit-component = { version = "0.255.0" }
wit-parser = { version = "0.255.0" }
quote = { version = "1.0.45" }
Expand Down
52 changes: 43 additions & 9 deletions src/hyperlight_component_util/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::etypes;
#[derive(Debug)]
pub enum WitSource {
Wasm(std::path::PathBuf),
Wat(std::path::PathBuf),
Wit(std::path::PathBuf),
Inline(String),
}
Expand All @@ -44,6 +45,16 @@ impl WitSource {
}
bytes
}
Self::Wat(path) => {
let path = manifest_path(&path);
let bytes = wat::parse_file(&path).unwrap_or_else(|err| {
panic!("failed to read wat input '{}': {err:#}", path.display());
});
Comment thread
syntactically marked this conversation as resolved.
if !wasmparser::Parser::is_component(&bytes) {
panic!("wat input '{}' is not a wasm component", path.display());
}
bytes
}
Comment thread
syntactically marked this conversation as resolved.
Self::Wit(path) => {
let path = manifest_path(&path);
let mut resolve = wit_parser::Resolve::default();
Expand All @@ -59,14 +70,33 @@ impl WitSource {
})
}
Self::Inline(contents) => {
let mut resolve = wit_parser::Resolve::default();
let package = resolve
.push_str("inline.wit", &contents)
.unwrap_or_else(|err| panic!("failed to parse inline WIT input: {err:#}"));

wit_component::encode(&resolve, package).unwrap_or_else(|err| {
panic!("failed to encode inline WIT input as a wasm component type: {err:#}")
})
match wat::Detect::from_bytes(&contents) {
wat::Detect::WasmBinary => {
panic!("inline component type looks like a binary!")
}
Comment thread
syntactically marked this conversation as resolved.
wat::Detect::WasmText => {
let bytes = wat::parse_str(&contents).unwrap_or_else(|err| {
panic!("failed to read inline wat input: {err:#}")
});
if !wasmparser::Parser::is_component(&bytes) {
panic!("inline wat input is not a wasm component");
}
bytes
}
wat::Detect::Unknown => {
// It's probably WIT
let mut resolve = wit_parser::Resolve::default();
let package =
resolve
.push_str("inline.wit", &contents)
.unwrap_or_else(|err| {
panic!("failed to parse inline WIT input: {err:#}")
});
wit_component::encode(&resolve, package).unwrap_or_else(|err| {
panic!("failed to encode inline WIT input as a wasm component type: {err:#}")
})
}
}
}
}
}
Expand Down Expand Up @@ -119,8 +149,12 @@ pub fn read_wit_type_from_file<R, F: FnMut(String, &etypes::Component) -> R>(
/// Deal with `$HYPERLIGHT_COMPONENT_MACRO_DEBUG`: if it is present,
/// save the given token stream (representing the result of
/// macroexpansion) to the debug file and then return the token stream
pub fn emit_decls(decls: proc_macro2::TokenStream) -> proc_macro2::TokenStream {
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 {
Expand Down
72 changes: 64 additions & 8 deletions src/tests/rust_guests/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading