Skip to content

Commit

Permalink
Fix linkme call for multiple callback definitions
Browse files Browse the repository at this point in the history
commit-id:aee4271b
  • Loading branch information
maciektr committed Mar 22, 2024
1 parent b125447 commit e7c0955
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
21 changes: 19 additions & 2 deletions plugins/cairo-lang-macro-attributes/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use proc_macro::TokenStream;
use quote::quote;
use scarb_stable_hash::short_hash;
use syn::spanned::Spanned;
use syn::{parse_macro_input, ItemFn};

/// Constructs the attribute macro implementation.
Expand All @@ -14,18 +15,26 @@ pub fn attribute_macro(_args: TokenStream, input: TokenStream) -> TokenStream {
let original_item_name = item.sig.ident.to_string();
let item = hide_name(item);
let item_name = &item.sig.ident;

let callback_link = format!(
"EXPANSIONS_DESERIALIZE_{}",
item_name.to_string().to_uppercase()
);
let callback_link = syn::Ident::new(callback_link.as_str(), item.span());

let expanded = quote! {
#item

#[::cairo_lang_macro::linkme::distributed_slice(::cairo_lang_macro::MACRO_DEFINITIONS_SLICE)]
#[linkme(crate = ::cairo_lang_macro::linkme)]
static MACRO_DEFINITIONS_SLICE_DESERIALIZE: ::cairo_lang_macro::ExpansionDefinition =
static #callback_link: ::cairo_lang_macro::ExpansionDefinition =
::cairo_lang_macro::ExpansionDefinition{
name: #original_item_name,
kind: ::cairo_lang_macro::ExpansionKind::Attr,
fun: #item_name,
};
};

TokenStream::from(expanded)
}

Expand Down Expand Up @@ -55,13 +64,21 @@ pub fn post_process(_args: TokenStream, input: TokenStream) -> TokenStream {
let item: ItemFn = parse_macro_input!(input as ItemFn);
let item = hide_name(item);
let item_name = &item.sig.ident;

let callback_link = format!(
"POST_PROCESS_DESERIALIZE_{}",
item_name.to_string().to_uppercase()
);
let callback_link = syn::Ident::new(callback_link.as_str(), item.span());

let expanded = quote! {
#item

#[::cairo_lang_macro::linkme::distributed_slice(::cairo_lang_macro::AUX_DATA_CALLBACKS)]
#[linkme(crate = ::cairo_lang_macro::linkme)]
static AUX_DATA_CALLBACK_DESERIALIZE: fn(Vec<AuxData>) = #item_name;
static #callback_link: fn(Vec<AuxData>) = #item_name;
};

TokenStream::from(expanded)
}

Expand Down
5 changes: 5 additions & 0 deletions scarb/tests/build_cairo_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,11 @@ fn can_return_aux_data_from_plugin() {
.collect::<Vec<_>>();
println!("{:?}", aux_data);
}
#[post_process_callback]
pub fn some_no_op_callback(aux_data: Vec<AuxData>) {
drop(aux_data);
}
"##},
);

Expand Down

0 comments on commit e7c0955

Please sign in to comment.