Skip to content

Commit

Permalink
Fix linkme call for multiple callback definitions (#1216)
Browse files Browse the repository at this point in the history
commit-id:aee4271b

---

**Stack**:
- #1220
- #1219
- #1218
- #1217
- #1216⚠️ *Part of a stack created by [spr](https://github.com/ejoffe/spr). Do
not merge manually using the UI - doing so may have unexpected results.*
  • Loading branch information
maciektr authored Mar 22, 2024
1 parent f5be802 commit 87b2512
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 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
6 changes: 3 additions & 3 deletions plugins/cairo-lang-macro/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl TokenStreamMetadata {
/// For instance, auxiliary data can be serialized as JSON.
///
/// ```
/// use cairo_lang_macro::{AuxData, ProcMacroResult, TokenStream, attribute_macro, post_process_callback}
/// use cairo_lang_macro::{AuxData, ProcMacroResult, TokenStream, attribute_macro, post_process}
/// use serde::{Serialize, Deserialize};
/// #[derive(Debug, Serialize, Deserialize)]
/// struct SomeAuxDataFormat {
Expand All @@ -117,7 +117,7 @@ impl TokenStreamMetadata {
/// ProcMacroResult::replace(token_stream, Some(aux_data))
/// }
///
/// #[post_process_callback]
/// #[post_process]
/// pub fn callback(aux_data: Vec<AuxData>) {
/// let aux_data = aux_data.into_iter()
/// .map(|aux_data| {
Expand All @@ -131,7 +131,7 @@ impl TokenStreamMetadata {
/// ```
///
/// All auxiliary data emitted during compilation can be consumed
/// in the `post_process_callback` implementation.
/// in the `post_process` implementation.
#[derive(Debug, Clone)]
pub struct AuxData(Vec<u8>);

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]
pub fn some_no_op_callback(aux_data: Vec<AuxData>) {
drop(aux_data);
}
"##},
);

Expand Down

0 comments on commit 87b2512

Please sign in to comment.