Skip to main content

datex_core/runtime/
mod.rs

1//! This module contains the implementation of the runtime, which is the backbone of the DATEX system.
2use crate::{
3    core_compiler::core_compilation_context::DXBWithSharedValues,
4    runtime::{
5        execution::{ExecutionError, context::RemoteExecutionTarget},
6        remote_value_sync::SubscriberError,
7    },
8    values::value_container::ValueContainer,
9};
10
11use crate::prelude::*;
12use core::{fmt::Debug, ops::Deref, result::Result};
13use execution::context::{ExecutionContext, ScriptExecutionError};
14pub mod cache;
15mod config;
16pub mod execution;
17mod incoming_sections;
18mod internal;
19mod logger;
20pub mod pointer_address_provider;
21pub mod pointer_availability_lookup;
22pub mod remote_value_sync;
23mod runner;
24#[cfg(test)]
25pub mod test_utils;
26
27use crate::{
28    core_compiler::instruction_input::InstructionInput,
29    runtime::execution::{
30        context::{ExecutionMode, FinalOrPersistedExecutionContext},
31        execution_input::ExecutionCallerMetadata,
32        remote_execution_options::RemoteExecutionOptions,
33    },
34    values::core_values::endpoint::Endpoint,
35};
36pub use config::*;
37pub use internal::*;
38pub use runner::*;
39
40#[derive(Clone, Debug)]
41pub struct Runtime {
42    internal: Rc<RuntimeInternal>,
43}
44
45impl Deref for Runtime {
46    type Target = RuntimeInternal;
47
48    fn deref(&self) -> &Self::Target {
49        &self.internal
50    }
51}
52
53/// publicly exposed wrapper impl for the Runtime
54/// around RuntimeInternal
55impl Runtime {
56    pub(crate) fn new(runtime_internal: RuntimeInternal) -> Runtime {
57        Runtime {
58            internal: Rc::new(runtime_internal),
59        }
60    }
61
62    pub fn register_endpoint_property(
63        &self,
64        name: &str,
65        value: ValueContainer,
66    ) {
67        self.internal.register_endpoint_property(name, value);
68    }
69
70    pub fn stub() -> Runtime {
71        Runtime::new(RuntimeInternal::stub())
72    }
73
74    fn internal(&self) -> Rc<RuntimeInternal> {
75        Rc::clone(&self.internal)
76    }
77
78    #[cfg(feature = "compiler")]
79    pub async fn execute<'a>(
80        &'a self,
81        script: &'a str,
82        inserted_values: &'a [ValueContainer],
83        execution_context: Option<
84            impl Into<FinalOrPersistedExecutionContext<'a>>,
85        >,
86        mark_as_debug: bool,
87    ) -> Result<Option<ValueContainer>, ScriptExecutionError> {
88        RuntimeInternal::execute(
89            self.internal(),
90            script,
91            inserted_values,
92            execution_context,
93            mark_as_debug,
94        )
95        .await
96    }
97
98    #[cfg(feature = "compiler")]
99    pub fn execute_sync<'a>(
100        &'a self,
101        script: &'a str,
102        inserted_values: &'a [ValueContainer],
103        execution_context: Option<
104            impl Into<FinalOrPersistedExecutionContext<'a>>,
105        >,
106    ) -> Result<Option<ValueContainer>, ScriptExecutionError> {
107        RuntimeInternal::execute_sync(
108            self.internal(),
109            script,
110            inserted_values,
111            execution_context,
112        )
113    }
114
115    pub async fn execute_dxb<'a>(
116        &'a self,
117        input: DXBWithSharedValues,
118        initial_stack_values: Option<Vec<ValueContainer>>,
119        execution_context: Option<
120            impl Into<FinalOrPersistedExecutionContext<'a>> + 'a,
121        >,
122        mark_as_debug: bool,
123    ) -> Result<Option<ValueContainer>, ExecutionError> {
124        RuntimeInternal::execute_dxb(
125            self.internal(),
126            input,
127            initial_stack_values,
128            execution_context,
129            mark_as_debug,
130        )
131        .await
132    }
133
134    pub fn execute_dxb_sync<'a>(
135        &'a self,
136        input: DXBWithSharedValues,
137        initial_stack_values: Option<Vec<ValueContainer>>,
138        execution_context: Option<
139            impl Into<FinalOrPersistedExecutionContext<'a>>,
140        >,
141    ) -> Result<Option<ValueContainer>, ExecutionError> {
142        RuntimeInternal::execute_dxb_sync(
143            self.internal(),
144            input,
145            initial_stack_values,
146            execution_context,
147        )
148    }
149
150    pub async fn execute_remote(
151        &self,
152        remote_execution_target: &RemoteExecutionTarget,
153        input: DXBWithSharedValues,
154        options: RemoteExecutionOptions,
155    ) -> Result<Vec<(RemoteExecutionResult, Endpoint)>, SubscriberError> {
156        RuntimeInternal::execute_remote(
157            self.internal(),
158            remote_execution_target,
159            input,
160            options,
161        )
162        .await
163    }
164
165    pub async fn execute_remote_single_receiver(
166        &self,
167        remote_execution_target: &RemoteExecutionTarget,
168        input: DXBWithSharedValues,
169        options: RemoteExecutionOptions,
170    ) -> RemoteExecutionResult {
171        RuntimeInternal::execute_remote_single_receiver(
172            self.internal(),
173            remote_execution_target,
174            input,
175            options,
176        )
177        .await
178    }
179
180    pub async fn execute_instructions_remote(
181        &self,
182        endpoints: Vec<Endpoint>,
183        instructions_input: Vec<InstructionInput>,
184    ) -> Result<Vec<(RemoteExecutionResult, Endpoint)>, SubscriberError> {
185        RuntimeInternal::execute_instructions_remote(
186            self.internal(),
187            endpoints,
188            instructions_input,
189            false,
190        )
191        .await
192    }
193
194    pub async fn execute_instructions_remote_single_receiver(
195        &self,
196        endpoint: Endpoint,
197        instructions_input: Vec<InstructionInput>,
198        mark_as_debug: bool,
199    ) -> RemoteExecutionResult {
200        RuntimeInternal::execute_instructions_remote_single_receiver(
201            self.internal(),
202            endpoint,
203            instructions_input,
204            mark_as_debug,
205        )
206        .await
207    }
208
209    pub(crate) fn get_or_create_execution_context<'a>(
210        self: &Runtime,
211        ctx: Option<impl Into<FinalOrPersistedExecutionContext<'a>>>,
212    ) -> FinalOrPersistedExecutionContext<'a> {
213        match ctx {
214            Some(context) => {
215                let mut context = context.into();
216                // set current runtime in execution context if local execution context
217                if let &mut ExecutionContext::Local(ref mut local_context) =
218                    context.as_mut()
219                {
220                    local_context.set_runtime_internal(self.clone());
221                }
222                context
223            }
224            None => FinalOrPersistedExecutionContext::Final(
225                ExecutionContext::local(
226                    ExecutionMode::Static,
227                    self.clone(),
228                    ExecutionCallerMetadata::local_default(),
229                ),
230            ),
231        }
232    }
233}