Skip to main content

datex_core/runtime/execution/execution_loop/
interrupts.rs

1use core::cell::RefCell;
2
3use crate::values::value_container::ValueContainer;
4
5use crate::{
6    core_compiler::core_compilation_context::DXBWithSharedValues,
7    libs::core::core_lib_id::CoreLibId,
8    prelude::*,
9    shared_values::{
10        ReferenceMutability, RemotePointerAddress, SelfOwnedPointerAddress,
11    },
12    traits::apply::ApplyArgument,
13    values::core_values::endpoint::Endpoint,
14};
15
16#[derive(Debug)]
17pub enum ExecutionInterrupt {
18    // used for intermediate results in unbounded scopes
19    SetActiveValue(Option<ValueContainer>),
20    TakeActiveValue,
21    /// yields an external interrupt to be handled by the execution loop caller (for I/O operations, pointer resolution, remote execution, etc.)
22    External(ExternalExecutionInterrupt),
23}
24
25#[derive(Debug)]
26pub enum ExternalExecutionInterrupt {
27    Result(Option<ValueContainer>),
28    GetReferenceToRemotePointer(RemotePointerAddress, ReferenceMutability),
29    GetReferenceToLocalPointer(SelfOwnedPointerAddress),
30    GetCoreLibValue(CoreLibId),
31    RemoteExecution {
32        input: DXBWithSharedValues,
33        receivers: Vec<Endpoint>,
34    },
35    SetEndpointProperty {
36        endpoint: Endpoint,
37        property_name: String,
38        value: ValueContainer,
39    },
40    GetEndpointProperty {
41        endpoint: Endpoint,
42        property_name: String,
43    },
44    Apply(ValueContainer, Vec<ApplyArgument>),
45    CallMethod(ApplyArgument, String, Vec<ApplyArgument>),
46}
47
48#[derive(Debug)]
49pub enum InterruptResult {
50    /// Used to return a single resolved value or None
51    ResolvedValue(Option<ValueContainer>),
52    /// Used to return multiple resolved values
53    ResolvedValues(Vec<ValueContainer>),
54    /// Used in function calls: returns all borrowed arguments back, as well as an optional result value
55    ResolvedValueAndBorrowedArgs((Option<ValueContainer>, Vec<ValueContainer>)),
56}
57
58#[derive(Debug, Clone)]
59pub struct InterruptProvider {
60    result: Rc<RefCell<Option<InterruptResult>>>,
61}
62
63impl PartialEq for InterruptProvider {
64    fn eq(&self, other: &Self) -> bool {
65        Rc::ptr_eq(&self.result, &other.result)
66    }
67}
68
69impl Eq for InterruptProvider {}
70
71impl Default for InterruptProvider {
72    fn default() -> Self {
73        Self::new()
74    }
75}
76
77impl InterruptProvider {
78    pub fn new() -> Self {
79        Self {
80            result: Rc::new(RefCell::new(None)),
81        }
82    }
83
84    pub fn provide_result(&self, result: InterruptResult) {
85        *self.result.borrow_mut() = Some(result);
86    }
87
88    pub fn take_result(&self) -> Option<InterruptResult> {
89        self.result.borrow_mut().take()
90    }
91}