php_tokio_derive/
lib.rs

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
use anyhow::{bail, Result};
use proc_macro2::Span;
use proc_macro2::TokenStream;
use quote::TokenStreamExt;
use quote::{quote, ToTokens};
use syn::parse_quote;
use syn::FnArg;
use syn::GenericArgument;
use syn::ImplItemFn;
use syn::Pat;
use syn::PathArguments;
use syn::Type;
use syn::{parse_macro_input, ItemImpl};

#[proc_macro_attribute]
pub fn php_async_impl(
    _: proc_macro::TokenStream,
    input: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
    match parser(parse_macro_input!(input as ItemImpl)) {
        Ok(parsed) => parsed,
        Err(e) => syn::Error::new(Span::call_site(), e).to_compile_error(),
    }
    .into()
}

fn parser(input: ItemImpl) -> Result<TokenStream> {
    let ItemImpl { self_ty, items, .. } = input;

    if input.trait_.is_some() {
        bail!("This macro cannot be used on trait implementations.");
    }

    let tokens = items
        .into_iter()
        .map(|item| {
            Ok(match item {
                syn::ImplItem::Fn(method) => handle_method(method)?,
                item => item.to_token_stream(),
            })
        })
        .collect::<Result<Vec<_>>>()?;

    let output = quote! {
        #[::ext_php_rs::php_impl]
        impl #self_ty {
            #(#tokens)*
        }
    };

    Ok(output)
}

fn handle_method(input: ImplItemFn) -> Result<TokenStream> {
    let mut receiver = false;
    let mut receiver_mutable = false;
    let mut hack_tokens = quote! {};
    for arg in input.sig.inputs.iter() {
        match arg {
            FnArg::Receiver(r) => {
                receiver = true;
                receiver_mutable = r.mutability.is_some();
            }
            FnArg::Typed(ty) => {
                let mut this = false;
                for attr in ty.attrs.iter() {
                    if attr.path().to_token_stream().to_string() == "this" {
                        this = true;
                    }
                }

                if !this {
                    let param = match &*ty.pat {
                        Pat::Ident(pat) => &pat.ident,
                        _ => bail!("Invalid parameter type."),
                    };

                    let mut ty_inner = &*ty.ty;
                    let mut is_option = false;

                    if let Type::Path(t) = ty_inner {
                        if t.path.segments[0].ident.to_string() == "Option" {
                            if let PathArguments::AngleBracketed(t) = &t.path.segments[0].arguments
                            {
                                if let GenericArgument::Type(t) = &t.args[0] {
                                    ty_inner = t;
                                    is_option = true;
                                }
                            }
                        }
                    }
                    let mut is_str = false;
                    if let Type::Reference(t) = ty_inner {
                        if t.mutability.is_none() {
                            if let Type::Path(t) = &*t.elem {
                                is_str = t.path.is_ident("str");
                            }
                        }
                        hack_tokens.append_all(if is_str {
                            if is_option {
                                quote! { let #param = #param.and_then(|__temp| Some(unsafe { ::core::mem::transmute::<&str, &'static str>(__temp) })); }
                            } else {
                                quote! { let #param = unsafe { ::core::mem::transmute::<&str, &'static str>(#param) }; }
                            }
                        } else {
                            if is_option {
                                quote! { let #param = #param.and_then(|__temp| Some(unsafe { ::php_tokio::borrow_unchecked::borrow_unchecked(__temp) })); }
                            } else {
                                quote! { let #param = unsafe { ::php_tokio::borrow_unchecked::borrow_unchecked(#param) }; }
                            }
                        });
                    }
                }
            }
        }
    }

    let mut input = input.clone();
    if input.sig.asyncness.is_some() {
        input.sig.asyncness = None;
        let stmts = input.block;
        let this = if receiver {
            if receiver_mutable {
                quote! { let this = unsafe { std::mem::transmute::<&mut Self, &'static mut Self>(self) }; }
            } else {
                quote! { let this = unsafe { std::mem::transmute::<&Self, &'static Self>(self) }; }
            }
        } else {
            quote! {}
        };
        input.block = parse_quote! {{
            #this
            #hack_tokens

            ::php_tokio::EventLoop::suspend_on(async move #stmts)
        }};
    }

    let result = quote! {
        #input
    };
    Ok(result)
}