Skip to main content

mirascript_vm/
compile.rs

1use std::{
2    num::NonZeroU64,
3    rc::Rc,
4    sync::atomic::{AtomicU64, Ordering},
5};
6
7use crate::bytecode::Program;
8
9static NEXT_SCRIPT_ID: AtomicU64 = AtomicU64::new(1);
10
11/// Stable identity shared by clones of one compiled script.
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
13pub struct ScriptId(NonZeroU64);
14
15impl ScriptId {
16    fn new() -> Self {
17        let id = NEXT_SCRIPT_ID.fetch_add(1, Ordering::Relaxed);
18        Self(NonZeroU64::new(id).expect("MiraScript identifier space exhausted"))
19    }
20}
21
22/// A validated, reusable MiraScript program.
23#[derive(Clone, Debug)]
24pub struct MiraScript {
25    id: ScriptId,
26    program: Rc<Program>,
27}
28
29impl MiraScript {
30    /// Return the stable identity shared by clones of this script.
31    pub fn id(&self) -> ScriptId {
32        self.id
33    }
34
35    pub(crate) fn program_ref(&self) -> &Program {
36        &self.program
37    }
38
39    pub(crate) fn program(&self) -> Rc<Program> {
40        Rc::clone(&self.program)
41    }
42
43    pub(crate) fn new(program: Program) -> Self {
44        Self {
45            id: ScriptId::new(),
46            program: Rc::new(program),
47        }
48    }
49}