feat(wasm): execute client handlers without requests

req: client_local/001\nreq: client_local/004\nreq: client_local/005\nreq: client_local/009\nreq: client_local/010\nreq: client_local/014\nreq: v1_release/001
This commit is contained in:
slhx agent
2026-07-13 12:33:54 +02:00
parent 0d49007c61
commit 40643e360d
16 changed files with 515 additions and 25 deletions
+53 -2
View File
@@ -10,7 +10,20 @@ use syn::{
};
#[proc_macro_attribute]
pub fn handler(_attr: TokenStream, item: TokenStream) -> TokenStream {
pub fn handler(attr: TokenStream, item: TokenStream) -> TokenStream {
let placement = attr.to_string();
let is_client = match placement.as_str() {
"" => false,
"client" => true,
_ => {
return syn::Error::new(
proc_macro2::Span::call_site(),
"unsupported hemx handler placement; expected #[hemx::handler] or #[hemx::handler(client)]",
)
.into_compile_error()
.into();
}
};
let function = parse_macro_input!(item as ItemFn);
let name = function.sig.ident.to_string();
@@ -73,7 +86,45 @@ pub fn handler(_attr: TokenStream, item: TokenStream) -> TokenStream {
.into();
}
quote!(#function).into()
if !is_client {
return quote!(#function).into();
}
if !function.sig.inputs.is_empty()
|| function.sig.asyncness.is_some()
|| function.sig.unsafety.is_some()
|| function.sig.constness.is_some()
|| !function.sig.generics.params.is_empty()
{
let message = format!(
"client-local hemx handler `{name}` must be a safe, synchronous, non-generic function with no parameters"
);
return quote!(
#function
compile_error!(#message);
)
.into();
}
let function_name = &function.sig.ident;
let export_name = format_ident!("__hemx_client_{function_name}");
let export_module = format_ident!("__hemx_client_export_{function_name}");
quote!(
#function
#[cfg(target_arch = "wasm32")]
mod #export_module {
use ::hemx::wasm as wasm_bindgen;
#[::hemx::wasm::wasm_bindgen(js_name = #export_name)]
pub fn invoke() -> ::std::vec::Vec<u8> {
::hemx::wasm::encode_handler_effect(
super::#function_name(),
crate::ui::BUILD_FINGERPRINT,
)
}
}
)
.into()
}
#[proc_macro_attribute]