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