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, Default)]
33pub struct LocalExecutionContext {
34    #[cfg(feature = "compiler")]
35    pub compile_scope: CompilationScope,
36    pub loop_state: Option<ExecutionLoopState>,
37    pub runtime: Option<Rc<RuntimeInternal>>,
38    pub execution_options: ExecutionOptions,
39    pub verbose: bool,
40    pub execution_mode: ExecutionMode,
41}
42
43impl LocalExecutionContext {
44    pub fn new(execution_mode: ExecutionMode) -> Self {
45        LocalExecutionContext {
46            #[cfg(feature = "compiler")]
47            compile_scope: CompilationScope::new(execution_mode),
48            loop_state: None,
49            runtime: None,
50            execution_options: ExecutionOptions::default(),
51            verbose: false,
52            execution_mode,
53        }
54    }
55
56    /// Creates a new local execution context with the given compile scope.
57    pub fn debug(execution_mode: ExecutionMode) -> Self {
58        LocalExecutionContext {
59            #[cfg(feature = "compiler")]
60            compile_scope: CompilationScope::new(execution_mode),
61            execution_options: ExecutionOptions { verbose: true },
62            verbose: true,
63            execution_mode,
64            ..Default::default()
65        }
66    }
67
68    pub fn debug_with_runtime_internal(
69        runtime_internal: Rc<RuntimeInternal>,
70        execution_mode: ExecutionMode,
71    ) -> Self {
72        LocalExecutionContext {
73            #[cfg(feature = "compiler")]
74            compile_scope: CompilationScope::new(execution_mode),
75            loop_state: None,
76            runtime: Some(runtime_internal),
77            execution_options: ExecutionOptions { verbose: true },
78            verbose: true,
79            execution_mode,
80        }
81    }
82
83    pub fn new_with_runtime_internal(
84        runtime_internal: Rc<RuntimeInternal>,
85        execution_mode: ExecutionMode,
86    ) -> Self {
87        LocalExecutionContext {
88            #[cfg(feature = "compiler")]
89            compile_scope: CompilationScope::new(execution_mode),
90            loop_state: None,
91            runtime: Some(runtime_internal),
92            execution_mode,
93            ..Default::default()
94        }
95    }
96
97    pub fn set_runtime_internal(
98        &mut self,
99        runtime_internal: Rc<RuntimeInternal>,
100    ) {
101        self.runtime = Some(runtime_internal);
102    }
103}
104
105impl ExecutionContext {
106    /// Creates a new local execution context (can only be used once).
107    pub fn local() -> Self {
108        ExecutionContext::Local(LocalExecutionContext::new(
109            ExecutionMode::Static,
110        ))
111    }
112
113    /// Creates a new local execution context (can be used multiple times).
114    pub fn local_unbounded() -> Self {
115        ExecutionContext::Local(LocalExecutionContext::new(
116            ExecutionMode::Unbounded { has_next: true },
117        ))
118    }
119
120    /// Creates a new local execution context with a runtime.
121    pub fn local_with_runtime_internal(
122        runtime_internal: Rc<RuntimeInternal>,
123        execution_mode: ExecutionMode,
124    ) -> Self {
125        ExecutionContext::Local(
126            LocalExecutionContext::new_with_runtime_internal(
127                runtime_internal,
128                execution_mode,
129            ),
130        )
131    }
132
133    /// Creates a new local execution context with verbose mode enabled,
134    /// providing more log outputs for debugging purposes.
135    pub fn local_debug(execution_mode: ExecutionMode) -> Self {
136        ExecutionContext::Local(LocalExecutionContext::debug(execution_mode))
137    }
138
139    /// Creates a new local execution context with verbose mode enabled and a runtime.
140    pub fn local_debug_with_runtime_internal(
141        runtime_internal: Rc<RuntimeInternal>,
142        execution_mode: ExecutionMode,
143    ) -> Self {
144        ExecutionContext::Local(
145            LocalExecutionContext::debug_with_runtime_internal(
146                runtime_internal,
147                execution_mode,
148            ),
149        )
150    }
151}