datex_core/runtime/execution/
execution_input.rs1use 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#[derive(Debug)]
42pub struct ExecutionInput {
43 pub options: ExecutionOptions,
45 pub caller_metadata: ExecutionCallerMetadata,
47 pub dxb: Vec<u8>,
49 pub shared_values: Vec<SharedContainer>,
50 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(), 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![], 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 let mut loop_state =
124 if let Some(existing_loop_state) = self.loop_state.take() {
125 *existing_loop_state.dxb_body.borrow_mut() = self.dxb;
127 existing_loop_state
128 }
129 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 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}