ad-astra-export 1.0.0

Embeddable scripting language platform Ad Astra. Macro Crate.
Documentation
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
////////////////////////////////////////////////////////////////////////////////
// This file is part of "Ad Astra", an embeddable scripting programming       //
// language platform.                                                         //
//                                                                            //
// This work is proprietary software with source-available code.              //
//                                                                            //
// To copy, use, distribute, or contribute to this work, you must agree to    //
// the terms of the General License Agreement:                                //
//                                                                            //
// https://github.com/Eliah-Lakhin/ad-astra/blob/master/EULA.md               //
//                                                                            //
// The agreement grants a Basic Commercial License, allowing you to use       //
// this work in non-commercial and limited commercial products with a total   //
// gross revenue cap. To remove this commercial limit for one of your         //
// products, you must acquire a Full Commercial License.                      //
//                                                                            //
// If you contribute to the source code, documentation, or related materials, //
// you must grant me an exclusive license to these contributions.             //
// Contributions are governed by the "Contributions" section of the General   //
// License Agreement.                                                         //
//                                                                            //
// Copying the work in parts is strictly forbidden, except as permitted       //
// under the General License Agreement.                                       //
//                                                                            //
// If you do not or cannot agree to the terms of this Agreement,              //
// do not use this work.                                                      //
//                                                                            //
// This work is provided "as is", without any warranties, express or implied, //
// except where such disclaimers are legally invalid.                         //
//                                                                            //
// Copyright (c) 2024 Ilya Lakhin (Илья Александрович Лахин).                 //
// All rights reserved.                                                       //
////////////////////////////////////////////////////////////////////////////////

use std::{
    any::TypeId,
    cell::RefCell,
    env::var,
    hash::{Hash, Hasher},
};

use ahash::{AHashMap, AHasher};
use convert_case::{Case, Casing};
use proc_macro2::{Delimiter, Ident, Spacing, Span, TokenStream, TokenTree};
use quote::{quote_spanned, ToTokens};
use syn::{spanned::Spanned, Item, LitStr, Type};

use crate::utils::{seed_hash_map, seed_hasher, Facade};

pub type OriginRef = Ident;
pub type IdRef = Ident;
pub type SectionName = Ident;
pub type FunctionName = Ident;
pub type TypeName = Ident;
pub type StaticName = Ident;

#[derive(Clone, Copy)]
pub struct Context;

impl ToTokens for Context {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        self.borrow(|inner| inner.to_tokens(tokens));
    }
}

impl Context {
    #[inline(always)]
    pub fn init(self, item: &Item) {
        INNER.with_borrow_mut(|inner| {
            if inner.is_some() {
                panic!("Internal error. Inner context already initialized.")
            }

            *inner = Some(ContextInner::new(item));
        })
    }

    #[inline(always)]
    pub fn release(self) {
        INNER.with_borrow_mut(|inner| {
            if inner.is_none() {
                panic!("Internal error. Inner context already released.")
            }

            *inner = None;
        })
    }

    #[inline(always)]
    pub fn name(self) -> String {
        self.borrow(move |inner| inner.name.clone())
    }

    #[inline(always)]
    pub fn span(self) -> Span {
        self.borrow(move |inner| inner.span())
    }

    #[inline(always)]
    pub fn primary_origin(self) -> OriginRef {
        self.borrow(move |inner| {
            inner
                .origins
                .get(0)
                .expect("Internal error. Missing primary origin.")
                .clone()
        })
    }

    #[inline(always)]
    pub fn make_origin(self, name: &str, span: Span) -> OriginRef {
        self.borrow(move |inner| {
            let reference = ContextInner::format_ident::<OriginTag>(
                &mut inner.hasher,
                "origin",
                Case::UpperSnake,
                name,
                span,
            );

            inner.origins.push(reference.clone());

            reference
        })
    }

    #[inline(always)]
    pub fn make_unique_identifier(self, name: &str, span: Span) -> IdRef {
        self.borrow(move |inner| {
            let reference = ContextInner::format_ident::<IdentifierTag>(
                &mut inner.hasher,
                "id",
                Case::UpperSnake,
                name,
                span,
            );

            inner
                .unique_identifiers
                .push((LitStr::new(name, span), reference.clone()));

            reference
        })
    }

    #[inline(always)]
    pub fn make_shared_identifier(self, name: &str, span: Span) -> IdRef {
        self.borrow(move |inner| {
            let string = LitStr::new(name, span);

            if let Some(reference) = inner.shared_identifiers.get(&string) {
                return reference.clone();
            }

            let reference = ContextInner::format_ident::<IdentifierTag>(
                &mut inner.hasher,
                "id",
                Case::UpperSnake,
                name,
                span,
            );

            let _ = inner.shared_identifiers.insert(string, reference.clone());

            reference
        })
    }

