Skip to main content

datex_core/runtime/
mod.rs

1use crate::{
2    network::com_hub::ComHub,
3    runtime::execution::ExecutionError,
4    values::{
5        core_values::endpoint::Endpoint, value_container::ValueContainer,
6    },
7};
8
9use crate::prelude::*;
10use core::{cell::RefCell, fmt::Debug, result::Result};
11use execution::context::{
12    ExecutionContext, RemoteExecutionContext, ScriptExecutionError,
13};
14
15mod config;
16pub mod dif_interface;
17pub mod execution;
18mod incoming_sections;
19mod internal;
20pub mod memory;
21mod runner;
22
23#[cfg(test)]
24pub mod test_utils;
25
26use self::memory::Memory;
27pub use config::*;
28pub use internal::*;
29pub use runner::*;
30
31const VERSION: &str = env!("CARGO_PKG_VERSION");
32
33#[derive(Clone)]
34pub struct Runtime {
35    pub version: String,
36    pub internal: Rc<RuntimeInternal>,
37}
38
39impl Debug for Runtime {
40    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
41        f.debug_struct("Runtime")
42            .field("version", &self.version)
43            .finish()
44    }
45}
46
47/// publicly exposed wrapper impl for the Runtime
48/// around RuntimeInternal
49impl Runtime {
50    pub(crate) fn new(runtime_internal: RuntimeInternal) -> Runtime {
51        Runtime {
52            version: VERSION.to_string(),
53            internal: Rc::new(runtime_internal),
54        }
55    }
56
57    pub fn stub() -> Runtime {
58        Runtime::new(RuntimeInternal::stub())
59    }
60
61    pub fn com_hub(&self) -> Rc<ComHub> {
62        self.internal.com_hub.clone()
63    }
64    pub fn endpoint(&self) -> Endpoint {
65        self.internal.endpoint.clone()
66    }
67
68    pub fn internal(&self) -> Rc<RuntimeInternal> {
69        Rc::clone(&self.internal)
70    }
71
72    pub fn memory(&self) -> &RefCell<Memory> {
73        &self.internal.memory
74    }
75
76    #[cfg(feature = "compiler")]
77    pub async fn execute(
78        &self,
79        script: &str,
80        inserted_values: &[ValueContainer],
81        execution_context: Option<&mut ExecutionContext>,
82    ) -> Result<Option<ValueContainer>, ScriptExecutionError> {
83        RuntimeInternal::execute(
84            self.internal(),
85            script,
86            inserted_values,
87            execution_context,
88        )
89        .await
90    }
91
92    #[cfg(feature = "compiler")]
93    pub fn execute_sync(
94        &self,
95        script: &str,
96        inserted_values: &[ValueContainer],
97        execution_context: Option<&mut ExecutionContext>,
98    ) -> Result<Option<ValueContainer>, ScriptExecutionError> {
99        RuntimeInternal::execute_sync(
100            self.internal(),
101            script,
102            inserted_values,
103            execution_context,
104        )
105    }
106
107    pub async fn execute_dxb<'a>(
108        &'a self,
109        dxb: Vec<u8>,
110        execution_context: Option<&'a mut ExecutionContext>,
111        end_execution: bool,
112    ) -> Result<Option<ValueContainer>, ExecutionError> {
113        RuntimeInternal::execute_dxb(
114            self.internal(),
115            dxb,
116            execution_context,
117            end_execution,
118        )
119        .await
120    }
121
122    pub fn execute_dxb_sync(
123        &self,
124        dxb: &[u8],
125        execution_context: Option<&mut ExecutionContext>,
126        end_execution: bool,
127    ) -> Result<Option<ValueContainer>, ExecutionError> {
128        RuntimeInternal::execute_dxb_sync(
129            self.internal(),
130            dxb,
131            execution_context,
132            end_execution,
133        )
134    }
135
136    async fn execute_remote(
137        &self,
138        remote_execution_context: &mut RemoteExecutionContext,
139        dxb: Vec<u8>,
140    ) -> Result<Option<ValueContainer>, ExecutionError> {
141        RuntimeInternal::execute_remote(
142            self.internal(),
143            remote_execution_context,
144            dxb,
145        )
146        .await
147    }
148}