ad-astra 1.0.0

Embeddable scripting language platform Ad Astra. Main 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
////////////////////////////////////////////////////////////////////////////////
// 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::{
    cmp::Ordering,
    fmt::{Debug, Display, Formatter},
    hash::{Hash, Hasher},
    ops::Deref,
    sync::{RwLock, RwLockReadGuard},
};

use ahash::{AHashMap, AHashSet, RandomState};
use lady_deirdre::{
    arena::Id,
    sync::{Lazy, Table},
};
use semver::{Version, VersionReq};

use crate::{
    report::debug_unreachable,
    runtime::{
        Cell,
        RustOrigin,
        TypeMeta,
        __intrinsics::{DeclarationGroup, PackageDeclaration},
    },
};

/// A type that represents the Script Package of a crate.
///
/// This trait is automatically implemented on a struct type when you
/// export it as a crate package.
///
/// Through the [ScriptPackage::meta] function, you gain access to the
/// [PackageMeta] object. This can be used, for example, to instantiate new
/// [script modules](crate::analysis::ScriptModule) that can be analyzed in
/// accordance with the exported semantics of the crate or to run an LSP server.
///
/// ```
/// use ad_astra::{export, runtime::ScriptPackage};
///
/// #[export(package)]
/// #[derive(Default)]
/// struct Package;
///
/// assert_eq!(Package::meta().name(), "ad-astra");
/// ```
pub trait ScriptPackage {
    /// Returns a Rust source code location that points to where the package
    /// type was declared.
    ///
    /// This is a shortcut for [PackageMeta::origin].
    #[inline(always)]
    fn origin() -> &'static RustOrigin {
        Self::meta().origin()
    }

    /// Returns the name of the package's crate.
    ///
    /// This is a shortcut for [PackageMeta::name].
    #[inline(always)]
    fn name() -> &'static str {
        Self::meta().name()
    }

    /// Returns the version of the package's crate.
    ///
    /// This is a shortcut for [PackageMeta::version].
    #[inline(always)]
    fn version() -> &'static str {
        Self::meta().version()
    }

    /// Returns a reference to the full metadata object of the crate's package.
    fn meta() -> &'static PackageMeta;
}

/// Metadata for the [ScriptPackage].
///
/// You cannot instantiate this object manually; it is created automatically
/// by the Script Engine for each exported Script Package per crate. However,
/// you can obtain a static reference to the PackageMeta in several ways.
/// For instance, you can get it from the [ScriptPackage::meta] function of
/// the exported package struct. You can also manually find the reference
/// using the [PackageMeta::of] function.
///
/// ```
/// use ad_astra::{
///     export,
///     runtime::{PackageMeta, ScriptPackage},
/// };
///
/// #[export(package)]
/// #[derive(Default)]
/// struct Package;
///
/// let package_meta = Package::meta();
///
/// let same_package =
///     PackageMeta::of(package_meta.name(), &format!("={}", package_meta.version())).unwrap();
///
/// assert_eq!(package_meta, same_package);
/// ```
///
/// You can use this reference to instantiate
/// [script modules](crate::analysis::ScriptModule) or to run the LSP server.
///
/// The alternative [Debug] implementation for the package lists all script
/// modules currently associated with this Script Package. The alternative
/// [Display] implementation prints the canonical name of the package's crate:
/// `<package_name>@<package_version>`.
pub struct PackageMeta {
    origin: &'static RustOrigin,
    declaration: PackageDeclaration,
    modules: RwLock<AHashSet<Id>>,
}

impl PartialEq for PackageMeta {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        if self.declaration.name.ne(other.declaration.name) {
            return false;
        }

        if self.declaration.version.ne(other.declaration.version) {
            return false;
        }

        true
    }
}

impl Eq for PackageMeta {}

impl Hash for PackageMeta {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.declaration.name.hash(state);
        self.declaration.version.hash(state);
    }
}

impl Ord for PackageMeta {
    #[inline]
    fn cmp(&self, other: &Self) -> Ordering {
        match self.declaration.name.cmp(other.declaration.name) {
            Ordering::Equal => self.declaration.version.cmp(other.declaration.version),
            other => other,
        }
    }
}

