Skip to content

Commit

Permalink
simplify generated builtins syntax
Browse files Browse the repository at this point in the history
Rather than constructing a bunch of intermediate variables, inline
them into the final array.  Visually about the same but a lot less text.
  • Loading branch information
evmar committed Sep 24, 2024
1 parent ce12d36 commit 221153b
Show file tree
Hide file tree
Showing 3 changed files with 1,407 additions and 1,939 deletions.
8 changes: 4 additions & 4 deletions win32/derive/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub fn fn_wrapper(module: TokenStream, dllexport: &DllExport) -> (TokenStream, T
.collect::<Vec<_>>();
let (func, defn) = if dllexport.func.sig.asyncness.is_some() {
(
quote!(crate::shims::Handler::Async(impls::#sym_name)),
quote!(Handler::Async(impls::#sym_name)),
quote! {
pub unsafe fn #sym_name(machine: &mut Machine, esp: u32) -> std::pin::Pin<Box<dyn std::future::Future<Output = u32>>> {
#fetch_args
Expand All @@ -52,7 +52,7 @@ pub fn fn_wrapper(module: TokenStream, dllexport: &DllExport) -> (TokenStream, T
)
} else {
(
quote!(crate::shims::Handler::Sync(impls::#sym_name)),
quote!(Handler::Sync(impls::#sym_name)),
quote! {
pub unsafe fn #sym_name(machine: &mut Machine, esp: u32) -> u32 {
#fetch_args
Expand All @@ -69,9 +69,9 @@ pub fn fn_wrapper(module: TokenStream, dllexport: &DllExport) -> (TokenStream, T

(
defn,
quote!(pub const #sym_name: Shim = Shim {
quote!(Shim {
name: #name_str,
func: #func,
};),
}),
)
}
16 changes: 3 additions & 13 deletions win32/derive/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,13 @@ fn generate_shims_module(module_name: &str, dllexports: parse::DllExports) -> To

let mut impls = Vec::new();
let mut shims = Vec::new();
let mut shims_list = Vec::new();
for dllexport in &dllexports.fns {
let (wrapper, shim) = gen::fn_wrapper(quote! { winapi::#module }, dllexport);
impls.push(wrapper);
shims.push(shim);

let sym_name = &dllexport.sym_name;
shims_list.push(quote!(shims::#sym_name));
}

let shims_count = shims_list.len();
let shims_count = shims.len();
let raw_dll_path = format!("../../dll/{}", dll_name);
quote! {
pub mod #module {
Expand All @@ -122,14 +118,8 @@ fn generate_shims_module(module_name: &str, dllexports: parse::DllExports) -> To
#(#impls)*
}

mod shims {
use super::impls;
use crate::shims::Shim;
#(#shims)*
}

const SHIMS: [Shim; #shims_count] = [
#(#shims_list),*
#(#shims),*
];

pub const DLL: BuiltinDLL = BuiltinDLL {
Expand Down Expand Up @@ -166,7 +156,7 @@ fn generate_builtins_module(mods: Vec<TokenStream>) -> anyhow::Result<String> {
let out = quote! {
/// Generated code, do not edit.
use crate::shims::Shim;
use crate::shims::{Shim, Handler};

pub struct BuiltinDLL {
pub file_name: &'static str,
Expand Down
Loading

0 comments on commit 221153b

Please sign in to comment.