aboutsummaryrefslogtreecommitdiff
path: root/macros/src/codegen/init.rs
blob: 465a927d797594cca37301f0cf8263110839690e (plain) (blame)
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
use proc_macro2::TokenStream as TokenStream2;
use quote::{format_ident, quote};
use rtic_syntax::{ast::App, Context};

use crate::{
    analyze::Analysis,
    check::Extra,
    codegen::{locals, module, resources_struct, util},
};

/// Generates support code for `#[init]` functions
pub fn codegen(
    app: &App,
    analysis: &Analysis,
    extra: &Extra,
) -> (
    // mod_app_idle -- the `${init}Resources` constructor
    Option<TokenStream2>,
    // root_init -- items that must be placed in the root of the crate:
    // - the `${init}Locals` struct
    // - the `${init}Resources` struct
    // - the `${init}LateResources` struct
    // - the `${init}` module, which contains types like `${init}::Context`
    Vec<TokenStream2>,
    // user_init -- the `#[init]` function written by the user
    Option<TokenStream2>,
    // user_init_imports -- the imports for `#[init]` functio written by the user
    Vec<TokenStream2>,
    // call_init -- the call to the user `#[init]` if there's one
    Option<TokenStream2>,
) {
    if app.inits.len() > 0 {
        let init = &app.inits.first().unwrap();
        let mut needs_lt = false;
        let name = &init.name;

        let mut root_init = vec![];

        let late_fields = analysis
            .late_resources
            .iter()
            .flat_map(|resources| {
                resources.iter().map(|name| {
                    let ty = &app.late_resources[name].ty;
                    let cfgs = &app.late_resources[name].cfgs;

                    quote!(
                        #(#cfgs)*
                        pub #name: #ty
                    )
                })
            })
            .collect::<Vec<_>>();

        let mut user_init_imports = vec![];
        let late_resources = util::late_resources_ident(&name);

        root_init.push(quote!(
            /// Resources initialized at runtime
            #[allow(non_snake_case)]
            pub struct #late_resources {
                #(#late_fields),*
            }
        ));

        let name_late = format_ident!("{}LateResources", name);
        user_init_imports.push(quote!(
                #[allow(non_snake_case)]
                use super::#name_late;
        ));

        let mut locals_pat = None;
        let mut locals_new = None;
        if !init.locals.is_empty() {
            let (struct_, pat) = locals::codegen(Context::Init, &init.locals, app);

            locals_new = Some(quote!(#name::Locals::new()));
            locals_pat = Some(pat);
            root_init.push(struct_);
        }

        let context = &init.context;
        let attrs = &init.attrs;
        let stmts = &init.stmts;
        let locals_pat = locals_pat.iter();
        let user_init = Some(quote!(
            #(#attrs)*
            #[allow(non_snake_case)]
            fn #name(#(#locals_pat,)* #context: #name::Context) -> #name::LateResources {
                #(#stmts)*
            }
        ));
        user_init_imports.push(quote!(
                #(#attrs)*
                #[allow(non_snake_case)]
                use super::#name;
        ));

        let mut mod_app = None;
        if !init.args.resources.is_empty() {
            let (item, constructor) =
                resources_struct::codegen(Context::Init, 0, &mut needs_lt, app, analysis);

            root_init.push(item);
            mod_app = Some(constructor);

            let name_late = format_ident!("{}Resources", name);
            user_init_imports.push(quote!(
                    #[allow(non_snake_case)]
                    use super::#name_late;
            ));
        }

        let locals_new = locals_new.iter();
        let call_init = Some(
            quote!(let late = crate::#name(#(#locals_new,)* #name::Context::new(core.into()));),
        );

        root_init.push(module::codegen(
            Context::Init,
            needs_lt,
            app,
            analysis,
            extra,
        ));

        (mod_app, root_init, user_init, user_init_imports, call_init)
    } else {
        (None, vec![], None, vec![], None)
    }
}
dd algolia and cleanup docs readmeGravatar Fred K. Schott 17-597/+157 2021-08-26WIP update examples/docs/Gravatar Okiki 45-1115/+2090 2021-08-26[ci] yarn formatGravatar FredKSchott 1-1/+1 2021-08-26add "astro preview" command (#1226)Gravatar Fred K. Schott 35-44/+240 2021-08-26remove unpublished blog post from docsGravatar Fred K. Schott 1-241/+0 2021-08-26Remove VSCode and Langauge Server from this monorepo (#1230)Gravatar Matthew Phillips 55-5004/+0 2021-08-26Version Packages (#1228)astro@0.20.0@astrojs/markdown-support@0.3.0Gravatar github-actions[bot] 27-38/+45 2021-08-26[ci] yarn formatGravatar FredKSchott 1-1/+1 2021-08-25added bengali translation of the getting started page (#1215)Gravatar Rafid Muhymin Wafi 3-0/+68 2021-08-25updated comment 'threw' to 'through' (#1235)Gravatar Mark Howard 1-1/+1 2021-08-25Fix path to CSS file in www site (#1233)Gravatar Matthew Phillips 1-1/+1 2021-08-25NL docs typo fixes (#1232)Gravatar semvis123 1-1/+1 2021-08-25Update getting-started.md (#1231)Gravatar Anneke Sinnema 1-6/+6 2021-08-25[ci] yarn formatGravatar FredKSchott 2-142/+145 2021-08-25stop building, bundling, and transforming public/ files (#1210)Gravatar Fred K. Schott 28-191/+246 2021-08-25[ci] yarn formatGravatar matthewp 2-5/+5 2021-08-25Arabic getting-started translation (#1166)Gravatar حمد بنقالي 3-0/+71 2021-08-25Fix typo Mardown on line 219 (#1229)Gravatar E. Berke KARAGÖZ 1-1/+1 2021-08-25docs: fix header rtl logo view (#1224)Gravatar Fred K. Schott 1-0/+1 2021-08-25Upgrade unified deps and improve unified plugins types (#1200)Gravatar Robin Métral 12-428/+755 2021-08-24Fix CSS in docs example (#1221)Gravatar Marcus Otterström 1-2/+3 2021-08-24Version Packages (#1217)astro@0.19.4Gravatar github-actions[bot] 24-31/+28 2021-08-24[ci] yarn formatGravatar matthewp 1-1/+3 2021-08-24Fix linter errors and warnings (#1218)Gravatar Mihkel Eidast 12-26/+33 2021-08-24Fix resolution of Astro.resolve in nested components (#1213)Gravatar Matthew Phillips 5-5/+14 2021-08-24Lazy load the youtube embed to boost homepage loading times (#1205)Gravatar Caleb Jasik 4-4/+138 2021-08-24[ci] yarn formatGravatar FredKSchott 2-7/+7 2021-08-23[i18n][Docs] Add Korean Translation of Getting Started page (#1189)Gravatar Joohoon Cha 4-1/+70 2021-08-23remove ignored lint ruleGravatar Fred K. Schott 1-1/+1 2021-08-23update depsGravatar Fred K. Schott 1-317/+275 2021-08-23Version Packages (#1206)astro@0.19.3Gravatar github-actions[bot] 25-45/+38 2021-08-23[ci] yarn formatGravatar FredKSchott 1-4/+5 2021-08-23fix docs on config apiGravatar Fred K. Schott 1-7/+11 2021-08-23[ci] yarn formatGravatar FredKSchott 6-11/+10 2021-08-23Add zod schema validation (#1198)Gravatar Fred K. Schott 32-361/+378 2021-08-23[ci] yarn formatGravatar matthewp 1-2/+1 2021-08-23Add Astro `<Debug/>` component (#675)Gravatar Caleb Jasik 7-0/+350 2021-08-23Add a `titleClosure` to the `HeadSEO.astro` component (#1140)Gravatar Caleb Jasik 3-6/+69 2021-08-23Version Packages (#1181)astro@0.19.2Gravatar github-actions[bot] 27-46/+31 2021-08-23fix issue with multiple getStaticPaths calls during build (#1194)Gravatar Fred K. Schott 12-70/+116 2021-08-23Add trailingSlash & pageDirectoryUrl config options (#1197)Gravatar Fred K. Schott 17-92/+284 2021-08-23[ci] yarn formatGravatar matthewp 1-1/+0 2021-08-23fix the rtl search bar view (#1177)Gravatar Fred K. Schott 3-16/+14 2021-08-20update universal idGravatar Fred K. Schott 4-13/+10 2021-08-20Update getting-started.md (#1182)Gravatar headapplesgithub 1-1/+1 2021-08-20CodeSandbox-izing via template override (#1167)Gravatar Sarah Rainsberger 2-0/+11