use crate::{
global::{
protocol_structures::injected_values::{
InjectedValueDeclaration, InjectedValueType,
SharedInjectedValueType,
},
stack_index::StackIndex,
},
prelude::*,
runtime::{
Runtime,
cache::shared_values_cache::SharedValuesCache,
execution::{
ExecutionError,
execution_input::ExecutionCallerMetadata,
execution_loop::{
ExternalExecutionInterrupt, execution_loop,
interrupts::InterruptProvider, runtime_value::RuntimeValue,
},
},
},
shared_values::{
PointerAddress, SharedContainer,
base_shared_value_container::observers::TransceiverId,
},
values::value_container::ValueContainer,
};
use core::{cell::RefCell, fmt::Debug};
use itertools::{EitherOrBoth, Itertools};
pub struct ExecutionLoopState {
pub iterator: Box<
dyn Iterator<Item = Result<ExternalExecutionInterrupt, ExecutionError>>,
>,
pub dxb_body: Rc<RefCell<Vec<u8>>>,
pub(crate) interrupt_provider: InterruptProvider,
}
impl PartialEq for ExecutionLoopState {
fn eq(&self, other: &Self) -> bool {
Rc::ptr_eq(&self.dxb_body, &other.dxb_body)
&& self.interrupt_provider == other.interrupt_provider
}
}
impl Eq for ExecutionLoopState {}
impl ExecutionLoopState {
pub fn new(
dxb_body: Vec<u8>,
shared_values: Vec<SharedContainer>,
runtime: Runtime,
stack: RuntimeExecutionStack,
caller_metadata: ExecutionCallerMetadata,
) -> Self {
let state = RuntimeExecutionState {
runtime: runtime.clone(),
source_id: TransceiverId::from(
&caller_metadata
.endpoint
.as_local_if_endpoint(runtime.endpoint()),
),
stack,
caller_metadata,
shared_value_cache: SharedValuesCache::new(shared_values),
};
let dxb_rc = Rc::new(RefCell::new(dxb_body.to_vec()));
let interrupt_provider = InterruptProvider::new();
ExecutionLoopState {
dxb_body: dxb_rc.clone(),
iterator: Box::new(execution_loop(
state,
dxb_rc,
interrupt_provider.clone(),
)),
interrupt_provider,
}
}
}
impl Debug for ExecutionLoopState {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("ExecutionIterator")
.field("dxb_body_length", &self.dxb_body.borrow().len())
.finish()
}
}
#[derive(Debug)]
pub struct RuntimeExecutionState {
pub stack: RuntimeExecutionStack,
pub runtime: Runtime,
pub source_id: TransceiverId,
pub caller_metadata: ExecutionCallerMetadata,
pub shared_value_cache: SharedValuesCache,
}
impl RuntimeExecutionState {
pub(crate) fn source_id_cloned(&self) -> TransceiverId {
self.source_id.clone()
}
pub fn normalize_pointer_address(
&self,
address: &PointerAddress,
) -> PointerAddress {
let local_endpoint = self.runtime.endpoint().clone();
let owner_endpoint = &self.caller_metadata.endpoint;
match address {
PointerAddress::SelfOwned(address) => PointerAddress::Remote(
address.remote_for_endpoint(owner_endpoint),
)
.normalize_for_local(&local_endpoint),
PointerAddress::Remote(address) => {
address.normalize_for_local(&local_endpoint)
}
}
}
}
#[derive(Debug, Default)]
pub struct RuntimeExecutionStack {
pub values: Vec<Option<ValueContainer>>,
}
impl RuntimeExecutionStack {
pub fn new(values: Vec<ValueContainer>) -> Self {
Self {
values: values.into_iter().map(Some).collect(),
}
}
pub(crate) fn push(&mut self, value: ValueContainer) {
self.values.push(Some(value));
}
pub(crate) fn current_index(&self) -> StackIndex {
StackIndex(self.values.len() as u32)
}
pub(crate) fn truncate(&mut self, index: StackIndex) {
if index.0 as usize > self.values.len() {
panic!(
"Cannot free stack to index {:?} as it is out of bounds (current stack size: {})",
index,
self.values.len()
);
}
self.values.truncate(index.0 as usize);
}
pub(crate) fn take_stack_value(
&mut self,
index: StackIndex,
) -> Result<ValueContainer, ExecutionError> {
if let Some(stack_value) = self.values.get_mut(index.0 as usize) {
stack_value
.take()
.ok_or_else(|| ExecutionError::StackValueNotAllocated(index))
} else {
Err(ExecutionError::StackOutOfBoundsAccess(index))
}
}
pub(crate) fn set_stack_value(
&mut self,
index: StackIndex,
value: ValueContainer,
) -> Result<Option<ValueContainer>, ExecutionError> {
if let Some(stack_value) = self.values.get_mut(index.0 as usize) {
Ok(stack_value.replace(value))
} else {
Err(ExecutionError::StackOutOfBoundsAccess(index))
}
}
pub(crate) fn get_stack_value(
&self,
index: StackIndex,
) -> Result<&ValueContainer, ExecutionError> {
if let Some(stack_value) = self.values.get(index.0 as usize) {
stack_value
.as_ref()
.ok_or_else(|| ExecutionError::StackValueNotAllocated(index))
} else {
Err(ExecutionError::StackOutOfBoundsAccess(index))
}
}
pub(crate) fn get_stack_value_mut(
&mut self,
index: StackIndex,
) -> Result<&mut ValueContainer, ExecutionError> {
if let Some(stack_value) = self.values.get_mut(index.0 as usize) {
stack_value
.as_mut()
.ok_or_else(|| ExecutionError::StackValueNotAllocated(index))
} else {
Err(ExecutionError::StackOutOfBoundsAccess(index))
}
}
pub fn resolve_injected_values(
&mut self,
injected_values: &[InjectedValueDeclaration],
) -> Result<Vec<ValueContainer>, ExecutionError> {
let mut moved: Vec<Option<_>> = vec![None; injected_values.len()];
for (i, InjectedValueDeclaration { index, ty }) in
injected_values.iter().enumerate()
{
if matches!(
ty,
InjectedValueType::Shared(SharedInjectedValueType::Move)
) {
moved[i] = Some(self.take_stack_value(*index)?);
}
}
let mut resolved_values = Vec::with_capacity(injected_values.len());
for (i, InjectedValueDeclaration { index, ty }) in
injected_values.iter().enumerate()
{
resolved_values.push(match ty {
InjectedValueType::Shared(SharedInjectedValueType::Move) => {
match moved[i].take().unwrap() {
shared @ ValueContainer::Shared(_) => shared,
local @ ValueContainer::Local(_) => {
local
}
}
}
_ => self.get_stack_value(*index)?.clone(), });
}
Ok(resolved_values)
}
pub fn take_runtime_values_with_stack_indices(
&mut self,
values: Vec<RuntimeValue>,
) -> Result<(Vec<ValueContainer>, Vec<Option<StackIndex>>), ExecutionError>
{
let mut stack_indices = Vec::new();
let mut resolved_values = Vec::new();
for value in values {
match value {
RuntimeValue::StackValue(index) => {
stack_indices.push(Some(index));
resolved_values.push(self.take_stack_value(index)?);
}
RuntimeValue::ValueContainer(value) => {
stack_indices.push(None);
resolved_values.push(value);
}
}
}
Ok((resolved_values, stack_indices))
}
pub fn restore_stack_values(
&mut self,
values: Vec<ValueContainer>,
stack_indices: Vec<Option<StackIndex>>,
) -> Result<(), ExecutionError> {
for x in stack_indices.into_iter().flatten().zip_longest(values) {
match x {
EitherOrBoth::Both(previous, next) => {
self.set_stack_value(previous, next)?;
}
_ => {
return Err(ExecutionError::StackRestoreMismatch);
}
}
}
Ok(())
}
}