    #[inline(always)]
    pub fn make_section_name(self, name: &str, span: Span) -> SectionName {
        self.borrow(move |inner| {
            ContextInner::format_ident::<SectionTag>(
                &mut inner.hasher,
                "__adastra_export",
                Case::UpperSnake,
                name,
                span,
            )
        })
    }

    #[inline(always)]
    pub fn make_function_name(self, name: &str, span: Span) -> FunctionName {
        self.borrow(move |inner| {
            ContextInner::format_ident::<FunctionTag>(
                &mut inner.hasher,
                "fn",
                Case::Snake,
                name,
                span,
            )
        })
    }

    #[inline(always)]
    pub fn make_type_name(self, name: &str, span: Span) -> TypeName {
        self.borrow(move |inner| {
            ContextInner::format_ident::<TypeTag>(
                &mut inner.hasher,
                "ty",
                Case::UpperCamel,
                name,
                span,
            )
        })
    }

    #[inline(always)]
    pub fn make_static_name(self, name: &str, span: Span) -> StaticName {
        self.borrow(move |inner| {
            ContextInner::format_ident::<StaticTag>(
                &mut inner.hasher,
                "st",
                Case::UpperSnake,
                name,
                span,
            )
        })
    }

    #[inline(always)]
    fn borrow<R>(&self, f: impl FnOnce(&mut ContextInner) -> R) -> R {
        INNER.with_borrow_mut(|inner| {
            let Some(inner) = inner else {
                panic!("Internal error. Inner context is not initialized.");
            };

            f(inner)
        })
    }
}

thread_local! {
    static INNER: RefCell<Option<ContextInner>> = RefCell::new(None)
}

struct ContextInner {
    name: String,
    hasher: AHasher,
    origins: Vec<Ident>,
    unique_identifiers: Vec<(LitStr, Ident)>,
    shared_identifiers: AHashMap<LitStr, Ident>,
}

impl ToTokens for ContextInner {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        for ident in &self.origins {
            let span = ident.span();

            let core = span.face_core();
            let module_path = span.face_module_path();
            let line = span.face_line();
            let column = span.face_column();
            let panic = span.face_panic();
            let env = span.face_env();
            let option = span.face_option();

            quote_spanned!(span=> static #ident: #core::runtime::RustOrigin = {
                #[cold]
                #[track_caller]
                fn blame_fn(message: &str) {
                    #panic("{}", message);
                }

                #core::runtime::RustOrigin {
                    package: #option::<(&'static str, &'static str)>::Some(
                        (#env("CARGO_PKG_NAME"), #env("CARGO_PKG_VERSION")),
                    ),
                    code: #option::<#core::runtime::RustCode>::Some(
                        #core::runtime::RustCode {
                            module: #module_path(),
                            line: #line(),
                            column: #column(),
                            blame_fn,
                        },
                    ),
                }
            };)
            .to_tokens(tokens)
        }

        let identifiers = self
            .unique_identifiers
            .iter()
            .map(|(string, ident)| (string, ident))
            .chain(self.shared_identifiers.iter());

        for (string, ident) in identifiers {
            let span = ident.span();

            let core = span.face_core();
            let module_path = span.face_module_path();
            let line = span.face_line();
            let column = span.face_column();
            let panic = span.face_panic();
            let env = span.face_env();
            let option = span.face_option();

            quote_spanned!(span=> static #ident: #core::runtime::RustIdent = {
                #[cold]
                #[track_caller]
                fn blame_fn(message: &str) {
                    #panic("{}", message);
                }

                static ORIGIN: #core::runtime::RustOrigin = #core::runtime::RustOrigin {
                    package: #option::<(&'static str, &'static str)>::Some(
                        (#env("CARGO_PKG_NAME"), #env("CARGO_PKG_VERSION")),
                    ),
                    code: #option::<#core::runtime::RustCode>::Some(
                        #core::runtime::RustCode {
                            module: #module_path(),
                            line: #line(),
                            column: #column(),
                            blame_fn,
                        },
                    ),
                };

                #core::runtime::RustIdent {
                    origin: &ORIGIN,
                    string: #string,
                }
            };)
            .to_tokens(tokens)
        }
    }
}

