rune 0.12.0

An embeddable dynamic programming language for Rust.
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
//! Compiler metadata for Rune.

use crate::collections::HashSet;
use crate::compile::{Item, Location, Visibility};
use crate::parse::Id;
use crate::runtime::ConstValue;
use crate::Hash;
use std::fmt;
use std::path::Path;
use std::sync::Arc;

/// Provides an owned human-readable description of a meta item.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct Meta {
    /// The item being described.
    pub item: Item,
    /// The kind of the item.
    pub kind: MetaKind,
}

/// Provides a human-readable description of a meta item. This is cheaper to use
/// than [Meta] because it avoids having to clone some data.
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub struct MetaRef<'a> {
    /// The item being described.
    pub item: &'a Item,
    /// The kind of the item.
    pub kind: MetaKind,
    /// The source of the meta.
    pub source: Option<&'a SourceMeta>,
}

/// Describes the kind of a [Meta] or [MetaRef].
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub enum MetaKind {
    /// An unknown type.
    Unknown,
    /// Item describes a unit structure.
    UnitStruct,
    /// Item describes a tuple structure.
    TupleStruct,
    /// Item describes a regular structure.
    Struct,
    /// Item describes a unit variant.
    UnitVariant,
    /// Item describes a tuple variant.
    TupleVariant,
    /// Item describes a struct variant.
    StructVariant,
    /// Item describes an enum.
    Enum,
    /// Item describes a function.
    Function {
        /// The type hash of the function.
        type_hash: Hash,
        /// If the function is a test.
        is_test: bool,
        /// If the function is a benchmark.
        is_bench: bool,
    },
    /// Item describes a closure.
    Closure,
    /// Item describes an async block.
    AsyncBlock,
    /// Item describes a constant.
    Const,
    /// Item describes a constant function.
    ConstFn,
    /// Item describes an import.
    Import,
}

impl fmt::Display for Meta {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.kind {
            MetaKind::Unknown => {
                write!(fmt, "unknown {}", self.item)?;
            }
            MetaKind::UnitStruct => {
                write!(fmt, "struct {}", self.item)?;
            }
            MetaKind::TupleStruct => {
                write!(fmt, "struct {}", self.item)?;
            }
            MetaKind::Struct => {
                write!(fmt, "struct {}", self.item)?;
            }
            MetaKind::UnitVariant => {
                write!(fmt, "unit variant {}", self.item)?;
            }
            MetaKind::TupleVariant => {
                write!(fmt, "variant {}", self.item)?;
            }
            MetaKind::StructVariant => {
                write!(fmt, "variant {}", self.item)?;
            }
            MetaKind::Enum => {
                write!(fmt, "enum {}", self.item)?;
            }
            MetaKind::Function { .. } => {
                write!(fmt, "fn {}", self.item)?;
            }
            MetaKind::Closure => {
                write!(fmt, "closure {}", self.item)?;
            }
            MetaKind::AsyncBlock => {
                write!(fmt, "async block {}", self.item)?;
            }
            MetaKind::Const => {
                write!(fmt, "const {}", self.item)?;
            }
            MetaKind::ConstFn => {
                write!(fmt, "const fn {}", self.item)?;
            }
            MetaKind::Import => {
                write!(fmt, "import {}", self.item)?;
            }
        }

        Ok(())
    }
}

/// Information on a compile sourc.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct SourceMeta {
    /// The location of the compile source.
    pub location: Location,
    /// The optional path where the meta is declared.
    pub path: Option<Box<Path>>,
}

/// Metadata about a variable captured by a clsoreu.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub(crate) struct CaptureMeta {
    /// Identity of the captured variable.
    pub(crate) ident: Box<str>,
}

/// Metadata about a compiled unit.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub(crate) struct PrivMeta {
    /// The item of the returned compile meta.
    pub(crate) item: Arc<ItemMeta>,
    /// The kind of the compile meta.
    pub(crate) kind: PrivMetaKind,
    /// The source of the meta.
    pub(crate) source: Option<SourceMeta>,
}

