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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
use crate::{primitives::Primitive, utils::extract_path_from_type};
use proc_macro::{TokenStream, TokenTree};
use proc_macro_error::{abort, proc_macro_error, ResultExt};
use quote::{format_ident, quote, ToTokens};
use std::{
    collections::{HashMap, HashSet},
    iter::once,
};
use syn::{
    AttributeArgs, FnArg, ForeignItemFn, GenericParam, ItemFn, ItemType, ItemUse, Pat, PatPath,
    Path, PathArguments, PathSegment, ReturnType,
};
use utils::{flatten_using_statement, normalize_return_type};

mod primitives;
mod serializable;
mod typing;
mod utils;

/// Used to annotate types (`enum`s and `struct`s) that can be passed across the Wasm bridge.
#[proc_macro_derive(Serializable, attributes(fp))]
pub fn derive_serializable(item: TokenStream) -> TokenStream {
    crate::serializable::impl_derive_serializable(item)
}

/// Declares functions the plugin can import from the host runtime.
#[proc_macro]
pub fn fp_import(token_stream: TokenStream) -> TokenStream {
    let ParsedStatements {
        functions,
        type_paths,
        aliases,
    } = parse_statements(token_stream);
    let type_paths = type_paths.iter();
    let alias_keys = aliases.keys();
    let alias_paths = aliases
        .values()
        .map(|path| path.to_token_stream().to_string());

    let replacement = quote! {
        fn __fp_declare_import_fns() -> (fp_bindgen::prelude::FunctionList, fp_bindgen::prelude::TypeMap) {
            let mut import_types = fp_bindgen::prelude::TypeMap::new();
            #( #type_paths::collect_types(&mut import_types); )*
            #( import_types.insert(TypeIdent::from(#alias_keys), Type::Alias(#alias_keys.to_owned(), std::str::FromStr::from_str(#alias_paths).unwrap())); )*

            let mut list = fp_bindgen::prelude::FunctionList::new();
            #( list.add_function(#functions); )*

            (list, import_types)
        }
    };
    replacement.into()
}

/// Declares functions the plugin may export to the host runtime.
#[proc_macro]
pub fn fp_export(token_stream: TokenStream) -> TokenStream {
    let ParsedStatements {
        functions,
        type_paths,
        aliases,
    } = parse_statements(token_stream);
    let type_paths = type_paths.iter();
    let alias_keys = aliases.keys();
    let alias_paths = aliases
        .values()
        .map(|path| path.to_token_stream().to_string());

    let replacement = quote! {
        fn __fp_declare_export_fns() -> (fp_bindgen::prelude::FunctionList, fp_bindgen::prelude::TypeMap) {
            let mut export_types = fp_bindgen::prelude::TypeMap::new();
            #( #type_paths::collect_types(&mut export_types); )*
            #( export_types.insert(TypeIdent::from(#alias_keys), Type::Alias(#alias_keys.to_owned(), std::str::FromStr::from_str(#alias_paths).unwrap())); )*

            let mut list = fp_bindgen::prelude::FunctionList::new();
            #( list.add_function(#functions); )*

            (list, export_types)
        }
    };
    replacement.into()
}

/// Contains all the relevant information extracted from inside the `fp_import!` and `fp_export!`
/// macros.
struct ParsedStatements {
    pub functions: Vec<String>,
    pub type_paths: HashSet<Path>,
    pub aliases: HashMap<String, Path>,
}

/// Parses statements like function declarations and 'use Foobar;' and returns them in a list.
/// In addition, it returns a list of doc lines for every function as well.
/// Finally, it returns two sets: one with all the paths for types that may need serialization
/// to call the functions, and one with all the paths for types that may need deserialization to
/// call the functions.
fn parse_statements(token_stream: TokenStream) -> ParsedStatements {
    let mut functions = Vec::new();
    let mut type_paths = HashSet::new();
    let mut aliases = HashMap::new();

    let mut current_item_tokens = Vec::<TokenTree>::new();
    for token in token_stream.into_iter() {
        match token {
            TokenTree::Punct(punct) if punct.as_char() == ';' => {
                current_item_tokens.push(TokenTree::Punct(punct));

                let stream = current_item_tokens.into_iter().collect::<TokenStream>();

                if let Ok(function) = syn::parse::<ForeignItemFn>(stream.clone()) {
                    for input in &function.sig.inputs {
                        match input {
                            FnArg::Receiver(_) => panic!(
                                "Methods are not supported. Found `self` in function declaration: {:?}",
                                function.sig
                            ),
                            FnArg::Typed(arg) => {
                                type_paths.insert(
                                    extract_path_from_type(arg.ty.as_ref()).unwrap_or_else(|| {
                                        panic!(
                                            "Only value types are supported. \
                                                Incompatible argument type in function declaration: {:?}",
                                            function.sig
                                        )
                                    }),
                                );
                            }
                        }
                    }

                    if let Some(ty) = normalize_return_type(&function.sig.output) {
                        type_paths.insert(extract_path_from_type(ty).unwrap_or_else(|| {
                            panic!(
                                "Only value types are supported. \
                                            Incompatible return type in function declaration: {:?}",
                                function.sig
                            )
                        }));
                    }

                    functions.push(function.into_token_stream().to_string());
                } else if let Ok(using) = syn::parse::<ItemUse>(stream.clone()) {
                    for path in flatten_using_statement(using) {
                        type_paths.insert(path);
                    }
                } else if let Ok(type_alias) = syn::parse::<ItemType>(stream) {
                    aliases.insert(
                        type_alias.ident.to_string(),
                        extract_path_from_type(type_alias.ty.as_ref()).unwrap_or_else(|| {
                            panic!(
                                "Only value types are supported. \
                                    Incompatible type in alias: {:?}",
                                type_alias
                            )
                        }),
                    );
                }

                current_item_tokens = Vec::new();
            }
            other => current_item_tokens.push(other),
        }
    }

    ParsedStatements {
        functions,
        type_paths,
        aliases,
    }
}

