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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
////////////////////////////////////////////////////////////////////////////////
// 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::cell::RefCell;

use ahash::{AHashMap, AHashSet};
use convert_case::{Case, Casing};
use proc_macro2::{Ident, Span, TokenStream};
use quote::{quote_spanned, ToTokens};
use syn::{spanned::Spanned, Type};

use crate::utils::{
    seed_hash_map,
    seed_hash_set,
    ty::is_str_type,
    Coercion,
    Context,
    Facade,
    Operator,
    TypeUtils,
};

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

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

impl Shallow {
    #[inline(always)]
    pub fn init(self, shallow_mode: bool) {
        INNER.with_borrow_mut(move |inner| {
            if inner.is_some() {
                panic!("Internal error. Inner shallow interface already initialized.");
            }

            *inner = Some({
                let mut shallow_inner = ShallowInner::default();

                shallow_inner.shallow_mode = shallow_mode;

                shallow_inner
            });
        })
    }

    #[inline(always)]
    pub fn release(self, enforce: bool) {
        INNER.with_borrow_mut(|inner| {
            if enforce && inner.is_none() {
                panic!("Internal error. Inner shallow interface already released.");
            }

            *inner = None;
        })
    }

    #[inline(always)]
    pub fn enabled(self) -> bool {
        self.borrow(|inner| inner.shallow_mode)
    }

    pub fn assert_type_impls_downcast(self, ty: &Type, span: Span) {
        if !Shallow.enabled() {
            return;
        }

        self.borrow(|inner| {
            let _ = inner.assert_type_impls_downcast.insert(ty.clone(), span);
        });
    }

    pub fn assert_type_impls_upcast(self, ty: &Type, span: Span) {
        if !Shallow.enabled() {
            return;
        }

        self.borrow(|inner| {
            let _ = inner.assert_type_impls_upcast.insert(ty.clone(), span);
        });
    }

    pub fn assert_ref_type_impls_static_upcast(self, ty: &Type, span: Span) {
        if !Shallow.enabled() {
            return;
        }

        self.borrow(|inner| {
            let _ = inner
                .assert_ref_type_impls_static_upcast
                .insert(ty.clone(), span);
        });
    }

    pub fn assert_type_impls_script_type(self, ty: &Type, span: Span) {
        if !Shallow.enabled() {
            return;
        }

        self.borrow(|inner| {
            if inner.impl_registered_type.contains(ty) {
                return;
            }

            let _ = inner.assert_type_impls_script_type.insert(ty.clone(), span);
        });
    }

    pub fn assert_type_meets_op_requirements(self, ty: &Type, operator: Operator, span: Span) {
        if !Shallow.enabled() {
            return;
        }

        let key = (operator, ty.clone());

        if self.borrow(|inner| inner.impl_operator.contains_key(&key)) {
            return;
        }

        if let Operator::Debug | Operator::Display | Operator::Hash = operator {
            if !is_str_type(ty) {
                self.assert_type_impls_script_type(ty, span);
            }
        }

        self.borrow(|inner| {
            let _ = inner.assert_type_meets_op_requirements.insert(key, span);
        });
    }

    pub fn impl_registered_type(self, ty: &Type) {
        if !Shallow.enabled() {
            return;
        }

        self.borrow(|inner| {
            let _ = inner.assert_type_impls_script_type.remove(ty);
            let _ = inner.impl_registered_type.insert(ty.clone());
        });
    }

    pub fn impl_coercion(self, ty: &Type, coercion: Coercion) {
        if !Shallow.enabled() {
            return;
        }

        self.assert_type_impls_script_type(ty, ty.span());

        self.borrow(|inner| match inner.impl_coercion.get_mut(ty) {
            Some(previous) => previous.enrich(coercion),
            None => {
                let _ = inner.impl_coercion.insert(ty.clone(), coercion);
            }
        });
    }

    pub fn impl_operator(self, lhs: &Type, rhs: Option<&Type>, operator: Operator, span: Span) {
        if !Shallow.enabled() {
            return;
        }

        self.assert_type_impls_script_type(lhs, span);

        if let Some(rhs) = rhs {
            self.assert_type_impls_script_type(rhs, span);
        }

        self.borrow(|inner| {
            let _ = inner
                .impl_operator
                .insert((operator, lhs.clone()), (rhs.cloned(), span));

            let _ = inner
                .assert_type_meets_op_requirements
                .remove(&(operator, lhs.clone()));
        });
    }

    pub fn impl_package(self, ty: &Type, span: Span) {
        if !Shallow.enabled() {
            return;
        }

        self.borrow(|inner| {
            let _ = inner.impl_package.insert(ty.clone(), span);
        });
    }

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