impl ContextInner {
    fn new(item: &Item) -> Self {
        let mut hasher = seed_hasher();

        Self::feed_tag::<CargoNameTag>(&mut hasher);
        if let Ok(value) = var("CARGO_PKG_NAME") {
            Self::feed_string(&mut hasher, &value)
        }

        Self::feed_tag::<CargoVersionTag>(&mut hasher);
        if let Ok(value) = var("CARGO_PKG_VERSION") {
            Self::feed_string(&mut hasher, &value)
        }

        Self::feed_tag::<ItemTag>(&mut hasher);
        Self::feed_stream(&mut hasher, item.to_token_stream());

        let span;
        let name;

        match item {
            Item::Const(item) => {
                span = item.ident.span();
                name = format!("const {}", item.ident);
            }

            Item::Fn(item) => {
                span = item.sig.ident.span();
                name = format!("fn {}", item.sig.ident);
            }

            Item::Impl(item) => {
                span = item.self_ty.span();

                let this = match &item.self_ty.as_ref() {
                    Type::Path(ty) => match ty.path.get_ident() {
                        Some(ident) => Some(ident.to_string()),
                        None => None,
                    },
                    _ => None,
                };

                let this = this.unwrap_or_else(|| String::from("type"));

                name = match &item.trait_ {
                    Some((_, path, _)) => match path.get_ident() {
                        Some(ident) => {
                            format!("{ident} for {}", this)
                        }
                        None => format!("trait for {}", this),
                    },

                    None => this,
                };
            }

            Item::Static(item) => {
                span = item.ident.span();
                name = format!("static {}", item.ident);
            }

            Item::Struct(item) => {
                span = item.ident.span();
                name = format!("struct {}", item.ident);
            }

            Item::Trait(item) => {
                span = item.ident.span();
                name = format!("trait {}", item.ident);
            }

            Item::Type(item) => {
                span = item.ident.span();
                name = format!("type {}", item.ident);
            }

            _ => {
                span = Span::call_site();
                name = String::new();
            }
        };

        let primary_origin = Self::format_ident::<OriginTag>(
            &mut hasher,
            "origin",
            Case::UpperSnake,
            name.as_ref(),
            span,
        );

        Self {
            name,
            hasher,
            origins: vec![primary_origin],
            unique_identifiers: Vec::new(),
            shared_identifiers: seed_hash_map(),
        }
    }

    fn format_ident<T: 'static>(
        hasher: &mut impl Hasher,
        suffix: &'static str,
        case: Case,
        ident: &str,
        span: Span,
    ) -> Ident {
        Self::feed_tag::<T>(hasher);
        Self::feed_string(hasher, ident);

        let mut hash = format!("{:x}", hasher.finish());

        hash.truncate(16);

        let mut string = String::new();

        string.push_str(suffix);
        string.push('_');
        string.push_str(&hash);

        if let Case::UpperCamel | Case::UpperKebab | Case::UpperFlat | Case::UpperSnake = case {
            string = string.to_ascii_uppercase();
        }

        let mut first = true;
        for char in ident.to_case(case).chars() {
            if char.is_ascii_alphanumeric() || char == '_' {
                if first {
                    string.push('_');
                    first = false;
                }

                string.push(char);
                continue;
            }
        }

        Ident::new(&string, span)
    }

    #[inline(always)]
    fn feed_tag<T: 'static>(hasher: &mut impl Hasher) {
        TypeId::of::<T>().hash(hasher);
    }

    #[inline]
    fn feed_string(hasher: &mut impl Hasher, string: &str) {
        string.hash(hasher);
    }

    #[inline]
    fn feed_char(hasher: &mut impl Hasher, ch: &char) {
        ch.hash(hasher);
    }

    #[inline]
    #[allow(unused)]
    fn feed_int(hasher: &mut impl Hasher, int: usize) {
        int.hash(hasher);
    }

    #[inline]
    fn feed_stream(hasher: &mut impl Hasher, stream: TokenStream) {
        for tree in stream {
            Self::feed_token_tree(hasher, tree);
        }
    }

    fn feed_token_tree(hasher: &mut impl Hasher, tree: TokenTree) {
        match tree {
            TokenTree::Group(variant) => {
                match variant.delimiter() {
                    Delimiter::Parenthesis => Self::feed_tag::<ParenthesisTag>(hasher),
                    Delimiter::Brace => Self::feed_tag::<BraceTag>(hasher),
                    Delimiter::Bracket => Self::feed_tag::<BracketTag>(hasher),
                    Delimiter::None => Self::feed_tag::<GroupTag>(hasher),
                }

                Self::feed_stream(hasher, variant.stream());
            }

            TokenTree::Ident(variant) => {
                Self::feed_tag::<IdentTag>(hasher);
                Self::feed_string(hasher, variant.to_string().as_str());
            }

            TokenTree::Punct(variant) => {
                match variant.spacing() {
                    Spacing::Alone => Self::feed_tag::<AloneTag>(hasher),
                    Spacing::Joint => Self::feed_tag::<JointTag>(hasher),
                }

                Self::feed_char(hasher, &variant.as_char());
            }

            TokenTree::Literal(variant) => {
                Self::feed_tag::<LiteralTag>(hasher);
                Self::feed_string(hasher, variant.to_string().as_str());
            }
        }
    }
}

struct CargoNameTag;
struct CargoVersionTag;
struct ParenthesisTag;
struct BraceTag;
struct BracketTag;
struct GroupTag;
struct IdentTag;
struct AloneTag;
struct JointTag;
struct LiteralTag;
struct OriginTag;
struct IdentifierTag;
struct ItemTag;
struct SectionTag;
struct FunctionTag;
struct TypeTag;
struct StaticTag;