/// Generates bindings for the functions declared in the `fp_import!{}` and `fp_export!{}` blocks.
#[proc_macro]
pub fn fp_bindgen(args: TokenStream) -> TokenStream {
    let args: proc_macro2::TokenStream = args.into();
    let replacement = quote! {
        let (import_functions, import_types) = __fp_declare_import_fns();
        let (export_functions, mut export_types) = __fp_declare_export_fns();

        let mut types = import_types;
        types.append(&mut export_types);

        fp_bindgen::generate_bindings(
            import_functions,
            export_functions,
            types,
            #args
        );
    };
    replacement.into()
}

#[doc(hidden)]
#[proc_macro]
pub fn primitive_impls(_: TokenStream) -> TokenStream {
    let primitives = [
        Primitive::Bool,
        Primitive::F32,
        Primitive::F64,
        Primitive::I8,
        Primitive::I16,
        Primitive::I32,
        Primitive::I64,
        Primitive::U8,
        Primitive::U16,
        Primitive::U32,
        Primitive::U64,
    ];

    let mut token_stream = TokenStream::new();
    for primitive in primitives {
        token_stream.extend(primitive.gen_impl().into_iter());
    }
    token_stream
}

/// Exports a signature in a provider crate.
/// This is not meant to be used directly.
#[proc_macro_attribute]
#[proc_macro_error]
pub fn fp_export_signature(_attributes: TokenStream, input: TokenStream) -> TokenStream {
    proc_macro_error::set_dummy(input.clone().into());

    let func = syn::parse_macro_input::parse::<ForeignItemFn>(input.clone()).unwrap_or_abort();
    let args = typing::extract_args(&func.sig).collect::<Vec<_>>();

    let mut sig = func.sig.clone();
    //Massage the signature into what we wish to export
    {
        typing::morph_signature(&mut sig, "fp_bindgen_support");
        sig.inputs = sig
            .inputs
            .into_iter()
            //append a function ptr to the end which signature matches the original exported function
            .chain(once({
                let input_types = args.iter().map(|(_, pt, _)| pt.ty.as_ref());
                let output = if func.sig.asyncness.is_some() {
                    syn::parse::<ReturnType>((quote! {-> FUT}).into()).unwrap_or_abort()
                } else {
                    func.sig.output.clone()
                };

                syn::parse::<FnArg>((quote! {fptr: fn (#(#input_types),*) #output}).into())
                    .unwrap_or_abort()
            }))
            .collect();
        sig.generics.params.clear();
        if func.sig.asyncness.is_some() {
            let output = typing::get_output_type(&func.sig.output);
            sig.generics.params.push(
                syn::parse::<GenericParam>(
                    //the 'static life time is ok since we give it a box::pin
                    (quote! {FUT: std::future::Future<Output=#output> + 'static}).into(),
                )
                .unwrap_or_abort(),
            )
        }
    }

    let (complex_names, complex_types): (Vec<_>, Vec<_>) = args
        .iter()
        .filter_map(|&(_, pt, is_complex)| {
            if is_complex {
                Some((pt.pat.as_ref(), pt.ty.as_ref()))
            } else {
                None
            }
        })
        .unzip();

    let names = args.iter().map(|(_, pt, _)| pt.pat.as_ref());
    let func_call = quote! {(fptr)(#(#names),*)};

    let func_wrapper = if func.sig.asyncness.is_some() {
        quote! {
            let ret = fp_bindgen_support::guest::r#async::task::Task::alloc_and_spawn(#func_call);
        }
    } else {
        // Check the output type and replace complex ones with FatPtr
        let return_wrapper = if typing::is_ret_type_complex(&func.sig.output) {
            quote! {let ret = fp_bindgen_support::guest::io::export_value_to_host(&ret);}
        } else {
            Default::default()
        };
        quote! {
            let ret = #func_call;
            #return_wrapper
        }
    };

    //build the actual exported wrapper function
    (quote! {
        /// This is a implementation detail an should not be called directly
        #[inline(always)]
        pub #sig {
            #(let #complex_names = unsafe { fp_bindgen_support::guest::io::import_value_from_host::<#complex_types>(#complex_names) };)*
            #func_wrapper
            ret
        }
    })
    .into()
}

/// Exports an implementation of a specific provider function
///
/// Example usage of implementing a `log` function of a `logger` provider:
/// ```no_compile
/// use fp_bindgen_macros::fp_export_impl; //this would be `logger::fp_export_impl` inside the plugin crate
/// #[fp_export_impl(logger)]
/// pub fn log(msg: String, foo: String) -> String {
///     format!("{} + {} => {0}{1}", msg, foo)
/// }
/// ```
#[proc_macro_attribute]
#[proc_macro_error]
pub fn fp_export_impl(attributes: TokenStream, input: TokenStream) -> TokenStream {
    proc_macro_error::set_dummy(input.clone().into());

    let func = syn::parse_macro_input::parse::<ItemFn>(input.clone()).unwrap_or_abort();
    let attrs =
        syn::parse_macro_input::parse::<AttributeArgs>(attributes.clone()).unwrap_or_abort();

    let protocol_path = attrs
        .get(0)
        .map(|om| match om {
            syn::NestedMeta::Meta(meta) => match meta {
                syn::Meta::Path(path) => path,
                _ => abort!(meta, "unsupported attribute, must name a path"),
            },
            _ => abort!(om, "unsupported attribute, must name a path"),
        })
        .unwrap_or_else(|| abort!(func, "missing attribute. Must name which provider is being implemented eg: #[fp_export_impl(foobar)]"));

    let args = typing::extract_args(&func.sig).collect::<Vec<_>>();

    let mut sig = func.sig.clone();
    //Massage the signature into what we wish to export
    {
        typing::morph_signature(
            &mut sig,
            protocol_path.to_token_stream().to_string().as_str(),
        );
        sig.ident = format_ident!("__fp_gen_{}", sig.ident);
    }

    let fn_name = &func.sig.ident;

    let impl_fn_pat = Pat::Path(PatPath {
        attrs: vec![],
        qself: None,
        path: PathSegment {
            ident: func.sig.ident.clone(),
            arguments: PathArguments::None,
        }
        .into(),
    });
    let call_args = args
        .iter()
        .map(|&(_, pt, _)| pt.pat.as_ref())
        .chain(once(&impl_fn_pat))
        .collect::<Vec<_>>();

    let ts: proc_macro2::TokenStream = input.clone().into();
    //build the actual exported wrapper function
    (quote! {
        #[no_mangle]
        pub #sig {
            #protocol_path::#fn_name(#(#call_args),*)
        }
        #ts
    })
    .into()
}

/// Imports a signature in a provider crate.
/// This is not meant to be used directly.
#[proc_macro_attribute]
#[proc_macro_error]
pub fn fp_import_signature(_attributes: TokenStream, input: TokenStream) -> TokenStream {
    proc_macro_error::set_dummy(input.clone().into());

    let func = syn::parse_macro_input::parse::<ForeignItemFn>(input.clone()).unwrap_or_abort();
    let args = typing::extract_args(&func.sig).collect::<Vec<_>>();

    let wrapper_sig = func.sig.clone();
    let mut extern_sig = wrapper_sig.clone();
    //Massage the signature into what we wish to export
    {
        extern_sig.ident = format_ident!("__fp_gen_{}", extern_sig.ident);
        typing::morph_signature(&mut extern_sig, "fp_bindgen_support");
    }

    let complex_names: Vec<_> = args
        .iter()
        .filter_map(|&(_, pt, is_complex)| {
            if is_complex {
                Some(pt.pat.as_ref())
            } else {
                None
            }
        })
        .collect();

    let names = args.iter().map(|(_, pt, _)| pt.pat.as_ref());
    let extern_ident = &extern_sig.ident;
    let func_call = quote! {#extern_ident(#(#names),*)};

    let ret_wrapper = if func.sig.asyncness.is_some() {
        quote! {
            let ret = unsafe {
                fp_bindgen_support::guest::io::import_value_from_host(fp_bindgen_support::guest::r#async::HostFuture::new(ret).await)
            };
        }
    } else {
        // Check the output type and replace complex ones with FatPtr
        if typing::is_ret_type_complex(&func.sig.output) {
            quote! {
                let ret = unsafe { fp_bindgen_support::guest::io::import_value_from_host(ret) };
            }
        } else {
            Default::default()
        }
    };

    let attrs = &func.attrs;

    //build the actual imported wrapper function
    (quote! {
        #[link(wasm_import_module = "fp")]
        extern "C" { #extern_sig; }

        #[inline(always)]
        #(#attrs)*
        pub #wrapper_sig {
            #(let #complex_names = fp_bindgen_support::guest::io::export_value_to_host(&#complex_names);)*
            let ret = unsafe { #func_call };
            #ret_wrapper
            ret
        }
    })
    .into()
}