            f(inner)
        })
    }
}

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

struct ShallowInner {
    shallow_mode: bool,
    assert_type_impls_downcast: AHashMap<Type, Span>,
    assert_type_impls_upcast: AHashMap<Type, Span>,
    assert_ref_type_impls_static_upcast: AHashMap<Type, Span>,
    assert_type_impls_script_type: AHashMap<Type, Span>,
    assert_type_meets_op_requirements: AHashMap<(Operator, Type), Span>,
    impl_registered_type: AHashSet<Type>,
    impl_coercion: AHashMap<Type, Coercion>,
    impl_operator: AHashMap<(Operator, Type), (Option<Type>, Span)>,
    impl_package: AHashMap<Type, Span>,
}

impl Default for ShallowInner {
    fn default() -> Self {
        Self {
            shallow_mode: false,
            assert_type_impls_downcast: seed_hash_map(),
            assert_type_impls_upcast: seed_hash_map(),
            assert_ref_type_impls_static_upcast: seed_hash_map(),
            assert_type_impls_script_type: seed_hash_map(),
            assert_type_meets_op_requirements: seed_hash_map(),
            impl_registered_type: seed_hash_set(),
            impl_coercion: seed_hash_map(),
            impl_operator: seed_hash_map(),
            impl_package: seed_hash_map(),
        }
    }
}

impl ToTokens for ShallowInner {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        let span = Context.span();
        let name = Context.name();

        let section = Context.make_section_name(name.as_str(), span);

        let mut body = TokenStream::new();

        for (ty, span) in &self.assert_type_impls_downcast {
            let core = span.face_core();

            quote_spanned!(*span=> let _ = <#ty as #core::runtime::Downcast>::downcast;)
                .to_tokens(&mut body);
        }

        for (ty, span) in &self.assert_type_impls_upcast {
            let core = span.face_core();

            quote_spanned!(*span=> let _ = <#ty as #core::runtime::Upcast>::upcast;)
                .to_tokens(&mut body);
        }

        for (ty, span) in &self.assert_ref_type_impls_static_upcast {
            let core = span.face_core();

            quote_spanned!(*span=> let _ = <&#ty as #core::runtime::Upcast<'static>>::upcast;)
                .to_tokens(&mut body);
        }

        for (ty, span) in &self.assert_type_impls_script_type {
            let core = span.face_core();

            quote_spanned!(*span=> let _ = <#ty as #core::runtime::ScriptType>::type_meta;)
                .to_tokens(&mut body);
        }

        for ((operator, lhs), span) in &self.assert_type_meets_op_requirements {
            Self::assert_operator(&mut body, operator, lhs, *span);
        }

        for ty in &self.impl_registered_type {
            ty.impl_registered_type().to_tokens(&mut body);
        }

        for (ty, coercion) in &self.impl_coercion {
            ty.impl_shallow_coercion(*coercion).to_tokens(&mut body);
        }

        for ((operator, lhs), (rhs, span)) in &self.impl_operator {
            Self::impl_operator(&mut body, operator, lhs, rhs, *span);
        }

        for (ty, span) in &self.impl_package {
            let core = span.face_core();
            let type_name = span.face_type_name();
            let panic = span.face_panic();

            quote_spanned!(*span=>
                #[allow(non_local_definitions)]
                impl #core::runtime::ScriptPackage for #ty {
                    fn meta() -> &'static #core::runtime::PackageMeta {
                        let name = #type_name::<Self>();
                        #panic("{name} type was not registered. Probably because export has been \
                        disabled for this type.");
                    }
                }
            )
            .to_tokens(&mut body);
        }

        if body.is_empty() {
            return;
        }

        quote_spanned!(span=> static #section: () = {
            #body
        };)
        .to_tokens(tokens);
    }
}

