Skip to main content

datex_core/runtime/execution/
execution_input.rs

1use crate::{
2    core_compiler::core_compilation_context::DXBWithSharedValues,
3    prelude::*,
4    runtime::{
5        Runtime,
6        execution::{
7            ExecutionError,
8            execution_loop::{
9                interrupts::{ExternalExecutionInterrupt, InterruptProvider},
10                state::{ExecutionLoopState, RuntimeExecutionStack},
11            },
12        },
13    },
14    shared_values::SharedContainer,
15};
16
17use crate::values::core_values::endpoint::Endpoint;
18
19#[derive(Debug, Clone, Default)]
20pub struct ExecutionOptions {
21    pub verbose: bool,
22}
23
24#[derive(Debug, Clone)]
25pub struct ExecutionCallerMetadata {
26    pub endpoint: Endpoint,
27}
28
29impl ExecutionCallerMetadata {
30    pub fn local_default() -> Self {
31        Self {
32            endpoint: Endpoint::LOCAL,
33        }
34    }
35    pub fn new(endpoint: Endpoint) -> Self {
36        Self { endpoint }
37    }
38}
39
40/// Input required to execute a DXB program.
41#[derive(Debug)]
42pub struct ExecutionInput {
43    /// Options for execution.
44    pub options: ExecutionOptions,
45    /// Metadata about the caller of the execution
46    pub caller_metadata: ExecutionCallerMetadata,
47    /// The DXB program body containing raw bytecode.
48    pub dxb: Vec<u8>,
49    pub shared_values: Vec<SharedContainer>,
50    /// For persisting execution state across multiple executions (e.g., for REPL scenarios).
51    pub loop_state: Option<ExecutionLoopState>,
52    pub runtime: Runtime,
53}
54
55impl ExecutionInput {
56    pub fn new(
57        dxb_with_shared_values: DXBWithSharedValues,
58        caller_metadata: ExecutionCallerMetadata,
59        options: ExecutionOptions,
60        runtime: Runtime,
61    ) -> Self {
62        Self {
63            options,
64            caller_metadata,
65            dxb: dxb_with_shared_values.dxb,
66            shared_values: dxb_with_shared_values.shared_values,
67            loop_state: None,
68            runtime,
69        }
70    }
71
72    pub fn new_with_loop_state(
73        dxb_with_shared_values: DXBWithSharedValues,
74        caller_metadata: ExecutionCallerMetadata,
75        options: ExecutionOptions,
76        runtime: Runtime,
77        loop_state: Option<ExecutionLoopState>,
78    ) -> Self {
79        Self {
80            options,
81            caller_metadata,
82            dxb: dxb_with_shared_values.dxb,
83            shared_values: dxb_with_shared_values.shared_values,
84            loop_state,
85            runtime,
86        }
87    }
88
89    pub fn new_with_stack(
90        dxb_with_shared_values: DXBWithSharedValues,
91        caller_metadata: ExecutionCallerMetadata,
92        options: ExecutionOptions,
93        runtime: Runtime,
94        stack: RuntimeExecutionStack,
95    ) -> Self {
96        let state = ExecutionLoopState::new(
97            dxb_with_shared_values.dxb.clone(), // FIXME avoid clone
98            dxb_with_shared_values.shared_values,
99            runtime.clone(),
100            stack,
101            caller_metadata.clone(),
102        );
103        Self {
104            options,
105            caller_metadata,
106            dxb: dxb_with_shared_values.dxb,
107            shared_values: vec![], // FIXME: shared_values where moved to ExecutionLoopState, is that okay?
108            loop_state: Some(state),
109            runtime,
110        }
111    }
112    pub fn into_dxb_and_shared_values(self) -> DXBWithSharedValues {
113        DXBWithSharedValues::new(self.dxb, self.shared_values)
114    }
115
116    pub fn execution_loop(
117        mut self,
118    ) -> (
119        InterruptProvider,
120        impl Iterator<Item = Result<ExternalExecutionInterrupt, ExecutionError>>,
121    ) {
122        // use execution iterator if one already exists from previous execution
123        let mut loop_state =
124            if let Some(existing_loop_state) = self.loop_state.take() {
125                // update dxb so that instruction iterator can continue with next instructions
126                *existing_loop_state.dxb_body.borrow_mut() = self.dxb;
127                existing_loop_state
128            }
129            // otherwise start a new execution loop
130            else {
131                ExecutionLoopState::new(
132                    self.dxb,
133                    self.shared_values,
134                    self.runtime.clone(),
135                    Default::default(),
136                    self.caller_metadata.clone(),
137                )
138            };
139        let interrupt_provider = loop_state.interrupt_provider.clone();
140
141        // proxy the iterator, storing it back into state if interrupted to await more instructions
142        let iterator = gen move {
143            loop {
144                let item = loop_state.iterator.next();
145                if item.is_none() {
146                    break;
147                }
148                let item = item.unwrap();
149
150                match item {
151                    Err(ExecutionError::IntermediateResultWithState(
152                        intermediate_result,
153                        _,
154                    )) => {
155                        yield Err(ExecutionError::IntermediateResultWithState(
156                            intermediate_result,
157                            Some(loop_state),
158                        ));
159                        break;
160                    }
161                    _ => yield item,
162                }
163            }
164        };
165
166        (interrupt_provider, iterator)
167    }
168}