impl PartialOrd for PackageMeta {
    #[inline(always)]
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Debug for PackageMeta {
    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
        let alternate = formatter.alternate();

        let mut debug_struct = formatter.debug_struct("PackageMeta");

        if alternate {
            debug_struct.field("origin", self.origin());
        }

        debug_struct
            .field("name", &self.name())
            .field("version", &self.version());

        if alternate {
            if let Ok(modules) = self.modules.try_read() {
                struct ListModules<'a> {
                    modules: RwLockReadGuard<'a, AHashSet<Id>>,
                }

                impl<'a> Debug for ListModules<'a> {
                    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
                        let mut list = formatter.debug_list();

                        for id in self.modules.iter() {
                            let name = id.name();

                            match name.is_empty() {
                                true => list.entry(&format_args!("‹#{}›", id.into_inner())),
                                false => list.entry(&format_args!("‹{}›", name)),
                            };
                        }

                        list.finish()
                    }
                }

                let print_modules = ListModules { modules };

                debug_struct.field("modules", &print_modules);
            }

            let prototype = self.declaration.instance.ty().prototype();

            debug_struct.field("prototype", prototype);
        }

        debug_struct.finish()
    }
}

impl Display for PackageMeta {
    #[inline]
    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
        formatter.write_str(self.name())?;

        if formatter.alternate() {
            formatter.write_fmt(format_args!("@{}", self.version()))?;
        }

        Ok(())
    }
}

impl PackageMeta {
    #[inline(always)]
    fn new(origin: &'static RustOrigin, declaration: PackageDeclaration) -> Self {
        Self {
            origin,
            declaration,
            modules: RwLock::new(AHashSet::new()),
        }
    }

    /// Looks up the `PackageMeta` by the package's crate `name` and `version`.
    ///
    /// The `name` should match the exact crate name specified in the crate's
    /// `Cargo.toml` configuration.
    ///
    /// The `version` specifies the crate version requirement. There can be
    /// multiple crates with the same name but different versions in the crate
    /// dependency graph. The format of the `version` string is the same as used
    /// in the `[dependencies]` section of `Cargo.toml`. For example, you can
    /// specify the version as `3.0` to match the latest minor version, or use
    /// the equality sign `=2.1.5` to match a specific version exactly.
    ///
    /// The function returns None if there are no crates with the specified
    /// name and version requirements or if the crates do not have an exported
    /// Script Package.
    pub fn of(name: &str, version: &str) -> Option<&'static Self> {
        let registry = PackageRegistry::get();

        let version_set = registry.index.get(name)?;

        let requirement = VersionReq::parse(version).ok()?;

        let mut candidate: Option<(&Version, &PackageMeta)> = None;

        for (version, variant) in version_set {
            if !requirement.matches(version) {
                continue;
            }

            match candidate {
                Some((previous, _)) if previous > version => (),
                _ => candidate = Some((version, variant)),
            }
        }

        let (_, meta) = candidate?;

