Skip to main content

mq_lang/engine/
compiled_program.rs

1//! [`CompiledProgram`]: a query compiled once and evaluated many times.
2#[cfg(not(feature = "debugger"))]
3use crate::SharedCell;
4#[cfg(any(not(feature = "debugger"), feature = "mqc"))]
5use crate::{Shared, tarn};
6
7/// A compiled mq program bundled with its original source, returned by `Engine::compile`.
8#[derive(Debug, Clone)]
9pub struct CompiledProgram {
10    pub(crate) source: String,
11    pub(crate) body: ProgramBody,
12}
13
14#[derive(Debug, Clone)]
15pub(crate) enum ProgramBody {
16    /// Compiled with debugger instrumentation on each run.
17    #[cfg(feature = "debugger")]
18    Ast(crate::ast::Program),
19    /// Bytecode is built on the first run and reused.
20    #[cfg(not(feature = "debugger"))]
21    Cached {
22        program: crate::ast::Program,
23        cache: VmCache,
24    },
25    /// Loaded from a `.mqc` file.
26    #[cfg(feature = "mqc")]
27    Precompiled(Shared<tarn::split_program::SplitProgram>),
28}
29
30/// Bytecode built from a [`ProgramBody::Cached`] program, shared by its clones.
31#[cfg(not(feature = "debugger"))]
32#[derive(Debug, Clone, Default)]
33pub(crate) struct VmCache(Shared<SharedCell<Option<Shared<tarn::CachedProgram>>>>);
34
35#[cfg(not(feature = "debugger"))]
36impl VmCache {
37    pub(crate) fn get(&self) -> Option<Shared<tarn::CachedProgram>> {
38        #[cfg(feature = "sync")]
39        {
40            self.0.read().unwrap().clone()
41        }
42        #[cfg(not(feature = "sync"))]
43        {
44            self.0.borrow().clone()
45        }
46    }
47
48    pub(crate) fn set(&self, program: Shared<tarn::CachedProgram>) {
49        #[cfg(feature = "sync")]
50        {
51            *self.0.write().unwrap() = Some(program);
52        }
53        #[cfg(not(feature = "sync"))]
54        {
55            *self.0.borrow_mut() = Some(program);
56        }
57    }
58}
59
60impl CompiledProgram {
61    /// Wraps `program` so its bytecode is cached across runs.
62    pub(super) fn cached(source: String, program: crate::ast::Program) -> Self {
63        #[cfg(not(feature = "debugger"))]
64        let body = ProgramBody::Cached {
65            program,
66            cache: VmCache::default(),
67        };
68        #[cfg(feature = "debugger")]
69        let body = ProgramBody::Ast(program);
70        Self { source, body }
71    }
72
73    /// Returns the original source code.
74    pub fn source(&self) -> &str {
75        &self.source
76    }
77
78    /// Returns the underlying AST nodes, or `None` for a program loaded from a `.mqc` file.
79    pub fn program(&self) -> Option<&crate::ast::Program> {
80        match &self.body {
81            #[cfg(feature = "debugger")]
82            ProgramBody::Ast(program) => Some(program),
83            #[cfg(not(feature = "debugger"))]
84            ProgramBody::Cached { program, .. } => Some(program),
85            #[cfg(feature = "mqc")]
86            ProgramBody::Precompiled(_) => None,
87        }
88    }
89
90    #[cfg(feature = "mqc")]
91    pub(crate) fn from_precompiled(source: String, program: tarn::split_program::SplitProgram) -> Self {
92        Self {
93            source,
94            body: ProgramBody::Precompiled(Shared::new(program)),
95        }
96    }
97
98    #[cfg(all(test, not(feature = "debugger")))]
99    pub(crate) fn vm_cache(&self) -> Option<&VmCache> {
100        match &self.body {
101            ProgramBody::Cached { cache, .. } => Some(cache),
102            #[allow(unreachable_patterns)]
103            _ => None,
104        }
105    }
106}
107
108impl From<crate::ast::Program> for CompiledProgram {
109    /// Wraps a raw `Program` (e.g. from `ast_from_json`) with no source context.
110    fn from(program: crate::ast::Program) -> Self {
111        Self::cached(String::new(), program)
112    }
113}