impl ShallowInner {
    fn assert_operator(body: &mut TokenStream, operator: &Operator, lhs: &Type, span: Span) {
        if let Operator::Clone = operator {
            let intrinsics = span.face_intrinsics();

            quote_spanned!(span=> let _ = #intrinsics::CloneFn::from_clone::<#lhs>;)
                .to_tokens(body);

            return;
        };

        let core = span.face_core();

        let description = operator.describe();

        let name = operator.to_string();
        let script_operator_ident = Ident::new(&format!("Script{}", name), span);

        if description.rhs {
            quote_spanned!(span=> let _ = <
                <#lhs as #core::runtime::ops::#script_operator_ident>::RHS
                as #core::runtime::ScriptType
            >::type_meta;)
            .to_tokens(body);
        }

        if description.result {
            quote_spanned!(span=> let _ = <
                <#lhs as #core::runtime::ops::#script_operator_ident>::Result
                as #core::runtime::ScriptType
            >::type_meta;)
            .to_tokens(body);
        }
    }

    fn impl_operator(
        body: &mut TokenStream,
        operator: &Operator,
        lhs: &Type,
        rhs: &Option<Type>,
        span: Span,
    ) {
        let result_type: Option<TokenStream>;

        let core = span.face_core();
        let type_name = span.face_type_name();
        let panic = span.face_panic();

        match operator {
            Operator::Field
            | Operator::Invocation
            | Operator::Binding
            | Operator::And
            | Operator::Or
            | Operator::None => return,

            Operator::Assign => {
                quote_spanned!(span=>
                    #[allow(non_local_definitions)]
                    impl #core::runtime::ops::ScriptAssign for #lhs {
                        type RHS = #lhs;

                        fn script_assign(
                            _origin: #core::runtime::Origin,
                            _lhs: #core::runtime::Arg,
                            _rhs: #core::runtime::Arg,
                        ) -> #core::runtime::RuntimeResult<()> {
                            let name = #type_name::<Self>();
                            #panic("{name} type was not registered. Probably because export has been \
                            disabled for this type.");
                        }
                    }
                )
                .to_tokens(body);

                return;
            }

            Operator::Concat => {
                quote_spanned!(span=>
                    #[allow(non_local_definitions)]
                    impl #core::runtime::ops::ScriptConcat for #lhs {
                        type Result = #lhs;

                        fn script_concat(
                            _origin: #core::runtime::Origin,
                            _items: &mut [#core::runtime::Arg],
                        ) -> #core::runtime::RuntimeResult<#core::runtime::Cell> {
                            let name = #type_name::<Self>();
                            #panic("{name} type was not registered. Probably because export has been \
                            disabled for this type.");
                        }
                    }
                )
                .to_tokens(body);

                return;
            }

            Operator::Clone => {
                quote_spanned!(span=>
                    #[allow(non_local_definitions)]
                    impl #core::runtime::ops::ScriptClone for #lhs {}
                )
                .to_tokens(body);
                return;
            }

            Operator::Debug => {
                quote_spanned!(span=>
                    #[allow(non_local_definitions)]
                    impl #core::runtime::ops::ScriptDebug for #lhs {}
                )
                .to_tokens(body);
                return;
            }

            Operator::Display => {
                quote_spanned!(span=>
                    #[allow(non_local_definitions)]
                    impl #core::runtime::ops::ScriptDisplay for #lhs {}
                )
                .to_tokens(body);
                return;
            }

