Skip to main content

datex_core/runtime/execution/context/
local.rs

1#[cfg(feature = "compiler")]
2use crate::compiler::scope::CompilationScope;
3use crate::runtime::{
4    RuntimeInternal,
5    execution::{
6        ExecutionOptions, context::ExecutionContext,
7        execution_loop::state::ExecutionLoopState,
8    },
9};
10
11use crate::prelude::*;
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
13pub enum ExecutionMode {
14    /// A single execution of a program that is completely known at compile time.
15    #[default]
16    Static,
17    /// An execution of a program that may be extended at runtime.
18    /// This mode is used for REPLs and dynamic remote executions with persistent contexts.
19    Unbounded { has_next: bool },
20}
21
22impl ExecutionMode {
23    pub fn unbounded() -> Self {
24        ExecutionMode::Unbounded { has_next: true }
25    }
26
27    pub fn is_unbounded(&self) -> bool {
28        matches!(self, ExecutionMode::Unbounded { .. })
29    }
30}
31
32#[derive(Debug)]
33pub struct LocalExecutionContext {
34    #[cfg(feature = "compiler")]
35    pub compile_scope: CompilationScope,
36    pub loop_state: Option<ExecutionLoopState>,
37    pub runtime: Rc<RuntimeInternal>,
38    pub execution_options: ExecutionOptions,
39    pub verbose: bool,
40    pub execution_mode: ExecutionMode,
41}
42
43impl LocalExecutionContext {
44    pub fn new(
45        execution_mode: ExecutionMode,
46        runtime: Rc<RuntimeInternal>,
47    ) -> Self {
48        LocalExecutionContext {
49            #[cfg(feature = "compiler")]
50            compile_scope: CompilationScope::new(execution_mode),
51            loop_state: None,
52            runtime,
53            execution_options: ExecutionOptions::default(),
54            verbose: false,
55            execution_mode,
56        }
57    }
58
59    /// Creates a new local execution context with the given compile scope.
60    pub fn debug(
61        execution_mode: ExecutionMode,
62        runtime: Rc<RuntimeInternal>,
63    ) -> Self {
64        LocalExecutionContext {
65            #[cfg(feature = "compiler")]
66            compile_scope: CompilationScope::new(execution_mode),
67            execution_options: ExecutionOptions { verbose: true },
68            loop_state: None,
69            runtime,
70            verbose: true,
71            execution_mode,
72        }
73    }
74
75    pub fn set_runtime_internal(
76        &mut self,
77        runtime_internal: Rc<RuntimeInternal>,
78    ) {
79        self.runtime = runtime_internal;
80    }
81}
82
83impl ExecutionContext {
84    pub fn local(
85        execution_mode: ExecutionMode,
86        runtime: Rc<RuntimeInternal>,
87    ) -> Self {
88        ExecutionContext::Local(LocalExecutionContext::new(
89            execution_mode,
90            runtime,
91        ))
92    }
93
94    /// Creates a new local static execution context (can only be used once).
95    pub fn local_static(runtime: Rc<RuntimeInternal>) -> Self {
96        ExecutionContext::Local(LocalExecutionContext::new(
97            ExecutionMode::Static,
98            runtime,
99        ))
100    }
101
102    /// Creates a new local execution context (can be used multiple times).
103    pub fn local_unbounded(runtime: Rc<RuntimeInternal>) -> Self {
104        ExecutionContext::Local(LocalExecutionContext::new(
105            ExecutionMode::Unbounded { has_next: true },
106            runtime,
107        ))
108    }
109
110    /// Creates a new local execution context with verbose mode enabled,
111    /// providing more log outputs for debugging purposes.
112    pub fn local_debug(
113        execution_mode: ExecutionMode,
114        runtime: Rc<RuntimeInternal>,
115    ) -> Self {
116        ExecutionContext::Local(LocalExecutionContext::debug(
117            execution_mode,
118            runtime,
119        ))
120    }
121}