Refactor using ClosureEval types (#12541)

# Description
Adds two new types in `nu-engine` for evaluating closures: `ClosureEval`
and `ClosureEvalOnce`. This removed some duplicate code and centralizes
our logic for setting up, running, and cleaning up closures. For
example, in the future if we are able to reduce the cloning necessary to
run a closure, then we only have to change the code related to these
types.

`ClosureEval` and `ClosureEvalOnce` are designed with a builder API.
`ClosureEval` is used to run a closure multiple times whereas
`ClosureEvalOnce` is used for a one-shot closure.

# User-Facing Changes
Should be none, unless I messed up one of the command migrations.
Actually, this will fix any unreported environment bugs for commands
that didn't reset the env after running a closure.
This commit is contained in:
Ian Manske
2024-04-22 06:15:09 +00:00
committed by GitHub
parent 83720a9f30
commit bae6d694ca
29 changed files with 930 additions and 1486 deletions

View File

@ -1,5 +1,5 @@
use crate::util::MutableCow;
use nu_engine::{get_eval_block_with_early_return, get_full_help};
use nu_engine::{get_eval_block_with_early_return, get_full_help, ClosureEvalOnce};
use nu_protocol::{
ast::Call,
engine::{Closure, EngineState, Redirection, Stack},
@ -112,23 +112,10 @@ impl<'a> PluginExecutionContext for PluginExecutionCommandContext<'a> {
let span = value.span();
match value {
Value::Closure { val, .. } => {
let input = PipelineData::Empty;
let block = self.engine_state.get_block(val.block_id).clone();
let mut stack = self.stack.captures_to_stack(val.captures);
let eval_block_with_early_return =
get_eval_block_with_early_return(&self.engine_state);
match eval_block_with_early_return(
&self.engine_state,
&mut stack,
&block,
input,
) {
Ok(v) => v.into_value(span),
Err(e) => Value::error(e, self.call.head),
}
ClosureEvalOnce::new(&self.engine_state, &self.stack, val)
.run_with_input(PipelineData::Empty)
.map(|data| data.into_value(span))
.unwrap_or_else(|err| Value::error(err, self.call.head))
}
_ => value.clone(),
}