        Some(meta)
    }

    #[inline(always)]
    pub(crate) fn by_id(id: Id) -> Option<&'static Self> {
        let registry = ModuleRegistry::get();

        Some(*registry.index.get(&id)?)
    }

    /// Returns the Rust source code location that points to where the
    /// package type was declared.
    #[inline(always)]
    pub fn origin(&self) -> &'static RustOrigin {
        self.origin
    }

    /// Returns the name of the crate for this package, as specified in the
    /// crate's `Cargo.toml`.
    #[inline(always)]
    pub fn name(&self) -> &'static str {
        self.declaration.name
    }

    /// Returns the version of the crate for this package, as specified in the
    /// crate's `Cargo.toml`.
    #[inline(always)]
    pub fn version(&self) -> &'static str {
        self.declaration.version
    }

    /// Returns the documentation URL of the crate for this package, as
    /// specified in the crate's `Cargo.toml`.
    #[inline(always)]
    pub fn doc(&self) -> Option<&'static str> {
        self.declaration.doc
    }

    /// Returns the type metadata of the Rust struct that has been exported
    /// as a [ScriptPackage].
    #[inline(always)]
    pub fn ty(&self) -> &'static TypeMeta {
        self.declaration.instance.deref().ty()
    }

    /// Returns a smart pointer to the instance of the Rust struct that
    /// represents the [ScriptPackage].
    ///
    /// The Script Engine automatically instantiates each package struct type
    /// during initialization, using the [Default] constructor of the Rust
    /// struct.
    ///
    /// Through this instance, you can access the exported fields and methods of
    /// the struct. The crate's exported global functions and statics are also
    /// available as [components](crate::runtime::Object::component) of this
    /// type. Additionally, dependency crates (that have exported ScriptPackage)
    /// become components of this instance.
    ///
    /// The script code can access this instance using the `crate` script
    /// keyword or by referencing dependent crates
    /// (`my_crate.dep_crate` or `crate.dep_crate`).
    #[inline(always)]
    pub fn instance(&self) -> Cell {
        self.declaration.instance.deref().clone()
    }

    // Safety: `id` is not registered anywhere.
    #[inline(always)]
    pub(crate) unsafe fn attach_module(&'static self, id: Id) {
        let mut modules = self
            .modules
            .write()
            .unwrap_or_else(|poison| poison.into_inner());

        if !modules.insert(id) {
            // Safety: Upheld by the caller.
            unsafe { debug_unreachable!("Duplicate module.") }
        }

        let registry = ModuleRegistry::get();

        if registry.index.insert(id, self).is_some() {
            // Safety: Upheld by the caller.
            unsafe { debug_unreachable!("Duplicate module.") }
        }
    }

    // Safety: `id` exists in this Package.
    #[inline(always)]
    pub(crate) unsafe fn detach_module(&'static self, id: Id) {
        let mut modules = self
            .modules
            .write()
            .unwrap_or_else(|poison| poison.into_inner());

        if !modules.remove(&id) {
            // Safety: Upheld by the caller.
            unsafe { debug_unreachable!("Missing module.") }
        }

        let registry = ModuleRegistry::get();

        if registry.index.remove(&id).is_none() {
            // Safety: Upheld by the caller.
            unsafe { debug_unreachable!("Missing module.") }
        }
    }
}

struct PackageRegistry {
    index: AHashMap<&'static str, AHashMap<Version, PackageMeta>>,
}

impl PackageRegistry {
    #[inline(always)]
    fn get() -> &'static Self {
        static REGISTRY: Lazy<PackageRegistry> = Lazy::new(|| {
            let mut index = AHashMap::<&'static str, AHashMap<Version, PackageMeta>>::new();

            for group in DeclarationGroup::enumerate() {
                let origin = group.origin;

                for declaration in &group.packages {
                    let declaration = declaration();

                    let version_set = index.entry(declaration.name).or_default();

                    let version = match Version::parse(declaration.version) {
                        Ok(version) => version,

                        Err(error) => {
                            let name = declaration.name;
                            let version = &declaration.version;

                            origin.blame(&format!(
                                "Package {name}@{version} version parse error. {error}",
                            ))
                        }
                    };

                    if let Some(previous) = version_set.get(&version) {
                        let name = declaration.name;
                        let version = &declaration.version;
                        let previous = previous.origin;

                        origin.blame(&format!(
                            "Package {name}@{version} already declared in {previous}.",
                        ))
                    }

                    let meta = PackageMeta::new(origin, declaration);

                    if let Some(_) = version_set.insert(version, meta) {
                        // Safety: Uniqueness checked above.
                        unsafe { debug_unreachable!("Duplicate package entry.") }
                    }
                }
            }

            PackageRegistry { index }
        });

        REGISTRY.deref()
    }
}

struct ModuleRegistry {
    index: Table<Id, &'static PackageMeta, RandomState>,
}

impl ModuleRegistry {
    fn get() -> &'static Self {
        static REGISTRY: Lazy<ModuleRegistry> = Lazy::new(|| ModuleRegistry {
            index: Table::new(),
        });

        &REGISTRY
    }
}