mirror of
https://github.com/fluencelabs/aquavm
synced 2025-03-16 04:50:49 +00:00
make apply(_with_tetraplets) always return only one value (#174)
This commit is contained in:
parent
5cd45385b4
commit
d75be8f9ea
air/src/execution_step
@ -110,11 +110,7 @@ fn apply_scalar_wl_impl(
|
||||
trace_ctx: &TraceHandler,
|
||||
) -> ExecutionResult<ValueAggregate> {
|
||||
let variable = Variable::scalar(scalar_name, position);
|
||||
let (jvalue, mut tetraplets) = apply_lambda(variable, lambda, exec_ctx)?;
|
||||
|
||||
let tetraplet = tetraplets
|
||||
.pop()
|
||||
.unwrap_or_else(|| Rc::new(RefCell::new(SecurityTetraplet::default())));
|
||||
let (jvalue, tetraplet) = apply_lambda(variable, lambda, exec_ctx)?;
|
||||
let result = ValueAggregate::new(Rc::new(jvalue), tetraplet, trace_ctx.trace_pos());
|
||||
|
||||
Ok(result)
|
||||
|
@ -132,10 +132,10 @@ fn create_scalar_lambda_iterable<'ctx>(
|
||||
}
|
||||
ScalarRef::IterableValue(fold_state) => {
|
||||
let iterable_value = fold_state.iterable.peek().unwrap();
|
||||
let jvalues = iterable_value.apply_lambda(lambda)?;
|
||||
let jvalue = iterable_value.apply_lambda(lambda)?;
|
||||
let tetraplet = as_tetraplet(&iterable_value);
|
||||
|
||||
from_jvalue(jvalues[0], tetraplet, lambda)
|
||||
from_jvalue(jvalue, tetraplet, lambda)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -25,6 +25,7 @@ use super::ExecutionError;
|
||||
use super::ExecutionResult;
|
||||
use super::ValueAggregate;
|
||||
use crate::execution_step::lambda_applier::*;
|
||||
use crate::execution_step::RSecurityTetraplet;
|
||||
use crate::execution_step::SecurityTetraplets;
|
||||
use crate::JValue;
|
||||
use crate::LambdaAST;
|
||||
@ -36,13 +37,10 @@ use std::borrow::Cow;
|
||||
/// Represent a value that could be transform to a JValue with or without tetraplets.
|
||||
pub(crate) trait JValuable {
|
||||
/// Applies lambda to the internal value, produces JValue.
|
||||
fn apply_lambda(&self, lambda: &LambdaAST<'_>) -> ExecutionResult<Vec<&JValue>>;
|
||||
fn apply_lambda(&self, lambda: &LambdaAST<'_>) -> ExecutionResult<&JValue>;
|
||||
|
||||
/// Applies lambda to the internal value, produces JValue with tetraplet.
|
||||
fn apply_lambda_with_tetraplets(
|
||||
&self,
|
||||
lambda: &LambdaAST<'_>,
|
||||
) -> ExecutionResult<(Vec<&JValue>, SecurityTetraplets)>;
|
||||
fn apply_lambda_with_tetraplets(&self, lambda: &LambdaAST<'_>) -> ExecutionResult<(&JValue, RSecurityTetraplet)>;
|
||||
|
||||
/// Return internal value as borrowed if it's possible, owned otherwise.
|
||||
fn as_jvalue(&self) -> Cow<'_, JValue>;
|
||||
|
@ -18,6 +18,7 @@ use super::select_from_stream;
|
||||
use super::ExecutionResult;
|
||||
use super::JValuable;
|
||||
use super::ValueAggregate;
|
||||
use crate::execution_step::RSecurityTetraplet;
|
||||
use crate::execution_step::SecurityTetraplets;
|
||||
use crate::JValue;
|
||||
use crate::LambdaAST;
|
||||
@ -28,23 +29,20 @@ use std::borrow::Cow;
|
||||
use std::ops::Deref;
|
||||
|
||||
impl JValuable for std::cell::Ref<'_, Vec<ValueAggregate>> {
|
||||
fn apply_lambda(&self, lambda: &LambdaAST<'_>) -> ExecutionResult<Vec<&JValue>> {
|
||||
fn apply_lambda(&self, lambda: &LambdaAST<'_>) -> ExecutionResult<&JValue> {
|
||||
let stream_iter = self.iter().map(|r| r.result.deref());
|
||||
let select_result = select_from_stream(stream_iter, lambda)?;
|
||||
Ok(vec![select_result.result])
|
||||
Ok(select_result.result)
|
||||
}
|
||||
|
||||
fn apply_lambda_with_tetraplets(
|
||||
&self,
|
||||
lambda: &LambdaAST<'_>,
|
||||
) -> ExecutionResult<(Vec<&JValue>, SecurityTetraplets)> {
|
||||
fn apply_lambda_with_tetraplets(&self, lambda: &LambdaAST<'_>) -> ExecutionResult<(&JValue, RSecurityTetraplet)> {
|
||||
let stream_iter = self.iter().map(|r| r.result.deref());
|
||||
let select_result = select_from_stream(stream_iter, lambda)?;
|
||||
|
||||
let tetraplet = self[select_result.tetraplet_idx].tetraplet.clone();
|
||||
tetraplet.borrow_mut().add_lambda(&format_ast(lambda));
|
||||
|
||||
Ok((vec![select_result.result], vec![tetraplet]))
|
||||
Ok((select_result.result, tetraplet))
|
||||
}
|
||||
|
||||
fn as_jvalue(&self) -> Cow<'_, JValue> {
|
||||
|
@ -19,21 +19,19 @@ use super::ExecutionResult;
|
||||
use super::JValuable;
|
||||
use super::LambdaAST;
|
||||
use crate::exec_err;
|
||||
use crate::execution_step::RSecurityTetraplet;
|
||||
use crate::execution_step::SecurityTetraplets;
|
||||
use crate::JValue;
|
||||
|
||||
use std::borrow::Cow;
|
||||
|
||||
impl JValuable for () {
|
||||
fn apply_lambda(&self, _lambda: &LambdaAST<'_>) -> ExecutionResult<Vec<&JValue>> {
|
||||
fn apply_lambda(&self, _lambda: &LambdaAST<'_>) -> ExecutionResult<&JValue> {
|
||||
// applying lambda to an empty stream will produce a join behaviour
|
||||
exec_err!(ExecutionError::EmptyStreamLambdaError)
|
||||
}
|
||||
|
||||
fn apply_lambda_with_tetraplets(
|
||||
&self,
|
||||
_lambda: &LambdaAST<'_>,
|
||||
) -> ExecutionResult<(Vec<&JValue>, SecurityTetraplets)> {
|
||||
fn apply_lambda_with_tetraplets(&self, _lambda: &LambdaAST<'_>) -> ExecutionResult<(&JValue, RSecurityTetraplet)> {
|
||||
// applying lambda to an empty stream will produce a join behaviour
|
||||
exec_err!(ExecutionError::EmptyStreamLambdaError)
|
||||
}
|
||||
|
@ -19,6 +19,7 @@ use super::ExecutionResult;
|
||||
use super::IterableItem;
|
||||
use super::JValuable;
|
||||
use super::LambdaAST;
|
||||
use crate::execution_step::RSecurityTetraplet;
|
||||
use crate::execution_step::SecurityTetraplets;
|
||||
use crate::JValue;
|
||||
|
||||
@ -26,7 +27,7 @@ use std::borrow::Cow;
|
||||
use std::ops::Deref;
|
||||
|
||||
impl<'ctx> JValuable for IterableItem<'ctx> {
|
||||
fn apply_lambda(&self, lambda: &LambdaAST<'_>) -> ExecutionResult<Vec<&JValue>> {
|
||||
fn apply_lambda(&self, lambda: &LambdaAST<'_>) -> ExecutionResult<&JValue> {
|
||||
use super::IterableItem::*;
|
||||
|
||||
let jvalue = match self {
|
||||
@ -36,13 +37,10 @@ impl<'ctx> JValuable for IterableItem<'ctx> {
|
||||
};
|
||||
|
||||
let selected_value = select(jvalue, lambda.iter())?;
|
||||
Ok(vec![selected_value])
|
||||
Ok(selected_value)
|
||||
}
|
||||
|
||||
fn apply_lambda_with_tetraplets(
|
||||
&self,
|
||||
lambda: &LambdaAST<'_>,
|
||||
) -> ExecutionResult<(Vec<&JValue>, SecurityTetraplets)> {
|
||||
fn apply_lambda_with_tetraplets(&self, lambda: &LambdaAST<'_>) -> ExecutionResult<(&JValue, RSecurityTetraplet)> {
|
||||
use super::IterableItem::*;
|
||||
|
||||
let (jvalue, tetraplet) = match self {
|
||||
@ -52,7 +50,7 @@ impl<'ctx> JValuable for IterableItem<'ctx> {
|
||||
};
|
||||
|
||||
let selected_value = select(jvalue, lambda.iter())?;
|
||||
Ok((vec![selected_value], vec![tetraplet.clone()]))
|
||||
Ok((selected_value, tetraplet.clone()))
|
||||
}
|
||||
|
||||
fn as_jvalue(&self) -> Cow<'_, JValue> {
|
||||
|
@ -19,6 +19,7 @@ use super::ExecutionResult;
|
||||
use super::JValuable;
|
||||
use super::LambdaAST;
|
||||
use super::ValueAggregate;
|
||||
use crate::execution_step::RSecurityTetraplet;
|
||||
use crate::execution_step::SecurityTetraplets;
|
||||
use crate::JValue;
|
||||
|
||||
@ -28,20 +29,17 @@ use std::borrow::Cow;
|
||||
use std::ops::Deref;
|
||||
|
||||
impl JValuable for ValueAggregate {
|
||||
fn apply_lambda(&self, lambda: &LambdaAST<'_>) -> ExecutionResult<Vec<&JValue>> {
|
||||
fn apply_lambda(&self, lambda: &LambdaAST<'_>) -> ExecutionResult<&JValue> {
|
||||
let selected_value = select(&self.result, lambda.iter())?;
|
||||
Ok(vec![selected_value])
|
||||
Ok(selected_value)
|
||||
}
|
||||
|
||||
fn apply_lambda_with_tetraplets(
|
||||
&self,
|
||||
lambda: &LambdaAST<'_>,
|
||||
) -> ExecutionResult<(Vec<&JValue>, SecurityTetraplets)> {
|
||||
fn apply_lambda_with_tetraplets(&self, lambda: &LambdaAST<'_>) -> ExecutionResult<(&JValue, RSecurityTetraplet)> {
|
||||
let selected_value = select(&self.result, lambda.iter())?;
|
||||
let tetraplet = self.tetraplet.clone();
|
||||
tetraplet.borrow_mut().add_lambda(&format_ast(lambda));
|
||||
|
||||
Ok((vec![selected_value], vec![tetraplet]))
|
||||
Ok((selected_value, tetraplet))
|
||||
}
|
||||
|
||||
fn as_jvalue(&self) -> Cow<'_, JValue> {
|
||||
|
@ -20,6 +20,7 @@ use super::JValuable;
|
||||
use crate::exec_err;
|
||||
use crate::execution_step::boxed_value::Generation;
|
||||
use crate::execution_step::boxed_value::Stream;
|
||||
use crate::execution_step::RSecurityTetraplet;
|
||||
use crate::execution_step::SecurityTetraplets;
|
||||
use crate::JValue;
|
||||
use crate::LambdaAST;
|
||||
@ -38,17 +39,14 @@ pub(crate) struct StreamJvaluableIngredients<'stream> {
|
||||
// TODO: this will be deleted soon, because it would be impossible to use streams without
|
||||
// canonicalization as an arg of a call
|
||||
impl JValuable for StreamJvaluableIngredients<'_> {
|
||||
fn apply_lambda(&self, lambda: &LambdaAST<'_>) -> ExecutionResult<Vec<&JValue>> {
|
||||
fn apply_lambda(&self, lambda: &LambdaAST<'_>) -> ExecutionResult<&JValue> {
|
||||
let iter = self.iter()?.map(|v| v.result.deref());
|
||||
let select_result = select_from_stream(iter, lambda)?;
|
||||
|
||||
Ok(vec![select_result.result])
|
||||
Ok(select_result.result)
|
||||
}
|
||||
|
||||
fn apply_lambda_with_tetraplets(
|
||||
&self,
|
||||
lambda: &LambdaAST<'_>,
|
||||
) -> ExecutionResult<(Vec<&JValue>, SecurityTetraplets)> {
|
||||
fn apply_lambda_with_tetraplets(&self, lambda: &LambdaAST<'_>) -> ExecutionResult<(&JValue, RSecurityTetraplet)> {
|
||||
let iter = self.iter()?.map(|v| v.result.deref());
|
||||
let select_result = select_from_stream(iter, lambda)?;
|
||||
|
||||
@ -57,7 +55,7 @@ impl JValuable for StreamJvaluableIngredients<'_> {
|
||||
let tetraplet = resolved_call.tetraplet.clone();
|
||||
tetraplet.borrow_mut().add_lambda(&format_ast(lambda));
|
||||
|
||||
Ok((vec![select_result.result], vec![tetraplet]))
|
||||
Ok((select_result.result, tetraplet))
|
||||
}
|
||||
|
||||
fn as_jvalue(&self) -> Cow<'_, JValue> {
|
||||
|
@ -20,6 +20,7 @@ use crate::execution_step::boxed_value::Variable;
|
||||
use crate::execution_step::execution_context::ExecutionCtx;
|
||||
use crate::execution_step::execution_context::LastErrorWithTetraplet;
|
||||
use crate::execution_step::ExecutionResult;
|
||||
use crate::execution_step::RSecurityTetraplet;
|
||||
use crate::JValue;
|
||||
use crate::LambdaAST;
|
||||
use crate::SecurityTetraplet;
|
||||
@ -112,7 +113,7 @@ pub(crate) fn resolve_ast_variable_wl<'ctx, 'i>(
|
||||
) -> ExecutionResult<(JValue, SecurityTetraplets)> {
|
||||
let variable: Variable<'_> = ast_variable.into();
|
||||
match ast_variable.lambda() {
|
||||
Some(lambda) => apply_lambda(variable, lambda, exec_ctx),
|
||||
Some(lambda) => apply_lambda(variable, lambda, exec_ctx).map(|(value, tetraplet)| (value, vec![tetraplet])),
|
||||
None => {
|
||||
let value = resolve_variable(variable, exec_ctx)?;
|
||||
let tetraplets = value.as_tetraplets();
|
||||
@ -125,10 +126,10 @@ pub(crate) fn apply_lambda<'i>(
|
||||
variable: Variable<'_>,
|
||||
lambda: &LambdaAST<'i>,
|
||||
exec_ctx: &ExecutionCtx<'i>,
|
||||
) -> ExecutionResult<(JValue, SecurityTetraplets)> {
|
||||
) -> ExecutionResult<(JValue, RSecurityTetraplet)> {
|
||||
let resolved = resolve_variable(variable, exec_ctx)?;
|
||||
let (jvalue, tetraplets) = resolved.apply_lambda_with_tetraplets(lambda)?;
|
||||
let (jvalue, tetraplet) = resolved.apply_lambda_with_tetraplets(lambda)?;
|
||||
|
||||
// it's known that apply_lambda_with_tetraplets returns vec of one value
|
||||
Ok((jvalue[0].clone(), tetraplets))
|
||||
Ok((jvalue.clone(), tetraplet))
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user