impl PrivMeta {
    /// Get the [Meta] which describes this [PrivMeta] object.
    pub(crate) fn info(&self) -> Meta {
        Meta {
            item: self.item.item.clone(),
            kind: self.kind.as_meta_info_kind(),
        }
    }

    /// Get the [MetaRef] which describes this [PrivMeta] object.
    pub(crate) fn info_ref(&self) -> MetaRef<'_> {
        MetaRef {
            item: &self.item.item,
            kind: self.kind.as_meta_info_kind(),
            source: self.source.as_ref(),
        }
    }

    /// Get the type hash of the base type (the one to type check for) for the
    /// given compile meta.
    ///
    /// Note: Variants cannot be used for type checking, you should instead
    /// compare them against the enum type.
    pub(crate) fn type_hash_of(&self) -> Option<Hash> {
        match &self.kind {
            PrivMetaKind::Unknown { type_hash, .. } => Some(*type_hash),
            PrivMetaKind::Struct { type_hash, .. } => Some(*type_hash),
            PrivMetaKind::Enum { type_hash, .. } => Some(*type_hash),
            PrivMetaKind::Function { type_hash, .. } => Some(*type_hash),
            PrivMetaKind::Closure { type_hash, .. } => Some(*type_hash),
            PrivMetaKind::AsyncBlock { type_hash, .. } => Some(*type_hash),
            PrivMetaKind::Variant { .. } => None,
            PrivMetaKind::Const { .. } => None,
            PrivMetaKind::ConstFn { .. } => None,
            PrivMetaKind::Import { .. } => None,
        }
    }
}

/// Private variant metadata.
#[derive(Debug, Clone)]
pub(crate) enum PrivVariantMeta {
    Tuple(PrivTupleMeta),
    Struct(PrivStructMeta),
    Unit,
}

/// Compile-time metadata kind about a unit.
#[derive(Debug, Clone)]
pub(crate) enum PrivMetaKind {
    /// The type is completely opaque. We have no idea about what it is with the
    /// exception of it having a type hash.
    Unknown { type_hash: Hash },
    /// Metadata about a struct.
    Struct {
        /// The type hash associated with this meta kind.
        type_hash: Hash,
        /// Variant metadata.
        variant: PrivVariantMeta,
    },
    /// Metadata about an empty variant.
    Variant {
        /// The type hash associated with this meta kind.
        type_hash: Hash,
        /// The item of the enum.
        enum_item: Item,
        /// Type hash of the enum this unit variant belongs to.
        enum_hash: Hash,
        /// The index of the variant.
        index: usize,
        /// Variant metadata.
        variant: PrivVariantMeta,
    },
    /// An enum item.
    Enum {
        /// The type hash associated with this meta kind.
        type_hash: Hash,
    },
    /// A function declaration.
    Function {
        /// The type hash associated with this meta kind.
        type_hash: Hash,

        /// Whether this function has a `#[test]` annotation
        is_test: bool,

        /// Whether this function has a `#[bench]` annotation.
        is_bench: bool,
    },
    /// A closure.
    Closure {
        /// The type hash associated with this meta kind.
        type_hash: Hash,
        /// Sequence of captured variables.
        captures: Arc<[CaptureMeta]>,
        /// If the closure moves its environment.
        do_move: bool,
    },
    /// An async block.
    AsyncBlock {
        /// The span where the async block is declared.
        type_hash: Hash,
        /// Sequence of captured variables.
        captures: Arc<[CaptureMeta]>,
        /// If the async block moves its environment.
        do_move: bool,
    },
    /// The constant expression.
    Const {
        /// The evaluated constant value.
        const_value: ConstValue,
    },
    /// A constant function.
    ConstFn {
        /// Opaque identifier for the constant function.
        id: Id,
    },
    /// Purely an import.
    Import {
        /// The module of the target.
        module: Arc<ModMeta>,
        /// The location of the import.
        location: Location,
        /// The imported target.
        target: Item,
    },
}

