use crate::{
core_compiler::instruction_input::InstructionInput,
instruction::regular_instruction::RegularInstruction,
prelude::*,
runtime::{Runtime, execution::ExecutionError},
shared_values::collapsed_container_value::CollapsedContainerValue,
traits::{
apply::{Apply, ApplyArgument},
classification::Classification,
},
types::{
entities::entity_impls::EntityImplMethod, entity_type::EntityType,
},
values::{
core_values::endpoint::Endpoint, value_container::ValueContainer,
},
};
use core::{
cell::Ref,
ops::{Deref, DerefMut},
};
use log::info;
impl ApplyArgument {
pub fn try_call_method_sync(
self,
method_name: &str,
mut args: Vec<ApplyArgument>,
runtime: &Runtime,
) -> Result<(Option<ValueContainer>, Vec<ValueContainer>), ExecutionError>
{
let val = self.value.collapsed_value();
let entity_type = Self::try_get_entity_type(val, runtime)?;
let method = Self::try_get_method_data(&entity_type, method_name)?;
let owner_endpoint = self.value.owner();
args.insert(0, self);
if !method.call_on_owner
|| owner_endpoint.is_local_or_equals_endpoint(runtime.endpoint())
{
let res = method.callable.try_apply_sync_checked(runtime, args)?;
Ok(res)
} else {
Err(ExecutionError::RequiresAsyncExecution)
}
}
pub async fn try_call_method_async(
self,
method_name: &str,
mut args: Vec<ApplyArgument>,
runtime: &Runtime,
) -> Result<(Option<ValueContainer>, Vec<ValueContainer>), ExecutionError>
{
let val = self.value.collapsed_value();
let entity_type = Self::try_get_entity_type(val, runtime)?;
let method = Self::try_get_method_data(&entity_type, method_name);
match method {
Ok(method) => {
self.try_call_method_async_direct(method.deref(), args, runtime)
.await
}
Err(_) => {
drop(method);
info!("fallback to remote call for method: {}", method_name);
let owner = self.value.owner();
args.push(self);
Self::try_call_method_on_owner(
owner,
method_name.to_string(),
args,
runtime,
)
.await
}
}
}
async fn try_call_method_async_direct<'a>(
self,
method: &'a EntityImplMethod,
mut args: Vec<ApplyArgument>,
runtime: &'a Runtime,
) -> Result<(Option<ValueContainer>, Vec<ValueContainer>), ExecutionError>
{
let owner_endpoint = self.value.owner();
if !method.call_on_owner
|| owner_endpoint.is_local_or_equals_endpoint(runtime.endpoint())
{
args.insert(0, self);
let res = method
.callable
.try_apply_async_checked(runtime, args)
.await?;
Ok(res)
}
else {
args.push(self);
Self::try_call_method_on_owner(
owner_endpoint,
method.name().unwrap().clone(),
args,
runtime,
)
.await
}
}
async fn try_call_method_on_owner(
owner_endpoint: Endpoint,
name: String,
args: Vec<ApplyArgument>,
runtime: &Runtime,
) -> Result<(Option<ValueContainer>, Vec<ValueContainer>), ExecutionError>
{
let callee_shared = match args.last() {
Some(ApplyArgument {
value: ValueContainer::Shared(shared),
..
}) => shared,
_ => {
return Err(ExecutionError::ExpectedSharedValue);
}
};
let mark_as_debug = callee_shared.is_debug();
let mut instructions = vec![
RegularInstruction::call_method(
name, args.len() as u8 - 1,
)
.into(),
];
instructions.extend(
args.into_iter()
.map(|v| InstructionInput::ValueContainer(v.value)),
);
let res = runtime
.execute_instructions_remote_single_receiver(
owner_endpoint,
instructions,
mark_as_debug,
)
.await?;
Ok((res, vec![]))
}
fn try_get_entity_type(
value: CollapsedContainerValue,
runtime: &Runtime,
) -> Result<EntityType, ExecutionError> {
if let Some(entity_type) = value
.borrow()
.deref()
.entity_type(runtime.shared_references_cache_mut().deref_mut())
{
Ok(entity_type)
} else {
Err(ExecutionError::ExpectedEntityValue)
}
}
fn try_get_method_data<'a>(
entity_type: &'a EntityType,
method_name: &'a str,
) -> Result<Ref<'a, EntityImplMethod>, ExecutionError> {
Ref::filter_map(entity_type.entity_definition(), |def| {
def.try_get_method(method_name)
})
.map_err(|_| ExecutionError::MethodNotFound(method_name.to_string()))
}
}