File size: 2,650 Bytes
b98ffbb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;

extern crate proc_macro;

#[proc_macro]
pub fn register_operator(item: TokenStream) -> TokenStream {
    // convert from `TokenStream` to `TokenStream2`, which is used by the
    // `syn` crate
    let item = TokenStream2::from(item);
    // generate the dora wrapper functions
    let generated = register_operator_impl(&item).unwrap_or_else(|err| err.to_compile_error());
    // output the generated functions
    let tokens = quote! {
        #generated
    };
    // convert the type back from `TokenStream2` to `TokenStream`
    tokens.into()
}

/// Generates the wrapper functions for the annotated function.
fn register_operator_impl(item: &TokenStream2) -> syn::Result<TokenStream2> {
    // parse the type given to the `register_operator` macro
    let operator_ty: syn::TypePath = syn::parse2(item.clone())
        .map_err(|e| syn::Error::new(e.span(), "expected type as argument"))?;

    let init = quote! {
        #[no_mangle]
        pub unsafe extern "C" fn dora_init_operator() -> dora_operator_api::types::DoraInitResult {
            dora_operator_api::raw::dora_init_operator::<#operator_ty>()
        }

        const _DORA_INIT_OPERATOR: dora_operator_api::types::DoraInitOperator = dora_operator_api::types::DoraInitOperator {
            init_operator: dora_init_operator,
        };
    };

    let drop = quote! {
        #[no_mangle]
        pub unsafe extern "C" fn dora_drop_operator(operator_context: *mut std::ffi::c_void)
            -> dora_operator_api::types::DoraResult
        {
            dora_operator_api::raw::dora_drop_operator::<#operator_ty>(operator_context)
        }

        const _DORA_DROP_OPERATOR: dora_operator_api::types::DoraDropOperator = dora_operator_api::types::DoraDropOperator {
            drop_operator: dora_drop_operator,
        };
    };

    let on_event = quote! {
        #[no_mangle]
        pub unsafe extern "C" fn dora_on_event(
            event: &mut dora_operator_api::types::RawEvent,
            send_output: &dora_operator_api::types::SendOutput,
            operator_context: *mut std::ffi::c_void,
        ) -> dora_operator_api::types::OnEventResult {
            dora_operator_api::raw::dora_on_event::<#operator_ty>(
                event, send_output, operator_context
            )
        }

        const _DORA_ON_EVENT: dora_operator_api::types::DoraOnEvent = dora_operator_api::types::DoraOnEvent {
            on_event: dora_operator_api::types::OnEventFn(dora_on_event),
        };
    };

    Ok(quote! {
        #init
        #drop
        #on_event
    })
}