            Operator::PartialEq => {
                let rhs = rhs.as_ref().unwrap_or(lhs);

                quote_spanned!(span=>
                    #[allow(non_local_definitions)]
                    impl #core::runtime::ops::ScriptPartialEq for #lhs {
                        type RHS = #rhs;

                        fn script_eq(
                            _origin: #core::runtime::Origin,
                            _lhs: #core::runtime::Arg,
                            _rhs: #core::runtime::Arg,
                        ) -> #core::runtime::RuntimeResult<bool> {
                            let name = #type_name::<Self>();
                            #panic("{name} type was not registered. Probably because export has been \
                            disabled for this type.");
                        }
                    }
                )
                .to_tokens(body);

                return;
            }

            Operator::Default => {
                quote_spanned!(span=>
                    #[allow(non_local_definitions)]
                    impl #core::runtime::ops::ScriptDefault for #lhs {
                        fn script_default(
                            origin: #core::runtime::Origin,
                        ) -> #core::runtime::RuntimeResult<#core::runtime::Cell> {
                            let name = #type_name::<Self>();
                            #panic("{name} type was not registered. Probably because export has been \
                            disabled for this type.");
                        }
                    }
                )
                .to_tokens(body);
                return;
            }

            Operator::PartialOrd => {
                let rhs = rhs.as_ref().unwrap_or(lhs);

                let option = span.face_option();
                let ordering = span.face_ordering();

                quote_spanned!(span=>
                    #[allow(non_local_definitions)]
                    impl #core::runtime::ops::ScriptPartialOrd for #lhs {
                        type RHS = #rhs;

                        fn script_partial_cmp(
                            _origin: #core::runtime::Origin,
                            _lhs: #core::runtime::Arg,
                            _rhs: #core::runtime::Arg,
                        ) -> #core::runtime::RuntimeResult<#option<#ordering>> {
                            let name = #type_name::<Self>();
                            #panic("{name} type was not registered. Probably because export has been \
                            disabled for this type.");
                        }
                    }
                )
                .to_tokens(body);

                return;
            }

            Operator::Ord => {
                let ordering = span.face_ordering();

                quote_spanned!(span=>
                    #[allow(non_local_definitions)]
                    impl #core::runtime::ops::ScriptOrd for #lhs {
                        fn script_cmp(
                            _origin: #core::runtime::Origin,
                            _lhs: #core::runtime::Arg,
                            _rhs: #core::runtime::Arg,
                        ) -> #core::runtime::RuntimeResult<#ordering> {
                            let name = #type_name::<Self>();
                            #panic("{name} type was not registered. Probably because export has been \
                            disabled for this type.");
                        }
                    }
                )
                .to_tokens(body);

                return;
            }

            Operator::Hash => {
                quote_spanned!(span=>
                    #[allow(non_local_definitions)]
                    impl #core::runtime::ops::ScriptHash for #lhs {}
                )
                .to_tokens(body);
                return;
            }

            Operator::Not => {
                let not = span.face_not();

                quote_spanned!(span=>
                    #[allow(non_local_definitions)]
                    impl #core::runtime::ops::ScriptNot for #lhs {
                        type Result = <#lhs as #not>::Output;

                        fn script_not(
                            _origin: #core::runtime::Origin,
                            _lhs: #core::runtime::Arg,
                        ) -> #core::runtime::RuntimeResult<#core::runtime::Cell> {
                            let name = #type_name::<Self>();
                            #panic("{name} type was not registered. Probably because export has been \
                            disabled for this type.");
                        }
                    }
                )
                .to_tokens(body);

                return;
            }

            Operator::Neg => {
                let neg = span.face_neg();

                quote_spanned!(span=>
                    #[allow(non_local_definitions)]
                    impl #core::runtime::ops::ScriptNeg for #lhs {
                        type Result = <#lhs as #neg>::Output;

                        fn script_neg(
                            _origin: #core::runtime::Origin,
                            _lhs: #core::runtime::Arg,
                        ) -> #core::runtime::RuntimeResult<#core::runtime::Cell> {
                            let name = #type_name::<Self>();
                            #panic("{name} type was not registered. Probably because export has been \
                            disabled for this type.");
                        }
                    }
                )
                .to_tokens(body);

                return;
            }

            Operator::Add => {
                result_type = Some(span.face_add());
            }

            Operator::Sub => {
                result_type = Some(span.face_sub());
            }

            Operator::Mul => {
                result_type = Some(span.face_mul());
            }

            Operator::Div => {
                result_type = Some(span.face_div());
            }

            Operator::BitAnd => {
                result_type = Some(span.face_bit_and());
            }

            Operator::BitOr => {
                result_type = Some(span.face_bit_or());
            }

            Operator::BitXor => {
                result_type = Some(span.face_bit_xor());
            }

            Operator::Shl => {
                result_type = Some(span.face_shl());
            }

            Operator::Shr => {
                result_type = Some(span.face_shr());
            }

            Operator::Rem => {
                result_type = Some(span.face_rem());
            }

            Operator::AddAssign
            | Operator::SubAssign
            | Operator::MulAssign
            | Operator::DivAssign
            | Operator::BitAndAssign
            | Operator::BitOrAssign
            | Operator::BitXorAssign
            | Operator::ShlAssign
            | Operator::ShrAssign
            | Operator::RemAssign => {
                result_type = None;
            }
        }

        let rhs = rhs.as_ref().unwrap_or(lhs);

        let operator_name = operator.to_string();
        let script_operator = Ident::new(&format!("Script{operator_name}"), span);
        let script_function = Ident::new(
            &format!("script_{}", operator_name.to_case(Case::Snake)),
            span,
        );

        let output;
        let assertion;

        match result_type {
            None => {
                output = None;
                assertion = None;
            }

            Some(ty) => {
                output = Some(quote_spanned!(span=> type Result = <#lhs as #ty::<#rhs>>::Output;));
                assertion = Some(quote_spanned!(span=>
                    let _ = <Self::Output as #core::runtime::Upcast<'static>>::upcast;
                ));
            }
        };

        quote_spanned!(span=>
            #[allow(non_local_definitions)]
            impl #core::runtime::ops::#script_operator for #lhs {
                type RHS = #rhs;
                #output

                fn #script_function(
                    _origin: #core::runtime::Origin,
                    _lhs: #core::runtime::Arg,
                    _rhs: #core::runtime::Arg,
                ) -> #core::runtime::RuntimeResult<#core::runtime::Cell> {
                    #assertion

                    let name = #type_name::<Self>();
                    #panic("{name} type was not registered. Probably because export has been \
                    disabled for this type.");
                }
            }
        )
        .to_tokens(body)
    }
}