impl PrivMetaKind {
    /// Coerce into a [MetaKind].
    pub(crate) fn as_meta_info_kind(&self) -> MetaKind {
        match self {
            PrivMetaKind::Unknown { .. } => MetaKind::Unknown,
            PrivMetaKind::Struct {
                variant: PrivVariantMeta::Unit,
                ..
            } => MetaKind::UnitStruct,
            PrivMetaKind::Struct {
                variant: PrivVariantMeta::Tuple(..),
                ..
            } => MetaKind::TupleStruct,
            PrivMetaKind::Struct {
                variant: PrivVariantMeta::Struct(..),
                ..
            } => MetaKind::Struct,
            PrivMetaKind::Variant {
                variant: PrivVariantMeta::Unit,
                ..
            } => MetaKind::UnitVariant,
            PrivMetaKind::Variant {
                variant: PrivVariantMeta::Tuple(..),
                ..
            } => MetaKind::TupleVariant,
            PrivMetaKind::Variant {
                variant: PrivVariantMeta::Struct(..),
                ..
            } => MetaKind::StructVariant,
            PrivMetaKind::Enum { .. } => MetaKind::Enum,
            PrivMetaKind::Function {
                type_hash,
                is_bench,
                is_test,
                ..
            } => MetaKind::Function {
                type_hash: *type_hash,
                is_bench: *is_bench,
                is_test: *is_test,
            },
            PrivMetaKind::Closure { .. } => MetaKind::Closure,
            PrivMetaKind::AsyncBlock { .. } => MetaKind::AsyncBlock,
            PrivMetaKind::Const { .. } => MetaKind::Const,
            PrivMetaKind::ConstFn { .. } => MetaKind::ConstFn,
            PrivMetaKind::Import { .. } => MetaKind::Import,
        }
    }
}

/// The metadata about a struct.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub(crate) struct PrivStructMeta {
    /// Fields associated with the type.
    pub(crate) fields: HashSet<Box<str>>,
}

/// The metadata about a tuple.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub(crate) struct PrivTupleMeta {
    /// The number of arguments the variant takes.
    pub(crate) args: usize,
    /// Hash of the constructor function.
    pub(crate) hash: Hash,
}

/// Item and the module that the item belongs to.
#[derive(Default, Debug, Clone)]
#[non_exhaustive]
pub(crate) struct ItemMeta {
    /// The id of the item.
    pub(crate) id: Id,
    /// The location of the item.
    pub(crate) location: Location,
    /// The name of the item.
    pub(crate) item: Item,
    /// The visibility of the item.
    pub(crate) visibility: Visibility,
    /// The module associated with the item.
    pub(crate) module: Arc<ModMeta>,
}

impl ItemMeta {
    /// Test if the item is public (and should be exported).
    pub(crate) fn is_public(&self) -> bool {
        self.visibility.is_public() && self.module.is_public()
    }
}

impl From<Item> for ItemMeta {
    fn from(item: Item) -> Self {
        Self {
            id: Default::default(),
            location: Default::default(),
            item,
            visibility: Default::default(),
            module: Default::default(),
        }
    }
}

/// Module, its item and its visibility.
#[derive(Default, Debug)]
#[non_exhaustive]
pub(crate) struct ModMeta {
    /// The location of the module.
    pub(crate) location: Location,
    /// The item of the module.
    pub(crate) item: Item,
    /// The visibility of the module.
    pub(crate) visibility: Visibility,
    /// The kind of the module.
    pub(crate) parent: Option<Arc<ModMeta>>,
}

impl ModMeta {
    /// Test if the module recursively is public.
    pub(crate) fn is_public(&self) -> bool {
        let mut current = Some(self);

        while let Some(m) = current.take() {
            if !m.visibility.is_public() {
                return false;
            }

            current = m.parent.as_deref();
        }

        true
    }
}