Skip to main content

datex_core/runtime/execution/context/
local.rs

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