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,8 +1,5 @@
use nu_engine::{command_prelude::*, eval_block_with_early_return};
use nu_protocol::{
debugger::{Profiler, WithDebug},
engine::Closure,
};
use nu_engine::{command_prelude::*, ClosureEvalOnce};
use nu_protocol::{debugger::Profiler, engine::Closure};
#[derive(Clone)]
pub struct DebugProfile;
@ -84,23 +81,18 @@ confusing the id/parent_id hierarchy. The --expr flag is helpful for investigati
fn run(
&self,
engine_state: &EngineState,
caller_stack: &mut Stack,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let closure: Closure = call.req(engine_state, caller_stack, 0)?;
let mut callee_stack = caller_stack.captures_to_stack(closure.captures);
let block = engine_state.get_block(closure.block_id);
let default_max_depth = 2;
let collect_spans = call.has_flag(engine_state, caller_stack, "spans")?;
let collect_expanded_source =
call.has_flag(engine_state, caller_stack, "expanded-source")?;
let collect_values = call.has_flag(engine_state, caller_stack, "values")?;
let collect_exprs = call.has_flag(engine_state, caller_stack, "expr")?;
let closure: Closure = call.req(engine_state, stack, 0)?;
let collect_spans = call.has_flag(engine_state, stack, "spans")?;
let collect_expanded_source = call.has_flag(engine_state, stack, "expanded-source")?;
let collect_values = call.has_flag(engine_state, stack, "values")?;
let collect_exprs = call.has_flag(engine_state, stack, "expr")?;
let max_depth = call
.get_flag(engine_state, caller_stack, "max-depth")?
.unwrap_or(default_max_depth);
.get_flag(engine_state, stack, "max-depth")?
.unwrap_or(2);
let profiler = Profiler::new(
max_depth,
@ -112,26 +104,19 @@ confusing the id/parent_id hierarchy. The --expr flag is helpful for investigati
call.span(),
);
let lock_err = {
|_| ShellError::GenericError {
error: "Profiler Error".to_string(),
msg: "could not lock debugger, poisoned mutex".to_string(),
span: Some(call.head),
help: None,
inner: vec![],
}
let lock_err = |_| ShellError::GenericError {
error: "Profiler Error".to_string(),
msg: "could not lock debugger, poisoned mutex".to_string(),
span: Some(call.head),
help: None,
inner: vec![],
};
engine_state
.activate_debugger(Box::new(profiler))
.map_err(lock_err)?;
let result = eval_block_with_early_return::<WithDebug>(
engine_state,
&mut callee_stack,
block,
input,
);
let result = ClosureEvalOnce::new(engine_state, stack, closure).run_with_input(input);
// TODO: See eval_source()
match result {
@ -142,10 +127,11 @@ confusing the id/parent_id hierarchy. The --expr flag is helpful for investigati
Err(_e) => (), // TODO: Report error
}
let debugger = engine_state.deactivate_debugger().map_err(lock_err)?;
let res = debugger.report(engine_state, call.span());
res.map(|val| val.into_pipeline_data())
Ok(engine_state
.deactivate_debugger()
.map_err(lock_err)?
.report(engine_state, call.span())?
.into_pipeline_data())
}
fn examples(&self) -> Vec<Example> {