1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#![doc(html_root_url = "https://docs.rs/fluence-sdk-macro/0.1.8")]
#![recursion_limit = "128"]
extern crate proc_macro;
mod macro_attr_parser;
mod macro_input_parser;
use crate::macro_attr_parser::{generate_side_modules_glue_code, HandlerAttrs};
use crate::macro_input_parser::{InputTypeGenerator, ParsedType, ReturnTypeGenerator};
use proc_macro::TokenStream;
use quote::quote;
use syn::spanned::Spanned;
use syn::{parse::Error, parse_macro_input, ItemFn};
fn invoke_handler_impl(
attr: proc_macro2::TokenStream,
fn_item: syn::ItemFn,
) -> syn::Result<proc_macro2::TokenStream> {
let ItemFn {
constness,
unsafety,
abi,
ident,
decl,
..
} = &fn_item;
if let Err(e) = (|| {
if let Some(constness) = constness {
return Err(Error::new(
constness.span,
"The invocation handler shouldn't be constant",
));
}
if let Some(unsafety) = unsafety {
return Err(Error::new(
unsafety.span,
"The invocation handler shouldn't be unsage",
));
}
if let Some(abi) = abi {
return Err(Error::new(
abi.extern_token.span,
"The invocation handler shouldn't have any custom linkage",
));
}
if !decl.generics.params.is_empty() || decl.generics.where_clause.is_some() {
return Err(Error::new(
decl.fn_token.span,
"The invocation handler shouldn't use template parameters",
));
}
if let Some(variadic) = decl.variadic {
return Err(Error::new(
variadic.spans[0],
"The invocation handler shouldn't use variadic interface",
));
}
Ok(())
})() {
return Err(e);
}
let input_type = match decl.inputs.len() {
0 => ParsedType::Empty,
1 => ParsedType::from_fn_arg(decl.inputs.first().unwrap().into_value())?,
_ => {
return Err(Error::new(
decl.inputs.span(),
"The invocation handler shouldn't have more than one argument",
))
},
};
let output_type = ParsedType::from_return_type(&decl.output)?;
if output_type == ParsedType::Empty {
return Err(Error::new(
decl.output.span(),
"The invocation handler should have the return value",
));
}
let prolog = input_type.generate_fn_prolog();
let prolog = match input_type {
ParsedType::Empty => quote! {
#prolog
let result = #ident();
},
_ => quote! {
#prolog
let result = #ident(arg);
},
};
let epilog = output_type.generate_fn_epilog();
let attrs = syn::parse2::<HandlerAttrs>(attr)?;
let raw_init_fn_name = attrs.init_fn_name();
let raw_side_modules_list = attrs.side_modules();
let resulted_invoke = match raw_init_fn_name {
Some(init_fn_name) => {
let init_fn_name = syn::parse_str::<syn::Ident>(init_fn_name)?;
quote! {
#fn_item
static mut __FLUENCE_SDK_IS_INITED_d28374a960b570e5db00dfe7a0c7b93: bool = false;
#[no_mangle]
pub unsafe fn invoke(ptr: *mut u8, len: usize) -> std::ptr::NonNull<u8> {
if !__FLUENCE_SDK_IS_INITED_d28374a960b570e5db00dfe7a0c7b93 {
#init_fn_name();
unsafe { __FLUENCE_SDK_IS_INITED_d28374a960b570e5db00dfe7a0c7b93 = true; }
}
#prolog
#epilog
}
}
},
None => quote! {
#fn_item
#[no_mangle]
pub unsafe fn invoke(ptr: *mut u8, len: usize) -> std::ptr::NonNull<u8> {
#prolog
#epilog
}
},
};
match raw_side_modules_list {
Some(side_modules) => {
let side_modules_glue_code = generate_side_modules_glue_code(side_modules)?;
Ok(quote! {
#side_modules_glue_code
#resulted_invoke
})
},
_ => Ok(resulted_invoke),
}
}
#[proc_macro_attribute]
pub fn invocation_handler(attr: TokenStream, input: TokenStream) -> TokenStream {
let fn_item = parse_macro_input!(input as ItemFn);
match invoke_handler_impl(attr.into(), fn_item) {
Ok(v) => v,
Err(e) => e.to_compile_error(),
}
.into()
}