datex_core/runtime/execution/context/
local.rs

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