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
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
29 changed files with 930 additions and 1486 deletions

View File

@ -1,6 +1,6 @@
use crate::NushellPrompt; use crate::NushellPrompt;
use log::trace; use log::trace;
use nu_engine::get_eval_subexpression; use nu_engine::ClosureEvalOnce;
use nu_protocol::{ use nu_protocol::{
engine::{EngineState, Stack, StateWorkingSet}, engine::{EngineState, Stack, StateWorkingSet},
report_error, Config, PipelineData, Value, report_error, Config, PipelineData, Value,
@ -34,17 +34,13 @@ fn get_prompt_string(
engine_state: &EngineState, engine_state: &EngineState,
stack: &mut Stack, stack: &mut Stack,
) -> Option<String> { ) -> Option<String> {
let eval_subexpression = get_eval_subexpression(engine_state);
stack stack
.get_env_var(engine_state, prompt) .get_env_var(engine_state, prompt)
.and_then(|v| match v { .and_then(|v| match v {
Value::Closure { val, .. } => { Value::Closure { val, .. } => {
let block = engine_state.get_block(val.block_id); let result = ClosureEvalOnce::new(engine_state, stack, val)
let mut stack = stack.captures_to_stack(val.captures); .run_with_input(PipelineData::Empty);
// Use eval_subexpression to force a redirection of output, so we can use everything in prompt
let ret_val =
eval_subexpression(engine_state, &mut stack, block, PipelineData::empty());
trace!( trace!(
"get_prompt_string (block) {}:{}:{}", "get_prompt_string (block) {}:{}:{}",
file!(), file!(),
@ -52,7 +48,7 @@ fn get_prompt_string(
column!() column!()
); );
ret_val result
.map_err(|err| { .map_err(|err| {
let working_set = StateWorkingSet::new(engine_state); let working_set = StateWorkingSet::new(engine_state);
report_error(&working_set, &err); report_error(&working_set, &err);

View File

@ -1,4 +1,4 @@
use nu_engine::{command_prelude::*, get_eval_block_with_early_return}; use nu_engine::{command_prelude::*, ClosureEval, ClosureEvalOnce};
use nu_protocol::engine::Closure; use nu_protocol::engine::Closure;
#[derive(Clone)] #[derive(Clone)]
@ -67,116 +67,56 @@ impl Command for EachWhile {
call: &Call, call: &Call,
input: PipelineData, input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let capture_block: Closure = call.req(engine_state, stack, 0)?; let head = call.head;
let closure: Closure = call.req(engine_state, stack, 0)?;
let metadata = input.metadata(); let metadata = input.metadata();
let ctrlc = engine_state.ctrlc.clone();
let engine_state = engine_state.clone();
let block = engine_state.get_block(capture_block.block_id).clone();
let mut stack = stack.captures_to_stack(capture_block.captures);
let orig_env_vars = stack.env_vars.clone();
let orig_env_hidden = stack.env_hidden.clone();
let span = call.head;
let eval_block_with_early_return = get_eval_block_with_early_return(&engine_state);
match input { match input {
PipelineData::Empty => Ok(PipelineData::Empty), PipelineData::Empty => Ok(PipelineData::Empty),
PipelineData::Value(Value::Range { .. }, ..) PipelineData::Value(Value::Range { .. }, ..)
| PipelineData::Value(Value::List { .. }, ..) | PipelineData::Value(Value::List { .. }, ..)
| PipelineData::ListStream { .. } => Ok(input | PipelineData::ListStream(..) => {
// TODO: Could this be changed to .into_interruptible_iter(ctrlc) ? let mut closure = ClosureEval::new(engine_state, stack, closure);
.into_iter() Ok(input
.map_while(move |x| { .into_iter()
// with_env() is used here to ensure that each iteration uses .map_while(move |value| match closure.run_with_value(value) {
// a different set of environment variables. Ok(data) => {
// Hence, a 'cd' in the first loop won't affect the next loop. let value = data.into_value(head);
stack.with_env(&orig_env_vars, &orig_env_hidden); (!value.is_nothing()).then_some(value)
if let Some(var) = block.signature.get_positional(0) {
if let Some(var_id) = &var.var_id {
stack.add_var(*var_id, x.clone());
}
}
match eval_block_with_early_return(
&engine_state,
&mut stack,
&block,
x.into_pipeline_data(),
) {
Ok(v) => {
let value = v.into_value(span);
if value.is_nothing() {
None
} else {
Some(value)
}
} }
Err(_) => None, Err(_) => None,
} })
}) .fuse()
.fuse() .into_pipeline_data(engine_state.ctrlc.clone()))
.into_pipeline_data(ctrlc)), }
PipelineData::ExternalStream { stdout: None, .. } => Ok(PipelineData::empty()), PipelineData::ExternalStream { stdout: None, .. } => Ok(PipelineData::empty()),
PipelineData::ExternalStream { PipelineData::ExternalStream {
stdout: Some(stream), stdout: Some(stream),
.. ..
} => Ok(stream } => {
.into_iter() let mut closure = ClosureEval::new(engine_state, stack, closure);
.map_while(move |x| { Ok(stream
// with_env() is used here to ensure that each iteration uses .into_iter()
// a different set of environment variables. .map_while(move |value| {
// Hence, a 'cd' in the first loop won't affect the next loop. let value = value.ok()?;
stack.with_env(&orig_env_vars, &orig_env_hidden); match closure.run_with_value(value) {
Ok(data) => {
let x = match x { let value = data.into_value(head);
Ok(x) => x, (!value.is_nothing()).then_some(value)
Err(_) => return None,
};
if let Some(var) = block.signature.get_positional(0) {
if let Some(var_id) = &var.var_id {
stack.add_var(*var_id, x.clone());
}
}
match eval_block_with_early_return(
&engine_state,
&mut stack,
&block,
x.into_pipeline_data(),
) {
Ok(v) => {
let value = v.into_value(span);
if value.is_nothing() {
None
} else {
Some(value)
} }
Err(_) => None,
} }
Err(_) => None, })
} .fuse()
}) .into_pipeline_data(engine_state.ctrlc.clone()))
.fuse() }
.into_pipeline_data(ctrlc)),
// This match allows non-iterables to be accepted, // This match allows non-iterables to be accepted,
// which is currently considered undesirable (Nov 2022). // which is currently considered undesirable (Nov 2022).
PipelineData::Value(x, ..) => { PipelineData::Value(value, ..) => {
if let Some(var) = block.signature.get_positional(0) { ClosureEvalOnce::new(engine_state, stack, closure).run_with_value(value)
if let Some(var_id) = &var.var_id {
stack.add_var(*var_id, x.clone());
}
}
eval_block_with_early_return(
&engine_state,
&mut stack,
&block,
x.into_pipeline_data(),
)
} }
} }
.map(|x| x.set_metadata(metadata)) .map(|data| data.set_metadata(metadata))
} }
} }

View File

@ -1,6 +1,6 @@
use nu_engine::{command_prelude::*, get_eval_block, EvalBlockFn}; use nu_engine::{command_prelude::*, ClosureEval};
use nu_protocol::{ast::Block, engine::Closure, PipelineIterator}; use nu_protocol::{engine::Closure, PipelineIterator};
use std::{collections::HashSet, sync::Arc}; use std::collections::HashSet;
#[derive(Clone)] #[derive(Clone)]
pub struct UpdateCells; pub struct UpdateCells;
@ -87,24 +87,9 @@ impl Command for UpdateCells {
call: &Call, call: &Call,
input: PipelineData, input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
// the block to run on each cell let head = call.head;
let engine_state = engine_state.clone(); let closure: Closure = call.req(engine_state, stack, 0)?;
let block: Closure = call.req(&engine_state, stack, 0)?; let columns: Option<Value> = call.get_flag(engine_state, stack, "columns")?;
let mut stack = stack.captures_to_stack(block.captures);
let orig_env_vars = stack.env_vars.clone();
let orig_env_hidden = stack.env_hidden.clone();
let metadata = input.metadata();
let ctrlc = engine_state.ctrlc.clone();
let block: Arc<Block> = engine_state.get_block(block.block_id).clone();
let eval_block_fn = get_eval_block(&engine_state);
let span = call.head;
stack.with_env(&orig_env_vars, &orig_env_hidden);
// the columns to update
let columns: Option<Value> = call.get_flag(&engine_state, &mut stack, "columns")?;
let columns: Option<HashSet<String>> = match columns { let columns: Option<HashSet<String>> = match columns {
Some(val) => Some( Some(val) => Some(
val.into_list()? val.into_list()?
@ -115,27 +100,23 @@ impl Command for UpdateCells {
None => None, None => None,
}; };
let metadata = input.metadata();
Ok(UpdateCellIterator { Ok(UpdateCellIterator {
input: input.into_iter(), iter: input.into_iter(),
engine_state, closure: ClosureEval::new(engine_state, stack, closure),
stack,
block,
columns, columns,
span, span: head,
eval_block_fn,
} }
.into_pipeline_data(ctrlc) .into_pipeline_data(engine_state.ctrlc.clone())
.set_metadata(metadata)) .set_metadata(metadata))
} }
} }
struct UpdateCellIterator { struct UpdateCellIterator {
input: PipelineIterator, iter: PipelineIterator,
closure: ClosureEval,
columns: Option<HashSet<String>>, columns: Option<HashSet<String>>,
engine_state: EngineState,
stack: Stack,
block: Arc<Block>,
eval_block_fn: EvalBlockFn,
span: Span, span: Span,
} }
@ -143,70 +124,36 @@ impl Iterator for UpdateCellIterator {
type Item = Value; type Item = Value;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
match self.input.next() { let mut value = self.iter.next()?;
Some(val) => {
if let Some(ref cols) = self.columns { let value = if let Value::Record { val, .. } = &mut value {
if !val.columns().any(|c| cols.contains(c)) { let val = val.to_mut();
return Some(val); if let Some(columns) = &self.columns {
for (col, val) in val.iter_mut() {
if columns.contains(col) {
*val = eval_value(&mut self.closure, self.span, std::mem::take(val));
} }
} }
} else {
let span = val.span(); for (_, val) in val.iter_mut() {
match val { *val = eval_value(&mut self.closure, self.span, std::mem::take(val))
Value::Record { val, .. } => Some(Value::record(
val.into_owned()
.into_iter()
.map(|(col, val)| match &self.columns {
Some(cols) if !cols.contains(&col) => (col, val),
_ => (
col,
process_cell(
val,
&self.engine_state,
&mut self.stack,
&self.block,
span,
self.eval_block_fn,
),
),
})
.collect(),
span,
)),
val => Some(process_cell(
val,
&self.engine_state,
&mut self.stack,
&self.block,
self.span,
self.eval_block_fn,
)),
} }
} }
None => None,
} value
} else {
eval_value(&mut self.closure, self.span, value)
};
Some(value)
} }
} }
#[allow(clippy::too_many_arguments)] fn eval_value(closure: &mut ClosureEval, span: Span, value: Value) -> Value {
fn process_cell( closure
val: Value, .run_with_value(value)
engine_state: &EngineState, .map(|data| data.into_value(span))
stack: &mut Stack, .unwrap_or_else(|err| Value::error(err, span))
block: &Block,
span: Span,
eval_block_fn: EvalBlockFn,
) -> Value {
if let Some(var) = block.signature.get_positional(0) {
if let Some(var_id) = &var.var_id {
stack.add_var(*var_id, val.clone());
}
}
match eval_block_fn(engine_state, stack, block, val.into_pipeline_data()) {
Ok(pd) => pd.into_value(span),
Err(e) => Value::error(e, span),
}
} }
#[cfg(test)] #[cfg(test)]

View File

@ -1,11 +1,10 @@
use crate::{color_record_to_nustyle, lookup_ansi_color_style, text_style::Alignment, TextStyle}; use crate::{color_record_to_nustyle, lookup_ansi_color_style, text_style::Alignment, TextStyle};
use nu_ansi_term::{Color, Style}; use nu_ansi_term::{Color, Style};
use nu_engine::{env::get_config, eval_block}; use nu_engine::{env::get_config, ClosureEvalOnce};
use nu_protocol::{ use nu_protocol::{
cli_error::CliError, cli_error::CliError,
debugger::WithoutDebug, engine::{Closure, EngineState, Stack, StateWorkingSet},
engine::{EngineState, Stack, StateWorkingSet}, Span, Value,
IntoPipelineData, Value,
}; };
use std::{ use std::{
collections::HashMap, collections::HashMap,
@ -18,7 +17,7 @@ use std::{
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum ComputableStyle { pub enum ComputableStyle {
Static(Style), Static(Style),
Closure(Value), Closure(Closure, Span),
} }
// An alias for the mapping used internally by StyleComputer. // An alias for the mapping used internally by StyleComputer.
@ -56,51 +55,31 @@ impl<'a> StyleComputer<'a> {
// Static values require no computation. // Static values require no computation.
Some(ComputableStyle::Static(s)) => *s, Some(ComputableStyle::Static(s)) => *s,
// Closures are run here. // Closures are run here.
Some(ComputableStyle::Closure(v)) => { Some(ComputableStyle::Closure(closure, span)) => {
let span = v.span(); let result = ClosureEvalOnce::new(self.engine_state, self.stack, closure.clone())
match v { .debug(false)
Value::Closure { val, .. } => { .run_with_value(value.clone());
let block = self.engine_state.get_block(val.block_id).clone();
// Because captures_to_stack() clones, we don't need to use with_env() here
// (contrast with_env() usage in `each` or `do`).
let mut stack = self.stack.captures_to_stack(val.captures.clone());
// Support 1-argument blocks as well as 0-argument blocks. match result {
if let Some(var) = block.signature.get_positional(0) { Ok(v) => {
if let Some(var_id) = &var.var_id { let value = v.into_value(*span);
stack.add_var(*var_id, value.clone()); // These should be the same color data forms supported by color_config.
} match value {
} Value::Record { .. } => color_record_to_nustyle(&value),
Value::String { val, .. } => lookup_ansi_color_style(&val),
// Run the block. _ => Style::default(),
match eval_block::<WithoutDebug>(
self.engine_state,
&mut stack,
&block,
value.clone().into_pipeline_data(),
) {
Ok(v) => {
let value = v.into_value(span);
// These should be the same color data forms supported by color_config.
match value {
Value::Record { .. } => color_record_to_nustyle(&value),
Value::String { val, .. } => lookup_ansi_color_style(&val),
_ => Style::default(),
}
}
// This is basically a copy of nu_cli::report_error(), but that isn't usable due to
// dependencies. While crudely spitting out a bunch of errors like this is not ideal,
// currently hook closure errors behave roughly the same.
Err(e) => {
eprintln!(
"Error: {:?}",
CliError(&e, &StateWorkingSet::new(self.engine_state))
);
Style::default()
}
} }
} }
_ => Style::default(), // This is basically a copy of nu_cli::report_error(), but that isn't usable due to
// dependencies. While crudely spitting out a bunch of errors like this is not ideal,
// currently hook closure errors behave roughly the same.
Err(e) => {
eprintln!(
"Error: {:?}",
CliError(&e, &StateWorkingSet::new(self.engine_state))
);
Style::default()
}
} }
} }
// There should be no other kinds of values (due to create_map() in config.rs filtering them out) // There should be no other kinds of values (due to create_map() in config.rs filtering them out)
@ -165,9 +144,10 @@ impl<'a> StyleComputer<'a> {
].into_iter().collect(); ].into_iter().collect();
for (key, value) in &config.color_config { for (key, value) in &config.color_config {
let span = value.span();
match value { match value {
Value::Closure { .. } => { Value::Closure { val, .. } => {
map.insert(key.to_string(), ComputableStyle::Closure(value.clone())); map.insert(key.to_string(), ComputableStyle::Closure(val.clone(), span));
} }
Value::Record { .. } => { Value::Record { .. } => {
map.insert( map.insert(

View File

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

View File

@ -5,7 +5,7 @@ use notify_debouncer_full::{
EventKind, RecursiveMode, Watcher, EventKind, RecursiveMode, Watcher,
}, },
}; };
use nu_engine::{command_prelude::*, current_dir, get_eval_block}; use nu_engine::{command_prelude::*, current_dir, ClosureEval};
use nu_protocol::{ use nu_protocol::{
engine::{Closure, StateWorkingSet}, engine::{Closure, StateWorkingSet},
format_error, format_error,
@ -72,6 +72,7 @@ impl Command for Watch {
call: &Call, call: &Call,
_input: PipelineData, _input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let head = call.head;
let cwd = current_dir(engine_state, stack)?; let cwd = current_dir(engine_state, stack)?;
let path_arg: Spanned<String> = call.req(engine_state, stack, 0)?; let path_arg: Spanned<String> = call.req(engine_state, stack, 0)?;
@ -89,11 +90,7 @@ impl Command for Watch {
} }
}; };
let capture_block: Closure = call.req(engine_state, stack, 1)?; let closure: Closure = call.req(engine_state, stack, 1)?;
let block = engine_state
.clone()
.get_block(capture_block.block_id)
.clone();
let verbose = call.has_flag(engine_state, stack, "verbose")?; let verbose = call.has_flag(engine_state, stack, "verbose")?;
@ -167,69 +164,43 @@ impl Command for Watch {
eprintln!("Now watching files at {path:?}. Press ctrl+c to abort."); eprintln!("Now watching files at {path:?}. Press ctrl+c to abort.");
let eval_block = get_eval_block(engine_state); let mut closure = ClosureEval::new(engine_state, stack, closure);
let event_handler = let mut event_handler = move |operation: &str,
|operation: &str, path: PathBuf, new_path: Option<PathBuf>| -> Result<(), ShellError> { path: PathBuf,
let glob_pattern = glob_pattern.clone(); new_path: Option<PathBuf>|
let matches_glob = match glob_pattern.clone() { -> Result<(), ShellError> {
Some(glob) => glob.matches_path(&path), let matches_glob = match &glob_pattern {
None => true, Some(glob) => glob.matches_path(&path),
}; None => true,
if verbose && glob_pattern.is_some() {
eprintln!("Matches glob: {matches_glob}");
}
if matches_glob {
let stack = &mut stack.clone();
if let Some(position) = block.signature.get_positional(0) {
if let Some(position_id) = &position.var_id {
stack.add_var(*position_id, Value::string(operation, call.span()));
}
}
if let Some(position) = block.signature.get_positional(1) {
if let Some(position_id) = &position.var_id {
stack.add_var(
*position_id,
Value::string(path.to_string_lossy(), call.span()),
);
}
}
if let Some(position) = block.signature.get_positional(2) {
if let Some(position_id) = &position.var_id {
stack.add_var(
*position_id,
Value::string(
new_path.unwrap_or_else(|| "".into()).to_string_lossy(),
call.span(),
),
);
}
}
let eval_result = eval_block(
engine_state,
stack,
&block,
Value::nothing(call.span()).into_pipeline_data(),
);
match eval_result {
Ok(val) => {
val.print(engine_state, stack, false, false)?;
}
Err(err) => {
let working_set = StateWorkingSet::new(engine_state);
eprintln!("{}", format_error(&working_set, &err));
}
}
}
Ok(())
}; };
if verbose && glob_pattern.is_some() {
eprintln!("Matches glob: {matches_glob}");
}
if matches_glob {
let result = closure
.add_arg(Value::string(operation, head))
.add_arg(Value::string(path.to_string_lossy(), head))
.add_arg(Value::string(
new_path.unwrap_or_else(|| "".into()).to_string_lossy(),
head,
))
.run_with_input(PipelineData::Empty);
match result {
Ok(val) => {
val.print(engine_state, stack, false, false)?;
}
Err(err) => {
let working_set = StateWorkingSet::new(engine_state);
eprintln!("{}", format_error(&working_set, &err));
}
}
}
Ok(())
};
loop { loop {
match rx.recv_timeout(CHECK_CTRL_C_FREQUENCY) { match rx.recv_timeout(CHECK_CTRL_C_FREQUENCY) {

View File

@ -1,5 +1,5 @@
use super::utils::chain_error_with_input; use super::utils::chain_error_with_input;
use nu_engine::{command_prelude::*, get_eval_block_with_early_return}; use nu_engine::{command_prelude::*, ClosureEval, ClosureEvalOnce};
use nu_protocol::engine::Closure; use nu_protocol::engine::Closure;
#[derive(Clone)] #[derive(Clone)]
@ -107,126 +107,79 @@ with 'transpose' first."#
call: &Call, call: &Call,
input: PipelineData, input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let eval_block_with_early_return = get_eval_block_with_early_return(engine_state); let head = call.head;
let closure: Closure = call.req(engine_state, stack, 0)?;
let capture_block: Closure = call.req(engine_state, stack, 0)?;
let keep_empty = call.has_flag(engine_state, stack, "keep-empty")?; let keep_empty = call.has_flag(engine_state, stack, "keep-empty")?;
let metadata = input.metadata(); let metadata = input.metadata();
let ctrlc = engine_state.ctrlc.clone();
let outer_ctrlc = engine_state.ctrlc.clone();
let engine_state = engine_state.clone();
let block = engine_state.get_block(capture_block.block_id).clone();
let mut stack = stack.captures_to_stack(capture_block.captures);
let orig_env_vars = stack.env_vars.clone();
let orig_env_hidden = stack.env_hidden.clone();
let span = call.head;
match input { match input {
PipelineData::Empty => Ok(PipelineData::Empty), PipelineData::Empty => Ok(PipelineData::Empty),
PipelineData::Value(Value::Range { .. }, ..) PipelineData::Value(Value::Range { .. }, ..)
| PipelineData::Value(Value::List { .. }, ..) | PipelineData::Value(Value::List { .. }, ..)
| PipelineData::ListStream { .. } => Ok(input | PipelineData::ListStream(..) => {
.into_iter() let mut closure = ClosureEval::new(engine_state, stack, closure);
.map_while(move |x| { Ok(input
// with_env() is used here to ensure that each iteration uses .into_iter()
// a different set of environment variables. .map_while(move |value| {
// Hence, a 'cd' in the first loop won't affect the next loop. let span = value.span();
stack.with_env(&orig_env_vars, &orig_env_hidden); let is_error = value.is_error();
match closure.run_with_value(value) {
if let Some(var) = block.signature.get_positional(0) { Ok(data) => Some(data.into_value(head)),
if let Some(var_id) = &var.var_id { Err(ShellError::Continue { span }) => Some(Value::nothing(span)),
stack.add_var(*var_id, x.clone()); Err(ShellError::Break { .. }) => None,
Err(error) => {
let error = chain_error_with_input(error, is_error, span);
Some(Value::error(error, span))
}
} }
} })
.into_pipeline_data(engine_state.ctrlc.clone()))
let input_span = x.span(); }
let x_is_error = x.is_error();
match eval_block_with_early_return(
&engine_state,
&mut stack,
&block,
x.into_pipeline_data(),
) {
Ok(v) => Some(v.into_value(span)),
Err(ShellError::Continue { span }) => Some(Value::nothing(span)),
Err(ShellError::Break { .. }) => None,
Err(error) => {
let error = chain_error_with_input(error, x_is_error, input_span);
Some(Value::error(error, input_span))
}
}
})
.into_pipeline_data(ctrlc)),
PipelineData::ExternalStream { stdout: None, .. } => Ok(PipelineData::empty()), PipelineData::ExternalStream { stdout: None, .. } => Ok(PipelineData::empty()),
PipelineData::ExternalStream { PipelineData::ExternalStream {
stdout: Some(stream), stdout: Some(stream),
.. ..
} => Ok(stream } => {
.into_iter() let mut closure = ClosureEval::new(engine_state, stack, closure);
.map_while(move |x| { Ok(stream
// with_env() is used here to ensure that each iteration uses .into_iter()
// a different set of environment variables. .map_while(move |value| {
// Hence, a 'cd' in the first loop won't affect the next loop. let value = match value {
stack.with_env(&orig_env_vars, &orig_env_hidden); Ok(value) => value,
Err(ShellError::Continue { span }) => {
return Some(Value::nothing(span))
}
Err(ShellError::Break { .. }) => return None,
Err(err) => return Some(Value::error(err, head)),
};
let x = match x { let span = value.span();
Ok(x) => x, let is_error = value.is_error();
Err(ShellError::Continue { span }) => return Some(Value::nothing(span)), match closure.run_with_value(value) {
Err(ShellError::Break { .. }) => return None, Ok(data) => Some(data.into_value(head)),
Err(err) => return Some(Value::error(err, span)), Err(ShellError::Continue { span }) => Some(Value::nothing(span)),
}; Err(ShellError::Break { .. }) => None,
Err(error) => {
if let Some(var) = block.signature.get_positional(0) { let error = chain_error_with_input(error, is_error, span);
if let Some(var_id) = &var.var_id { Some(Value::error(error, span))
stack.add_var(*var_id, x.clone()); }
} }
} })
.into_pipeline_data(engine_state.ctrlc.clone()))
let input_span = x.span(); }
let x_is_error = x.is_error();
match eval_block_with_early_return(
&engine_state,
&mut stack,
&block,
x.into_pipeline_data(),
) {
Ok(v) => Some(v.into_value(span)),
Err(ShellError::Continue { span }) => Some(Value::nothing(span)),
Err(ShellError::Break { .. }) => None,
Err(error) => {
let error = chain_error_with_input(error, x_is_error, input_span);
Some(Value::error(error, input_span))
}
}
})
.into_pipeline_data(ctrlc)),
// This match allows non-iterables to be accepted, // This match allows non-iterables to be accepted,
// which is currently considered undesirable (Nov 2022). // which is currently considered undesirable (Nov 2022).
PipelineData::Value(x, ..) => { PipelineData::Value(value, ..) => {
if let Some(var) = block.signature.get_positional(0) { ClosureEvalOnce::new(engine_state, stack, closure).run_with_value(value)
if let Some(var_id) = &var.var_id {
stack.add_var(*var_id, x.clone());
}
}
eval_block_with_early_return(
&engine_state,
&mut stack,
&block,
x.into_pipeline_data(),
)
} }
} }
.and_then(|x| { .and_then(|x| {
x.filter( x.filter(
move |x| if !keep_empty { !x.is_nothing() } else { true }, move |x| if !keep_empty { !x.is_nothing() } else { true },
outer_ctrlc, engine_state.ctrlc.clone(),
) )
}) })
.map(|x| x.set_metadata(metadata)) .map(|data| data.set_metadata(metadata))
} }
} }

View File

@ -1,5 +1,5 @@
use super::utils::chain_error_with_input; use super::utils::chain_error_with_input;
use nu_engine::{command_prelude::*, get_eval_block_with_early_return}; use nu_engine::{command_prelude::*, ClosureEval, ClosureEvalOnce};
use nu_protocol::engine::Closure; use nu_protocol::engine::Closure;
#[derive(Clone)] #[derive(Clone)]
@ -47,135 +47,71 @@ a variable. On the other hand, the "row condition" syntax is not supported."#
call: &Call, call: &Call,
input: PipelineData, input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let capture_block: Closure = call.req(engine_state, stack, 0)?; let head = call.head;
let metadata = input.metadata(); let closure: Closure = call.req(engine_state, stack, 0)?;
let ctrlc = engine_state.ctrlc.clone();
let engine_state = engine_state.clone();
let block = engine_state.get_block(capture_block.block_id).clone();
let mut stack = stack.captures_to_stack(capture_block.captures);
let orig_env_vars = stack.env_vars.clone();
let orig_env_hidden = stack.env_hidden.clone();
let span = call.head;
let eval_block_with_early_return = get_eval_block_with_early_return(&engine_state);
let metadata = input.metadata();
match input { match input {
PipelineData::Empty => Ok(PipelineData::Empty), PipelineData::Empty => Ok(PipelineData::Empty),
PipelineData::Value(Value::Range { .. }, ..) PipelineData::Value(Value::Range { .. }, ..)
| PipelineData::Value(Value::List { .. }, ..) | PipelineData::Value(Value::List { .. }, ..)
| PipelineData::ListStream { .. } => Ok(input | PipelineData::ListStream(..) => {
// To enumerate over the input (for the index argument), let mut closure = ClosureEval::new(engine_state, stack, closure);
// it must be converted into an iterator using into_iter(). Ok(input
.into_iter() .into_iter()
.filter_map(move |x| { .filter_map(move |value| match closure.run_with_value(value.clone()) {
// with_env() is used here to ensure that each iteration uses Ok(pred) => pred.into_value(head).is_true().then_some(value),
// a different set of environment variables. Err(err) => {
// Hence, a 'cd' in the first loop won't affect the next loop. let span = value.span();
stack.with_env(&orig_env_vars, &orig_env_hidden); let err = chain_error_with_input(err, value.is_error(), span);
Some(Value::error(err, span))
if let Some(var) = block.signature.get_positional(0) {
if let Some(var_id) = &var.var_id {
stack.add_var(*var_id, x.clone());
} }
} })
.into_pipeline_data(engine_state.ctrlc.clone()))
match eval_block_with_early_return( }
&engine_state,
&mut stack,
&block,
// clone() is used here because x is given to Ok() below.
x.clone().into_pipeline_data(),
) {
Ok(v) => {
if v.into_value(span).is_true() {
Some(x)
} else {
None
}
}
Err(error) => Some(Value::error(
chain_error_with_input(error, x.is_error(), x.span()),
x.span(),
)),
}
})
.into_pipeline_data(ctrlc)),
PipelineData::ExternalStream { stdout: None, .. } => Ok(PipelineData::empty()), PipelineData::ExternalStream { stdout: None, .. } => Ok(PipelineData::empty()),
PipelineData::ExternalStream { PipelineData::ExternalStream {
stdout: Some(stream), stdout: Some(stream),
.. ..
} => Ok(stream } => {
.into_iter() let mut closure = ClosureEval::new(engine_state, stack, closure);
.filter_map(move |x| { Ok(stream
// see note above about with_env() .into_iter()
stack.with_env(&orig_env_vars, &orig_env_hidden); .filter_map(move |value| {
let value = match value {
Ok(value) => value,
Err(err) => return Some(Value::error(err, head)),
};
let x = match x { match closure.run_with_value(value.clone()) {
Ok(x) => x, Ok(pred) => pred.into_value(head).is_true().then_some(value),
Err(err) => return Some(Value::error(err, span)), Err(err) => {
}; let span = value.span();
let err = chain_error_with_input(err, value.is_error(), span);
if let Some(var) = block.signature.get_positional(0) { Some(Value::error(err, span))
if let Some(var_id) = &var.var_id {
stack.add_var(*var_id, x.clone());
}
}
match eval_block_with_early_return(
&engine_state,
&mut stack,
&block,
// clone() is used here because x is given to Ok() below.
x.clone().into_pipeline_data(),
) {
Ok(v) => {
if v.into_value(span).is_true() {
Some(x)
} else {
None
} }
} }
Err(error) => Some(Value::error( })
chain_error_with_input(error, x.is_error(), x.span()), .into_pipeline_data(engine_state.ctrlc.clone()))
x.span(), }
)),
}
})
.into_pipeline_data(ctrlc)),
// This match allows non-iterables to be accepted, // This match allows non-iterables to be accepted,
// which is currently considered undesirable (Nov 2022). // which is currently considered undesirable (Nov 2022).
PipelineData::Value(x, ..) => { PipelineData::Value(value, ..) => {
// see note above about with_env() let result = ClosureEvalOnce::new(engine_state, stack, closure)
stack.with_env(&orig_env_vars, &orig_env_hidden); .run_with_value(value.clone());
if let Some(var) = block.signature.get_positional(0) { Ok(match result {
if let Some(var_id) = &var.var_id { Ok(pred) => pred.into_value(head).is_true().then_some(value),
stack.add_var(*var_id, x.clone()); Err(err) => {
let span = value.span();
let err = chain_error_with_input(err, value.is_error(), span);
Some(Value::error(err, span))
} }
} }
.into_pipeline_data(engine_state.ctrlc.clone()))
Ok(match eval_block_with_early_return(
&engine_state,
&mut stack,
&block,
// clone() is used here because x is given to Ok() below.
x.clone().into_pipeline_data(),
) {
Ok(v) => {
if v.into_value(span).is_true() {
Some(x)
} else {
None
}
}
Err(error) => Some(Value::error(
chain_error_with_input(error, x.is_error(), x.span()),
x.span(),
)),
}
.into_pipeline_data(ctrlc))
} }
} }
.map(|x| x.set_metadata(metadata)) .map(|data| data.set_metadata(metadata))
} }
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {

View File

@ -1,5 +1,5 @@
use indexmap::IndexMap; use indexmap::IndexMap;
use nu_engine::{command_prelude::*, get_eval_block}; use nu_engine::{command_prelude::*, ClosureEval};
use nu_protocol::engine::Closure; use nu_protocol::engine::Closure;
#[derive(Clone)] #[derive(Clone)]
@ -202,20 +202,13 @@ fn group_closure(
stack: &mut Stack, stack: &mut Stack,
) -> Result<IndexMap<String, Vec<Value>>, ShellError> { ) -> Result<IndexMap<String, Vec<Value>>, ShellError> {
let mut groups = IndexMap::<_, Vec<_>>::new(); let mut groups = IndexMap::<_, Vec<_>>::new();
let eval_block = get_eval_block(engine_state); let mut closure = ClosureEval::new(engine_state, stack, closure);
let block = engine_state.get_block(closure.block_id);
for value in values { for value in values {
let mut stack = stack.captures_to_stack(closure.captures.clone()); let key = closure
.run_with_value(value.clone())?
let key = eval_block( .into_value(span)
engine_state, .coerce_into_string()?;
&mut stack,
block,
value.clone().into_pipeline_data(),
)?
.into_value(span)
.coerce_into_string()?;
groups.entry(key).or_default().push(value); groups.entry(key).or_default().push(value);
} }

View File

@ -1,8 +1,5 @@
use nu_engine::{command_prelude::*, get_eval_block, EvalBlockFn}; use nu_engine::{command_prelude::*, ClosureEval, ClosureEvalOnce};
use nu_protocol::{ use nu_protocol::ast::PathMember;
ast::{Block, PathMember},
engine::Closure,
};
#[derive(Clone)] #[derive(Clone)]
pub struct Insert; pub struct Insert;
@ -127,51 +124,38 @@ fn insert(
call: &Call, call: &Call,
input: PipelineData, input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let span = call.head; let head = call.head;
let cell_path: CellPath = call.req(engine_state, stack, 0)?; let cell_path: CellPath = call.req(engine_state, stack, 0)?;
let replacement: Value = call.req(engine_state, stack, 1)?; let replacement: Value = call.req(engine_state, stack, 1)?;
let replacement_span = replacement.span();
let ctrlc = engine_state.ctrlc.clone();
let eval_block = get_eval_block(engine_state);
match input { match input {
PipelineData::Value(mut value, metadata) => { PipelineData::Value(mut value, metadata) => {
if let Value::Closure { val: closure, .. } = replacement { if let Value::Closure { val, .. } = replacement {
match (cell_path.members.first(), &mut value) { match (cell_path.members.first(), &mut value) {
(Some(PathMember::String { .. }), Value::List { vals, .. }) => { (Some(PathMember::String { .. }), Value::List { vals, .. }) => {
let block = engine_state.get_block(closure.block_id); let mut closure = ClosureEval::new(engine_state, stack, val);
let stack = stack.captures_to_stack(closure.captures);
for val in vals { for val in vals {
let mut stack = stack.clone();
insert_value_by_closure( insert_value_by_closure(
val, val,
replacement_span, &mut closure,
engine_state, head,
&mut stack,
block,
&cell_path.members, &cell_path.members,
false, false,
eval_block,
)?; )?;
} }
} }
(first, _) => { (first, _) => {
insert_single_value_by_closure( insert_single_value_by_closure(
&mut value, &mut value,
closure, ClosureEvalOnce::new(engine_state, stack, val),
replacement_span, head,
engine_state,
stack,
&cell_path.members, &cell_path.members,
matches!(first, Some(PathMember::Int { .. })), matches!(first, Some(PathMember::Int { .. })),
eval_block,
)?; )?;
} }
} }
} else { } else {
value.insert_data_at_cell_path(&cell_path.members, replacement, span)?; value.insert_data_at_cell_path(&cell_path.members, replacement, head)?;
} }
Ok(value.into_pipeline_data_with_metadata(metadata)) Ok(value.into_pipeline_data_with_metadata(metadata))
} }
@ -199,27 +183,15 @@ fn insert(
} }
if path.is_empty() { if path.is_empty() {
if let Value::Closure { val: closure, .. } = replacement { if let Value::Closure { val, .. } = replacement {
let value = stream.next(); let value = stream.next();
let end_of_stream = value.is_none(); let end_of_stream = value.is_none();
let value = value.unwrap_or(Value::nothing(replacement_span)); let value = value.unwrap_or(Value::nothing(head));
let block = engine_state.get_block(closure.block_id); let new_value = ClosureEvalOnce::new(engine_state, stack, val)
let mut stack = stack.captures_to_stack(closure.captures); .run_with_value(value.clone())?
.into_value(head);
if let Some(var) = block.signature.get_positional(0) { pre_elems.push(new_value);
if let Some(var_id) = &var.var_id {
stack.add_var(*var_id, value.clone())
}
}
let output = eval_block(
engine_state,
&mut stack,
block,
value.clone().into_pipeline_data(),
)?;
pre_elems.push(output.into_value(replacement_span));
if !end_of_stream { if !end_of_stream {
pre_elems.push(value); pre_elems.push(value);
} }
@ -227,19 +199,16 @@ fn insert(
pre_elems.push(replacement); pre_elems.push(replacement);
} }
} else if let Some(mut value) = stream.next() { } else if let Some(mut value) = stream.next() {
if let Value::Closure { val: closure, .. } = replacement { if let Value::Closure { val, .. } = replacement {
insert_single_value_by_closure( insert_single_value_by_closure(
&mut value, &mut value,
closure, ClosureEvalOnce::new(engine_state, stack, val),
replacement_span, head,
engine_state,
stack,
path, path,
true, true,
eval_block,
)?; )?;
} else { } else {
value.insert_data_at_cell_path(path, replacement, span)?; value.insert_data_at_cell_path(path, replacement, head)?;
} }
pre_elems.push(value) pre_elems.push(value)
} else { } else {
@ -252,75 +221,61 @@ fn insert(
Ok(pre_elems Ok(pre_elems
.into_iter() .into_iter()
.chain(stream) .chain(stream)
.into_pipeline_data_with_metadata(metadata, ctrlc)) .into_pipeline_data_with_metadata(metadata, engine_state.ctrlc.clone()))
} else if let Value::Closure { val: closure, .. } = replacement { } else if let Value::Closure { val, .. } = replacement {
let engine_state = engine_state.clone(); let mut closure = ClosureEval::new(engine_state, stack, val);
let block = engine_state.get_block(closure.block_id).clone();
let stack = stack.captures_to_stack(closure.captures);
Ok(stream Ok(stream
.map(move |mut input| { .map(move |mut value| {
// Recreate the stack for each iteration to
// isolate environment variable changes, etc.
let mut stack = stack.clone();
let err = insert_value_by_closure( let err = insert_value_by_closure(
&mut input, &mut value,
replacement_span, &mut closure,
&engine_state, head,
&mut stack,
&block,
&cell_path.members, &cell_path.members,
false, false,
eval_block,
); );
if let Err(e) = err { if let Err(e) = err {
Value::error(e, span) Value::error(e, head)
} else { } else {
input value
} }
}) })
.into_pipeline_data_with_metadata(metadata, ctrlc)) .into_pipeline_data_with_metadata(metadata, engine_state.ctrlc.clone()))
} else { } else {
Ok(stream Ok(stream
.map(move |mut input| { .map(move |mut value| {
if let Err(e) = input.insert_data_at_cell_path( if let Err(e) = value.insert_data_at_cell_path(
&cell_path.members, &cell_path.members,
replacement.clone(), replacement.clone(),
span, head,
) { ) {
Value::error(e, span) Value::error(e, head)
} else { } else {
input value
} }
}) })
.into_pipeline_data_with_metadata(metadata, ctrlc)) .into_pipeline_data_with_metadata(metadata, engine_state.ctrlc.clone()))
} }
} }
PipelineData::Empty => Err(ShellError::IncompatiblePathAccess { PipelineData::Empty => Err(ShellError::IncompatiblePathAccess {
type_name: "empty pipeline".to_string(), type_name: "empty pipeline".to_string(),
span, span: head,
}), }),
PipelineData::ExternalStream { .. } => Err(ShellError::IncompatiblePathAccess { PipelineData::ExternalStream { .. } => Err(ShellError::IncompatiblePathAccess {
type_name: "external stream".to_string(), type_name: "external stream".to_string(),
span, span: head,
}), }),
} }
} }
#[allow(clippy::too_many_arguments)]
fn insert_value_by_closure( fn insert_value_by_closure(
value: &mut Value, value: &mut Value,
closure: &mut ClosureEval,
span: Span, span: Span,
engine_state: &EngineState,
stack: &mut Stack,
block: &Block,
cell_path: &[PathMember], cell_path: &[PathMember],
first_path_member_int: bool, first_path_member_int: bool,
eval_block_fn: EvalBlockFn,
) -> Result<(), ShellError> { ) -> Result<(), ShellError> {
let input = if first_path_member_int { let value_at_path = if first_path_member_int {
value value
.clone() .clone()
.follow_cell_path(cell_path, false) .follow_cell_path(cell_path, false)
@ -329,41 +284,28 @@ fn insert_value_by_closure(
value.clone() value.clone()
}; };
if let Some(var) = block.signature.get_positional(0) { let new_value = closure.run_with_value(value_at_path)?.into_value(span);
if let Some(var_id) = var.var_id { value.insert_data_at_cell_path(cell_path, new_value, span)
stack.add_var(var_id, input.clone());
}
}
let output = eval_block_fn(engine_state, stack, block, input.into_pipeline_data())?;
value.insert_data_at_cell_path(cell_path, output.into_value(span), span)
} }
#[allow(clippy::too_many_arguments)]
fn insert_single_value_by_closure( fn insert_single_value_by_closure(
value: &mut Value, value: &mut Value,
closure: Closure, closure: ClosureEvalOnce,
span: Span, span: Span,
engine_state: &EngineState,
stack: &mut Stack,
cell_path: &[PathMember], cell_path: &[PathMember],
first_path_member_int: bool, first_path_member_int: bool,
eval_block_fn: EvalBlockFn,
) -> Result<(), ShellError> { ) -> Result<(), ShellError> {
let block = engine_state.get_block(closure.block_id); let value_at_path = if first_path_member_int {
let mut stack = stack.captures_to_stack(closure.captures); value
.clone()
.follow_cell_path(cell_path, false)
.unwrap_or(Value::nothing(span))
} else {
value.clone()
};
insert_value_by_closure( let new_value = closure.run_with_value(value_at_path)?.into_value(span);
value, value.insert_data_at_cell_path(cell_path, new_value, span)
span,
engine_state,
&mut stack,
block,
cell_path,
first_path_member_int,
eval_block_fn,
)
} }
#[cfg(test)] #[cfg(test)]

View File

@ -1,4 +1,4 @@
use nu_engine::{command_prelude::*, get_eval_block_with_early_return}; use nu_engine::{command_prelude::*, ClosureEvalOnce};
use nu_protocol::engine::Closure; use nu_protocol::engine::Closure;
use std::{sync::mpsc, thread}; use std::{sync::mpsc, thread};
@ -106,23 +106,21 @@ interleave
call: &Call, call: &Call,
input: PipelineData, input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let head = call.head;
let closures: Vec<Closure> = call.rest(engine_state, stack, 0)?;
let buffer_size: usize = call let buffer_size: usize = call
.get_flag(engine_state, stack, "buffer-size")? .get_flag(engine_state, stack, "buffer-size")?
.unwrap_or(0); .unwrap_or(0);
let (tx, rx) = mpsc::sync_channel(buffer_size);
let closures: Vec<Closure> = call.rest(engine_state, stack, 0)?; let (tx, rx) = mpsc::sync_channel(buffer_size);
let eval_block_with_early_return = get_eval_block_with_early_return(engine_state);
// Spawn the threads for the input and closure outputs // Spawn the threads for the input and closure outputs
(!input.is_nothing()) (!input.is_nothing())
.then(|| Ok(input)) .then(|| Ok(input))
.into_iter() .into_iter()
.chain(closures.into_iter().map(|closure| { .chain(closures.into_iter().map(|closure| {
// Evaluate the closure on this thread ClosureEvalOnce::new(engine_state, stack, closure)
let block = engine_state.get_block(closure.block_id); .run_with_input(PipelineData::Empty)
let mut stack = stack.captures_to_stack(closure.captures);
eval_block_with_early_return(engine_state, &mut stack, block, PipelineData::Empty)
})) }))
.try_for_each(|stream| { .try_for_each(|stream| {
stream.and_then(|stream| { stream.and_then(|stream| {
@ -141,7 +139,7 @@ interleave
.map(|_| ()) .map(|_| ())
.map_err(|err| ShellError::IOErrorSpanned { .map_err(|err| ShellError::IOErrorSpanned {
msg: err.to_string(), msg: err.to_string(),
span: call.head, span: head,
}) })
}) })
})?; })?;

View File

@ -1,5 +1,5 @@
use super::utils::chain_error_with_input; use super::utils::chain_error_with_input;
use nu_engine::{command_prelude::*, get_eval_block_with_early_return}; use nu_engine::{command_prelude::*, ClosureEval};
use nu_protocol::engine::Closure; use nu_protocol::engine::Closure;
#[derive(Clone)] #[derive(Clone)]
@ -37,96 +37,68 @@ impl Command for Items {
call: &Call, call: &Call,
input: PipelineData, input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let capture_block: Closure = call.req(engine_state, stack, 0)?; let head = call.head;
let closure: Closure = call.req(engine_state, stack, 0)?;
let metadata = input.metadata(); let metadata = input.metadata();
let ctrlc = engine_state.ctrlc.clone();
let engine_state = engine_state.clone();
let block = engine_state.get_block(capture_block.block_id).clone();
let mut stack = stack.captures_to_stack(capture_block.captures);
let orig_env_vars = stack.env_vars.clone();
let orig_env_hidden = stack.env_hidden.clone();
let span = call.head;
let eval_block_with_early_return = get_eval_block_with_early_return(&engine_state);
let input_span = input.span().unwrap_or(call.head);
let run_for_each_item = move |keyval: (String, Value)| -> Option<Value> {
// with_env() is used here to ensure that each iteration uses
// a different set of environment variables.
// Hence, a 'cd' in the first loop won't affect the next loop.
stack.with_env(&orig_env_vars, &orig_env_hidden);
if let Some(var) = block.signature.get_positional(0) {
if let Some(var_id) = &var.var_id {
stack.add_var(*var_id, Value::string(keyval.0.clone(), span));
}
}
if let Some(var) = block.signature.get_positional(1) {
if let Some(var_id) = &var.var_id {
stack.add_var(*var_id, keyval.1);
}
}
match eval_block_with_early_return(
&engine_state,
&mut stack,
&block,
PipelineData::empty(),
) {
Ok(v) => Some(v.into_value(span)),
Err(ShellError::Break { .. }) => None,
Err(error) => {
let error = chain_error_with_input(error, false, input_span);
Some(Value::error(error, span))
}
}
};
match input { match input {
PipelineData::Empty => Ok(PipelineData::Empty), PipelineData::Empty => Ok(PipelineData::Empty),
PipelineData::Value(v, ..) => match v { PipelineData::Value(value, ..) => {
Value::Record { val, .. } => Ok(val let value = if let Value::LazyRecord { val, .. } = value {
.into_owned() val.collect()?
.into_iter() } else {
.map_while(run_for_each_item) value
.into_pipeline_data(ctrlc)), };
Value::LazyRecord { val, .. } => {
let record = match val.collect()? { let span = value.span();
Value::Record { val, .. } => val, match value {
_ => Err(ShellError::NushellFailedSpanned { Value::Record { val, .. } => {
msg: "`LazyRecord::collect()` promises `Value::Record`".into(), let mut closure = ClosureEval::new(engine_state, stack, closure);
label: "Violating lazy record found here".into(), Ok(val
span, .into_owned()
})?, .into_iter()
}; .map_while(move |(col, val)| {
Ok(record let result = closure
.into_owned() .add_arg(Value::string(col, span))
.into_iter() .add_arg(val)
.map_while(run_for_each_item) .run_with_input(PipelineData::Empty);
.into_pipeline_data(ctrlc))
match result {
Ok(data) => Some(data.into_value(head)),
Err(ShellError::Break { .. }) => None,
Err(err) => {
let err = chain_error_with_input(err, false, span);
Some(Value::error(err, head))
}
}
})
.into_pipeline_data(engine_state.ctrlc.clone()))
}
Value::Error { error, .. } => Err(*error),
other => Err(ShellError::OnlySupportsThisInputType {
exp_input_type: "record".into(),
wrong_type: other.get_type().to_string(),
dst_span: head,
src_span: other.span(),
}),
} }
Value::Error { error, .. } => Err(*error), }
other => Err(ShellError::OnlySupportsThisInputType {
exp_input_type: "record".into(),
wrong_type: other.get_type().to_string(),
dst_span: call.head,
src_span: other.span(),
}),
},
PipelineData::ListStream(..) => Err(ShellError::OnlySupportsThisInputType { PipelineData::ListStream(..) => Err(ShellError::OnlySupportsThisInputType {
exp_input_type: "record".into(), exp_input_type: "record".into(),
wrong_type: "stream".into(), wrong_type: "stream".into(),
dst_span: call.head, dst_span: head,
src_span: input_span, src_span: head,
}),
PipelineData::ExternalStream { .. } => Err(ShellError::OnlySupportsThisInputType {
exp_input_type: "record".into(),
wrong_type: "raw data".into(),
dst_span: call.head,
src_span: input_span,
}), }),
PipelineData::ExternalStream { span, .. } => {
Err(ShellError::OnlySupportsThisInputType {
exp_input_type: "record".into(),
wrong_type: "raw data".into(),
dst_span: head,
src_span: span,
})
}
} }
.map(|x| x.set_metadata(metadata)) .map(|data| data.set_metadata(metadata))
} }
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {

View File

@ -1,9 +1,8 @@
use nu_engine::{command_prelude::*, get_eval_block_with_early_return}; use super::utils::chain_error_with_input;
use nu_engine::{command_prelude::*, ClosureEvalOnce};
use nu_protocol::engine::Closure; use nu_protocol::engine::Closure;
use rayon::prelude::*; use rayon::prelude::*;
use super::utils::chain_error_with_input;
#[derive(Clone)] #[derive(Clone)]
pub struct ParEach; pub struct ParEach;
@ -113,16 +112,13 @@ impl Command for ParEach {
} }
} }
let capture_block: Closure = call.req(engine_state, stack, 0)?; let head = call.head;
let closure: Closure = call.req(engine_state, stack, 0)?;
let threads: Option<usize> = call.get_flag(engine_state, stack, "threads")?; let threads: Option<usize> = call.get_flag(engine_state, stack, "threads")?;
let max_threads = threads.unwrap_or(0); let max_threads = threads.unwrap_or(0);
let keep_order = call.has_flag(engine_state, stack, "keep-order")?; let keep_order = call.has_flag(engine_state, stack, "keep-order")?;
let metadata = input.metadata(); let metadata = input.metadata();
let ctrlc = engine_state.ctrlc.clone();
let outer_ctrlc = engine_state.ctrlc.clone();
let block_id = capture_block.block_id;
let mut stack = stack.captures_to_stack(capture_block.captures);
let span = call.head;
// A helper function sorts the output if needed // A helper function sorts the output if needed
let apply_order = |mut vec: Vec<(usize, Value)>| { let apply_order = |mut vec: Vec<(usize, Value)>| {
@ -135,8 +131,6 @@ impl Command for ParEach {
vec.into_iter().map(|(_, val)| val) vec.into_iter().map(|(_, val)| val)
}; };
let eval_block_with_early_return = get_eval_block_with_early_return(engine_state);
match input { match input {
PipelineData::Empty => Ok(PipelineData::Empty), PipelineData::Empty => Ok(PipelineData::Empty),
PipelineData::Value(value, ..) => { PipelineData::Value(value, ..) => {
@ -144,74 +138,51 @@ impl Command for ParEach {
match value { match value {
Value::List { vals, .. } => Ok(create_pool(max_threads)?.install(|| { Value::List { vals, .. } => Ok(create_pool(max_threads)?.install(|| {
let vec = vals let vec = vals
.par_iter() .into_par_iter()
.enumerate() .enumerate()
.map(move |(index, x)| { .map(move |(index, value)| {
let block = engine_state.get_block(block_id); let span = value.span();
let is_error = value.is_error();
let result =
ClosureEvalOnce::new(engine_state, stack, closure.clone())
.run_with_value(value);
let mut stack = stack.clone(); let value = match result {
Ok(data) => data.into_value(span),
if let Some(var) = block.signature.get_positional(0) { Err(err) => Value::error(
if let Some(var_id) = &var.var_id { chain_error_with_input(err, is_error, span),
stack.add_var(*var_id, x.clone()); span,
}
}
let val_span = x.span();
let x_is_error = x.is_error();
let val = match eval_block_with_early_return(
engine_state,
&mut stack,
block,
x.clone().into_pipeline_data(),
) {
Ok(v) => v.into_value(span),
Err(error) => Value::error(
chain_error_with_input(error, x_is_error, val_span),
val_span,
), ),
}; };
(index, val) (index, value)
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
apply_order(vec).into_pipeline_data(ctrlc) apply_order(vec).into_pipeline_data(engine_state.ctrlc.clone())
})), })),
Value::Range { val, .. } => Ok(create_pool(max_threads)?.install(|| { Value::Range { val, .. } => Ok(create_pool(max_threads)?.install(|| {
let ctrlc = engine_state.ctrlc.clone();
let vec = val let vec = val
.into_range_iter(span, ctrlc.clone()) .into_range_iter(span, ctrlc.clone())
.enumerate() .enumerate()
.par_bridge() .par_bridge()
.map(move |(index, x)| { .map(move |(index, value)| {
let block = engine_state.get_block(block_id); let span = value.span();
let is_error = value.is_error();
let result =
ClosureEvalOnce::new(engine_state, stack, closure.clone())
.run_with_value(value);
let mut stack = stack.clone(); let value = match result {
Ok(data) => data.into_value(span),
if let Some(var) = block.signature.get_positional(0) { Err(err) => Value::error(
if let Some(var_id) = &var.var_id { chain_error_with_input(err, is_error, span),
stack.add_var(*var_id, x.clone()); span,
}
}
let val_span = x.span();
let x_is_error = x.is_error();
let val = match eval_block_with_early_return(
engine_state,
&mut stack,
block,
x.into_pipeline_data(),
) {
Ok(v) => v.into_value(span),
Err(error) => Value::error(
chain_error_with_input(error, x_is_error, val_span),
val_span,
), ),
}; };
(index, val) (index, value)
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
@ -220,20 +191,7 @@ impl Command for ParEach {
// This match allows non-iterables to be accepted, // This match allows non-iterables to be accepted,
// which is currently considered undesirable (Nov 2022). // which is currently considered undesirable (Nov 2022).
value => { value => {
let block = engine_state.get_block(block_id); ClosureEvalOnce::new(engine_state, stack, closure).run_with_value(value)
if let Some(var) = block.signature.get_positional(0) {
if let Some(var_id) = &var.var_id {
stack.add_var(*var_id, value.clone());
}
}
eval_block_with_early_return(
engine_state,
&mut stack,
block,
value.into_pipeline_data(),
)
} }
} }
} }
@ -241,38 +199,24 @@ impl Command for ParEach {
let vec = stream let vec = stream
.enumerate() .enumerate()
.par_bridge() .par_bridge()
.map(move |(index, x)| { .map(move |(index, value)| {
let block = engine_state.get_block(block_id); let span = value.span();
let is_error = value.is_error();
let result = ClosureEvalOnce::new(engine_state, stack, closure.clone())
.run_with_value(value);
let mut stack = stack.clone(); let value = match result {
Ok(data) => data.into_value(head),
if let Some(var) = block.signature.get_positional(0) { Err(err) => {
if let Some(var_id) = &var.var_id { Value::error(chain_error_with_input(err, is_error, span), span)
stack.add_var(*var_id, x.clone());
} }
}
let val_span = x.span();
let x_is_error = x.is_error();
let val = match eval_block_with_early_return(
engine_state,
&mut stack,
block,
x.into_pipeline_data(),
) {
Ok(v) => v.into_value(span),
Err(error) => Value::error(
chain_error_with_input(error, x_is_error, val_span),
val_span,
),
}; };
(index, val) (index, value)
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
apply_order(vec).into_pipeline_data(ctrlc) apply_order(vec).into_pipeline_data(engine_state.ctrlc.clone())
})), })),
PipelineData::ExternalStream { stdout: None, .. } => Ok(PipelineData::empty()), PipelineData::ExternalStream { stdout: None, .. } => Ok(PipelineData::empty()),
PipelineData::ExternalStream { PipelineData::ExternalStream {
@ -282,41 +226,26 @@ impl Command for ParEach {
let vec = stream let vec = stream
.enumerate() .enumerate()
.par_bridge() .par_bridge()
.map(move |(index, x)| { .map(move |(index, value)| {
let x = match x { let value = match value {
Ok(x) => x, Ok(value) => value,
Err(err) => return (index, Value::error(err, span)), Err(err) => return (index, Value::error(err, head)),
}; };
let block = engine_state.get_block(block_id); let value = ClosureEvalOnce::new(engine_state, stack, closure.clone())
.run_with_value(value)
.map(|data| data.into_value(head))
.unwrap_or_else(|err| Value::error(err, head));
let mut stack = stack.clone(); (index, value)
if let Some(var) = block.signature.get_positional(0) {
if let Some(var_id) = &var.var_id {
stack.add_var(*var_id, x.clone());
}
}
let val = match eval_block_with_early_return(
engine_state,
&mut stack,
block,
x.into_pipeline_data(),
) {
Ok(v) => v.into_value(span),
Err(error) => Value::error(error, span),
};
(index, val)
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
apply_order(vec).into_pipeline_data(ctrlc) apply_order(vec).into_pipeline_data(engine_state.ctrlc.clone())
})), })),
} }
.and_then(|x| x.filter(|v| !v.is_nothing(), outer_ctrlc)) .and_then(|x| x.filter(|v| !v.is_nothing(), engine_state.ctrlc.clone()))
.map(|res| res.set_metadata(metadata)) .map(|data| data.set_metadata(metadata))
} }
} }

View File

@ -1,4 +1,4 @@
use nu_engine::{command_prelude::*, get_eval_block_with_early_return}; use nu_engine::{command_prelude::*, ClosureEval};
use nu_protocol::engine::Closure; use nu_protocol::engine::Closure;
#[derive(Clone)] #[derive(Clone)]
@ -88,72 +88,37 @@ impl Command for Reduce {
call: &Call, call: &Call,
input: PipelineData, input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let span = call.head; let head = call.head;
let fold: Option<Value> = call.get_flag(engine_state, stack, "fold")?; let fold: Option<Value> = call.get_flag(engine_state, stack, "fold")?;
let capture_block: Closure = call.req(engine_state, stack, 0)?; let closure: Closure = call.req(engine_state, stack, 0)?;
let mut stack = stack.captures_to_stack(capture_block.captures);
let block = engine_state.get_block(capture_block.block_id);
let ctrlc = engine_state.ctrlc.clone();
let eval_block_with_early_return = get_eval_block_with_early_return(engine_state);
let orig_env_vars = stack.env_vars.clone(); let mut iter = input.into_iter();
let orig_env_hidden = stack.env_hidden.clone();
// To enumerate over the input (for the index argument), let mut acc = fold
// it must be converted into an iterator using into_iter(). .or_else(|| iter.next())
let mut input_iter = input.into_iter(); .ok_or_else(|| ShellError::GenericError {
let start_val = if let Some(val) = fold {
val
} else if let Some(val) = input_iter.next() {
val
} else {
return Err(ShellError::GenericError {
error: "Expected input".into(), error: "Expected input".into(),
msg: "needs input".into(), msg: "needs input".into(),
span: Some(span), span: Some(head),
help: None, help: None,
inner: vec![], inner: vec![],
}); })?;
};
let mut acc = start_val; let mut closure = ClosureEval::new(engine_state, stack, closure);
for x in input_iter { for value in iter {
// with_env() is used here to ensure that each iteration uses if nu_utils::ctrl_c::was_pressed(&engine_state.ctrlc) {
// a different set of environment variables.
// Hence, a 'cd' in the first loop won't affect the next loop.
stack.with_env(&orig_env_vars, &orig_env_hidden);
// Element argument
if let Some(var) = block.signature.get_positional(0) {
if let Some(var_id) = &var.var_id {
stack.add_var(*var_id, x);
}
}
// Accumulator argument
if let Some(var) = block.signature.get_positional(1) {
if let Some(var_id) = &var.var_id {
stack.add_var(*var_id, acc);
}
}
acc = eval_block_with_early_return(
engine_state,
&mut stack,
block,
PipelineData::empty(),
)?
.into_value(span);
if nu_utils::ctrl_c::was_pressed(&ctrlc) {
break; break;
} }
acc = closure
.add_arg(value)
.add_arg(acc)
.run_with_input(PipelineData::Empty)?
.into_value(head);
} }
Ok(acc.with_span(span).into_pipeline_data()) Ok(acc.with_span(head).into_pipeline_data())
} }
} }

View File

@ -1,5 +1,5 @@
use indexmap::IndexMap; use indexmap::IndexMap;
use nu_engine::{command_prelude::*, get_eval_block_with_early_return}; use nu_engine::{command_prelude::*, ClosureEval};
use nu_protocol::engine::Closure; use nu_protocol::engine::Closure;
#[derive(Clone)] #[derive(Clone)]
@ -104,6 +104,8 @@ fn rename(
call: &Call, call: &Call,
input: PipelineData, input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let head = call.head;
let columns: Vec<String> = call.rest(engine_state, stack, 0)?;
let specified_column: Option<Record> = call.get_flag(engine_state, stack, "column")?; let specified_column: Option<Record> = call.get_flag(engine_state, stack, "column")?;
// convert from Record to HashMap for easily query. // convert from Record to HashMap for easily query.
let specified_column: Option<IndexMap<String, String>> = match specified_column { let specified_column: Option<IndexMap<String, String>> = match specified_column {
@ -133,24 +135,11 @@ fn rename(
} }
None => None, None => None,
}; };
let block_info = let closure: Option<Closure> = call.get_flag(engine_state, stack, "block")?;
if let Some(capture_block) = call.get_flag::<Closure>(engine_state, stack, "block")? {
let engine_state = engine_state.clone(); let mut closure = closure.map(|closure| ClosureEval::new(engine_state, stack, closure));
let block = engine_state.get_block(capture_block.block_id).clone();
let stack = stack.captures_to_stack(capture_block.captures);
let orig_env_vars = stack.env_vars.clone();
let orig_env_hidden = stack.env_hidden.clone();
Some((engine_state, block, stack, orig_env_vars, orig_env_hidden))
} else {
None
};
let columns: Vec<String> = call.rest(engine_state, stack, 0)?;
let metadata = input.metadata(); let metadata = input.metadata();
let eval_block_with_early_return = get_eval_block_with_early_return(engine_state);
let head_span = call.head;
input input
.map( .map(
move |item| { move |item| {
@ -158,31 +147,14 @@ fn rename(
match item { match item {
Value::Record { val: record, .. } => { Value::Record { val: record, .. } => {
let record = let record =
if let Some((engine_state, block, mut stack, env_vars, env_hidden)) = if let Some(closure) = &mut closure {
block_info.clone()
{
record record
.into_owned().into_iter() .into_owned().into_iter()
.map(|(col, val)| { .map(|(col, val)| {
stack.with_env(&env_vars, &env_hidden); let col = Value::string(col, span);
let data = closure.run_with_value(col)?;
if let Some(var) = block.signature.get_positional(0) { let col = data.collect_string_strict(span)?.0;
if let Some(var_id) = &var.var_id { Ok((col, val))
stack.add_var(
*var_id,
Value::string(col.clone(), span),
)
}
}
eval_block_with_early_return(
&engine_state,
&mut stack,
&block,
Value::string(col, span).into_pipeline_data(),
)
.and_then(|data| data.collect_string_strict(span))
.map(|(col, _, _)| (col, val))
}) })
.collect::<Result<Record, _>>() .collect::<Result<Record, _>>()
} else { } else {
@ -214,7 +186,7 @@ fn rename(
Err(ShellError::UnsupportedInput { Err(ShellError::UnsupportedInput {
msg: format!("The column '{missing}' does not exist in the input"), msg: format!("The column '{missing}' does not exist in the input"),
input: "value originated from here".into(), input: "value originated from here".into(),
msg_span: head_span, msg_span: head,
input_span: span, input_span: span,
}) })
} else { } else {
@ -242,16 +214,16 @@ fn rename(
ShellError::OnlySupportsThisInputType { ShellError::OnlySupportsThisInputType {
exp_input_type: "record".into(), exp_input_type: "record".into(),
wrong_type: other.get_type().to_string(), wrong_type: other.get_type().to_string(),
dst_span: head_span, dst_span: head,
src_span: other.span(), src_span: other.span(),
}, },
head_span, head,
), ),
} }
}, },
engine_state.ctrlc.clone(), engine_state.ctrlc.clone(),
) )
.map(|x| x.set_metadata(metadata)) .map(|data| data.set_metadata(metadata))
} }
#[cfg(test)] #[cfg(test)]

View File

@ -1,4 +1,4 @@
use nu_engine::{command_prelude::*, get_eval_block}; use nu_engine::{command_prelude::*, ClosureEval};
use nu_protocol::engine::Closure; use nu_protocol::engine::Closure;
#[derive(Clone)] #[derive(Clone)]
@ -74,33 +74,21 @@ impl Command for SkipUntil {
call: &Call, call: &Call,
input: PipelineData, input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let span = call.head; let head = call.head;
let closure: Closure = call.req(engine_state, stack, 0)?;
let mut closure = ClosureEval::new(engine_state, stack, closure);
let metadata = input.metadata(); let metadata = input.metadata();
let capture_block: Closure = call.req(engine_state, stack, 0)?;
let block = engine_state.get_block(capture_block.block_id).clone();
let var_id = block.signature.get_positional(0).and_then(|arg| arg.var_id);
let mut stack = stack.captures_to_stack(capture_block.captures);
let ctrlc = engine_state.ctrlc.clone();
let engine_state = engine_state.clone();
let eval_block = get_eval_block(&engine_state);
Ok(input Ok(input
.into_iter_strict(span)? .into_iter_strict(head)?
.skip_while(move |value| { .skip_while(move |value| {
if let Some(var_id) = var_id { closure
stack.add_var(var_id, value.clone()); .run_with_value(value.clone())
} .map(|data| data.into_value(head).is_false())
.unwrap_or(false)
!eval_block(&engine_state, &mut stack, &block, PipelineData::empty())
.map_or(false, |pipeline_data| {
pipeline_data.into_value(span).is_true()
})
}) })
.into_pipeline_data_with_metadata(metadata, ctrlc)) .into_pipeline_data_with_metadata(metadata, engine_state.ctrlc.clone()))
} }
} }

View File

@ -1,4 +1,4 @@
use nu_engine::{command_prelude::*, get_eval_block}; use nu_engine::{command_prelude::*, ClosureEval};
use nu_protocol::engine::Closure; use nu_protocol::engine::Closure;
#[derive(Clone)] #[derive(Clone)]
@ -79,33 +79,21 @@ impl Command for SkipWhile {
call: &Call, call: &Call,
input: PipelineData, input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let span = call.head; let head = call.head;
let closure: Closure = call.req(engine_state, stack, 0)?;
let mut closure = ClosureEval::new(engine_state, stack, closure);
let metadata = input.metadata(); let metadata = input.metadata();
let capture_block: Closure = call.req(engine_state, stack, 0)?;
let block = engine_state.get_block(capture_block.block_id).clone();
let var_id = block.signature.get_positional(0).and_then(|arg| arg.var_id);
let mut stack = stack.captures_to_stack(capture_block.captures);
let ctrlc = engine_state.ctrlc.clone();
let engine_state = engine_state.clone();
let eval_block = get_eval_block(&engine_state);
Ok(input Ok(input
.into_iter_strict(span)? .into_iter_strict(head)?
.skip_while(move |value| { .skip_while(move |value| {
if let Some(var_id) = var_id { closure
stack.add_var(var_id, value.clone()); .run_with_value(value.clone())
} .map(|data| data.into_value(head).is_true())
.unwrap_or(false)
eval_block(&engine_state, &mut stack, &block, PipelineData::empty())
.map_or(false, |pipeline_data| {
pipeline_data.into_value(span).is_true()
})
}) })
.into_pipeline_data_with_metadata(metadata, ctrlc)) .into_pipeline_data_with_metadata(metadata, engine_state.ctrlc.clone()))
} }
} }

View File

@ -1,4 +1,4 @@
use nu_engine::{command_prelude::*, get_eval_block}; use nu_engine::{command_prelude::*, ClosureEval};
use nu_protocol::engine::Closure; use nu_protocol::engine::Closure;
#[derive(Clone)] #[derive(Clone)]
@ -70,34 +70,21 @@ impl Command for TakeUntil {
call: &Call, call: &Call,
input: PipelineData, input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let head = call.head;
let closure: Closure = call.req(engine_state, stack, 0)?;
let mut closure = ClosureEval::new(engine_state, stack, closure);
let metadata = input.metadata(); let metadata = input.metadata();
let span = call.head;
let capture_block: Closure = call.req(engine_state, stack, 0)?;
let block = engine_state.get_block(capture_block.block_id).clone();
let var_id = block.signature.get_positional(0).and_then(|arg| arg.var_id);
let mut stack = stack.captures_to_stack(capture_block.captures);
let ctrlc = engine_state.ctrlc.clone();
let engine_state = engine_state.clone();
let eval_block = get_eval_block(&engine_state);
Ok(input Ok(input
.into_iter_strict(span)? .into_iter_strict(head)?
.take_while(move |value| { .take_while(move |value| {
if let Some(var_id) = var_id { closure
stack.add_var(var_id, value.clone()); .run_with_value(value.clone())
} .map(|data| data.into_value(head).is_false())
.unwrap_or(false)
!eval_block(&engine_state, &mut stack, &block, PipelineData::empty())
.map_or(false, |pipeline_data| {
pipeline_data.into_value(span).is_true()
})
}) })
.into_pipeline_data_with_metadata(metadata, ctrlc)) .into_pipeline_data_with_metadata(metadata, engine_state.ctrlc.clone()))
} }
} }

View File

@ -1,4 +1,4 @@
use nu_engine::{command_prelude::*, get_eval_block}; use nu_engine::{command_prelude::*, ClosureEval};
use nu_protocol::engine::Closure; use nu_protocol::engine::Closure;
#[derive(Clone)] #[derive(Clone)]
@ -70,34 +70,21 @@ impl Command for TakeWhile {
call: &Call, call: &Call,
input: PipelineData, input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let head = call.head;
let closure: Closure = call.req(engine_state, stack, 0)?;
let mut closure = ClosureEval::new(engine_state, stack, closure);
let metadata = input.metadata(); let metadata = input.metadata();
let span = call.head;
let capture_block: Closure = call.req(engine_state, stack, 0)?;
let block = engine_state.get_block(capture_block.block_id).clone();
let var_id = block.signature.get_positional(0).and_then(|arg| arg.var_id);
let mut stack = stack.captures_to_stack(capture_block.captures);
let ctrlc = engine_state.ctrlc.clone();
let engine_state = engine_state.clone();
let eval_block = get_eval_block(&engine_state);
Ok(input Ok(input
.into_iter_strict(span)? .into_iter_strict(head)?
.take_while(move |value| { .take_while(move |value| {
if let Some(var_id) = var_id { closure
stack.add_var(var_id, value.clone()); .run_with_value(value.clone())
} .map(|data| data.into_value(head).is_true())
.unwrap_or(false)
eval_block(&engine_state, &mut stack, &block, PipelineData::empty())
.map_or(false, |pipeline_data| {
pipeline_data.into_value(span).is_true()
})
}) })
.into_pipeline_data_with_metadata(metadata, ctrlc)) .into_pipeline_data_with_metadata(metadata, engine_state.ctrlc.clone()))
} }
} }

View File

@ -1,8 +1,5 @@
use nu_engine::{command_prelude::*, get_eval_block, EvalBlockFn}; use nu_engine::{command_prelude::*, ClosureEval, ClosureEvalOnce};
use nu_protocol::{ use nu_protocol::ast::PathMember;
ast::{Block, PathMember},
engine::Closure,
};
#[derive(Clone)] #[derive(Clone)]
pub struct Update; pub struct Update;
@ -111,46 +108,33 @@ fn update(
call: &Call, call: &Call,
input: PipelineData, input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let span = call.head; let head = call.head;
let cell_path: CellPath = call.req(engine_state, stack, 0)?; let cell_path: CellPath = call.req(engine_state, stack, 0)?;
let replacement: Value = call.req(engine_state, stack, 1)?; let replacement: Value = call.req(engine_state, stack, 1)?;
let replacement_span = replacement.span();
let ctrlc = engine_state.ctrlc.clone();
let eval_block = get_eval_block(engine_state);
match input { match input {
PipelineData::Value(mut value, metadata) => { PipelineData::Value(mut value, metadata) => {
if let Value::Closure { val: closure, .. } = replacement { if let Value::Closure { val, .. } = replacement {
match (cell_path.members.first(), &mut value) { match (cell_path.members.first(), &mut value) {
(Some(PathMember::String { .. }), Value::List { vals, .. }) => { (Some(PathMember::String { .. }), Value::List { vals, .. }) => {
let block = engine_state.get_block(closure.block_id); let mut closure = ClosureEval::new(engine_state, stack, val);
let stack = stack.captures_to_stack(closure.captures);
for val in vals { for val in vals {
let mut stack = stack.clone();
update_value_by_closure( update_value_by_closure(
val, val,
replacement_span, &mut closure,
engine_state, head,
&mut stack,
block,
&cell_path.members, &cell_path.members,
false, false,
eval_block,
)?; )?;
} }
} }
(first, _) => { (first, _) => {
update_single_value_by_closure( update_single_value_by_closure(
&mut value, &mut value,
closure, ClosureEvalOnce::new(engine_state, stack, val),
replacement_span, head,
engine_state,
stack,
&cell_path.members, &cell_path.members,
matches!(first, Some(PathMember::Int { .. })), matches!(first, Some(PathMember::Int { .. })),
eval_block,
)?; )?;
} }
} }
@ -187,16 +171,13 @@ fn update(
// cannot fail since loop above does at least one iteration or returns an error // cannot fail since loop above does at least one iteration or returns an error
let value = pre_elems.last_mut().expect("one element"); let value = pre_elems.last_mut().expect("one element");
if let Value::Closure { val: closure, .. } = replacement { if let Value::Closure { val, .. } = replacement {
update_single_value_by_closure( update_single_value_by_closure(
value, value,
closure, ClosureEvalOnce::new(engine_state, stack, val),
replacement_span, head,
engine_state,
stack,
path, path,
true, true,
eval_block,
)?; )?;
} else { } else {
value.update_data_at_cell_path(path, replacement)?; value.update_data_at_cell_path(path, replacement)?;
@ -205,121 +186,95 @@ fn update(
Ok(pre_elems Ok(pre_elems
.into_iter() .into_iter()
.chain(stream) .chain(stream)
.into_pipeline_data_with_metadata(metadata, ctrlc)) .into_pipeline_data_with_metadata(metadata, engine_state.ctrlc.clone()))
} else if let Value::Closure { val: closure, .. } = replacement { } else if let Value::Closure { val, .. } = replacement {
let engine_state = engine_state.clone(); let mut closure = ClosureEval::new(engine_state, stack, val);
let block = engine_state.get_block(closure.block_id).clone();
let stack = stack.captures_to_stack(closure.captures);
Ok(stream Ok(stream
.map(move |mut input| { .map(move |mut value| {
// Recreate the stack for each iteration to
// isolate environment variable changes, etc.
let mut stack = stack.clone();
let err = update_value_by_closure( let err = update_value_by_closure(
&mut input, &mut value,
replacement_span, &mut closure,
&engine_state, head,
&mut stack,
&block,
&cell_path.members, &cell_path.members,
false, false,
eval_block,
); );
if let Err(e) = err { if let Err(e) = err {
Value::error(e, span) Value::error(e, head)
} else { } else {
input value
} }
}) })
.into_pipeline_data_with_metadata(metadata, ctrlc)) .into_pipeline_data_with_metadata(metadata, engine_state.ctrlc.clone()))
} else { } else {
Ok(stream Ok(stream
.map(move |mut input| { .map(move |mut value| {
if let Err(e) = if let Err(e) =
input.update_data_at_cell_path(&cell_path.members, replacement.clone()) value.update_data_at_cell_path(&cell_path.members, replacement.clone())
{ {
Value::error(e, span) Value::error(e, head)
} else { } else {
input value
} }
}) })
.into_pipeline_data_with_metadata(metadata, ctrlc)) .into_pipeline_data_with_metadata(metadata, engine_state.ctrlc.clone()))
} }
} }
PipelineData::Empty => Err(ShellError::IncompatiblePathAccess { PipelineData::Empty => Err(ShellError::IncompatiblePathAccess {
type_name: "empty pipeline".to_string(), type_name: "empty pipeline".to_string(),
span, span: head,
}), }),
PipelineData::ExternalStream { .. } => Err(ShellError::IncompatiblePathAccess { PipelineData::ExternalStream { .. } => Err(ShellError::IncompatiblePathAccess {
type_name: "external stream".to_string(), type_name: "external stream".to_string(),
span, span: head,
}), }),
} }
} }
#[allow(clippy::too_many_arguments)]
fn update_value_by_closure( fn update_value_by_closure(
value: &mut Value, value: &mut Value,
closure: &mut ClosureEval,
span: Span, span: Span,
engine_state: &EngineState,
stack: &mut Stack,
block: &Block,
cell_path: &[PathMember], cell_path: &[PathMember],
first_path_member_int: bool, first_path_member_int: bool,
eval_block_fn: EvalBlockFn,
) -> Result<(), ShellError> { ) -> Result<(), ShellError> {
let input_at_path = value.clone().follow_cell_path(cell_path, false)?; let value_at_path = value.clone().follow_cell_path(cell_path, false)?;
if let Some(var) = block.signature.get_positional(0) { let arg = if first_path_member_int {
if let Some(var_id) = &var.var_id { &value_at_path
stack.add_var( } else {
*var_id, &*value
if first_path_member_int { };
input_at_path.clone()
} else {
value.clone()
},
)
}
}
let output = eval_block_fn( let new_value = closure
engine_state, .add_arg(arg.clone())
stack, .run_with_input(value_at_path.into_pipeline_data())?
block, .into_value(span);
input_at_path.into_pipeline_data(),
)?;
value.update_data_at_cell_path(cell_path, output.into_value(span)) value.update_data_at_cell_path(cell_path, new_value)
} }
#[allow(clippy::too_many_arguments)]
fn update_single_value_by_closure( fn update_single_value_by_closure(
value: &mut Value, value: &mut Value,
closure: Closure, closure: ClosureEvalOnce,
span: Span, span: Span,
engine_state: &EngineState,
stack: &mut Stack,
cell_path: &[PathMember], cell_path: &[PathMember],
first_path_member_int: bool, first_path_member_int: bool,
eval_block_fn: EvalBlockFn,
) -> Result<(), ShellError> { ) -> Result<(), ShellError> {
let block = engine_state.get_block(closure.block_id); let value_at_path = value.clone().follow_cell_path(cell_path, false)?;
let mut stack = stack.captures_to_stack(closure.captures);
update_value_by_closure( let arg = if first_path_member_int {
value, &value_at_path
span, } else {
engine_state, &*value
&mut stack, };
block,
cell_path, let new_value = closure
first_path_member_int, .add_arg(arg.clone())
eval_block_fn, .run_with_input(value_at_path.into_pipeline_data())?
) .into_value(span);
value.update_data_at_cell_path(cell_path, new_value)
} }
#[cfg(test)] #[cfg(test)]

View File

@ -1,8 +1,5 @@
use nu_engine::{command_prelude::*, get_eval_block, EvalBlockFn}; use nu_engine::{command_prelude::*, ClosureEval, ClosureEvalOnce};
use nu_protocol::{ use nu_protocol::ast::PathMember;
ast::{Block, PathMember},
engine::Closure,
};
#[derive(Clone)] #[derive(Clone)]
pub struct Upsert; pub struct Upsert;
@ -157,46 +154,33 @@ fn upsert(
call: &Call, call: &Call,
input: PipelineData, input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let span = call.head; let head = call.head;
let cell_path: CellPath = call.req(engine_state, stack, 0)?; let cell_path: CellPath = call.req(engine_state, stack, 0)?;
let replacement: Value = call.req(engine_state, stack, 1)?; let replacement: Value = call.req(engine_state, stack, 1)?;
let replacement_span = replacement.span();
let eval_block = get_eval_block(engine_state);
let ctrlc = engine_state.ctrlc.clone();
match input { match input {
PipelineData::Value(mut value, metadata) => { PipelineData::Value(mut value, metadata) => {
if let Value::Closure { val: closure, .. } = replacement { if let Value::Closure { val, .. } = replacement {
match (cell_path.members.first(), &mut value) { match (cell_path.members.first(), &mut value) {
(Some(PathMember::String { .. }), Value::List { vals, .. }) => { (Some(PathMember::String { .. }), Value::List { vals, .. }) => {
let block = engine_state.get_block(closure.block_id); let mut closure = ClosureEval::new(engine_state, stack, val);
let stack = stack.captures_to_stack(closure.captures);
for val in vals { for val in vals {
let mut stack = stack.clone();
upsert_value_by_closure( upsert_value_by_closure(
val, val,
replacement_span, &mut closure,
engine_state, head,
&mut stack,
block,
&cell_path.members, &cell_path.members,
false, false,
eval_block,
)?; )?;
} }
} }
(first, _) => { (first, _) => {
upsert_single_value_by_closure( upsert_single_value_by_closure(
&mut value, &mut value,
closure, ClosureEvalOnce::new(engine_state, stack, val),
replacement_span, head,
engine_state,
stack,
&cell_path.members, &cell_path.members,
matches!(first, Some(PathMember::Int { .. })), matches!(first, Some(PathMember::Int { .. })),
eval_block,
)?; )?;
} }
} }
@ -228,169 +212,129 @@ fn upsert(
} }
} }
if path.is_empty() { let value = if path.is_empty() {
let value = stream.next().unwrap_or(Value::nothing(span)); let value = stream.next().unwrap_or(Value::nothing(head));
if let Value::Closure { val: closure, .. } = replacement { if let Value::Closure { val, .. } = replacement {
let block = engine_state.get_block(closure.block_id); ClosureEvalOnce::new(engine_state, stack, val)
let mut stack = stack.captures_to_stack(closure.captures); .run_with_value(value)?
.into_value(head)
if let Some(var) = block.signature.get_positional(0) {
if let Some(var_id) = &var.var_id {
stack.add_var(*var_id, value.clone())
}
}
let output = eval_block(
engine_state,
&mut stack,
block,
value.clone().into_pipeline_data(),
)?;
pre_elems.push(output.into_value(replacement_span));
} else { } else {
pre_elems.push(replacement); replacement
} }
} else if let Some(mut value) = stream.next() { } else if let Some(mut value) = stream.next() {
if let Value::Closure { val: closure, .. } = replacement { if let Value::Closure { val, .. } = replacement {
upsert_single_value_by_closure( upsert_single_value_by_closure(
&mut value, &mut value,
closure, ClosureEvalOnce::new(engine_state, stack, val),
replacement_span, head,
engine_state,
stack,
path, path,
true, true,
eval_block,
)?; )?;
} else { } else {
value.upsert_data_at_cell_path(path, replacement)?; value.upsert_data_at_cell_path(path, replacement)?;
} }
pre_elems.push(value) value
} else { } else {
return Err(ShellError::AccessBeyondEnd { return Err(ShellError::AccessBeyondEnd {
max_idx: pre_elems.len() - 1, max_idx: pre_elems.len() - 1,
span: path_span, span: path_span,
}); });
} };
pre_elems.push(value);
Ok(pre_elems Ok(pre_elems
.into_iter() .into_iter()
.chain(stream) .chain(stream)
.into_pipeline_data_with_metadata(metadata, ctrlc)) .into_pipeline_data_with_metadata(metadata, engine_state.ctrlc.clone()))
} else if let Value::Closure { val: closure, .. } = replacement { } else if let Value::Closure { val, .. } = replacement {
let engine_state = engine_state.clone(); let mut closure = ClosureEval::new(engine_state, stack, val);
let block = engine_state.get_block(closure.block_id).clone();
let stack = stack.captures_to_stack(closure.captures);
Ok(stream Ok(stream
.map(move |mut input| { .map(move |mut value| {
// Recreate the stack for each iteration to
// isolate environment variable changes, etc.
let mut stack = stack.clone();
let err = upsert_value_by_closure( let err = upsert_value_by_closure(
&mut input, &mut value,
replacement_span, &mut closure,
&engine_state, head,
&mut stack,
&block,
&cell_path.members, &cell_path.members,
false, false,
eval_block,
); );
if let Err(e) = err { if let Err(e) = err {
Value::error(e, span) Value::error(e, head)
} else { } else {
input value
} }
}) })
.into_pipeline_data_with_metadata(metadata, ctrlc)) .into_pipeline_data_with_metadata(metadata, engine_state.ctrlc.clone()))
} else { } else {
Ok(stream Ok(stream
.map(move |mut input| { .map(move |mut value| {
if let Err(e) = if let Err(e) =
input.upsert_data_at_cell_path(&cell_path.members, replacement.clone()) value.upsert_data_at_cell_path(&cell_path.members, replacement.clone())
{ {
Value::error(e, span) Value::error(e, head)
} else { } else {
input value
} }
}) })
.into_pipeline_data_with_metadata(metadata, ctrlc)) .into_pipeline_data_with_metadata(metadata, engine_state.ctrlc.clone()))
} }
} }
PipelineData::Empty => Err(ShellError::IncompatiblePathAccess { PipelineData::Empty => Err(ShellError::IncompatiblePathAccess {
type_name: "empty pipeline".to_string(), type_name: "empty pipeline".to_string(),
span, span: head,
}), }),
PipelineData::ExternalStream { .. } => Err(ShellError::IncompatiblePathAccess { PipelineData::ExternalStream { .. } => Err(ShellError::IncompatiblePathAccess {
type_name: "external stream".to_string(), type_name: "external stream".to_string(),
span, span: head,
}), }),
} }
} }
#[allow(clippy::too_many_arguments)]
fn upsert_value_by_closure( fn upsert_value_by_closure(
value: &mut Value, value: &mut Value,
closure: &mut ClosureEval,
span: Span, span: Span,
engine_state: &EngineState,
stack: &mut Stack,
block: &Block,
cell_path: &[PathMember], cell_path: &[PathMember],
first_path_member_int: bool, first_path_member_int: bool,
eval_block_fn: EvalBlockFn,
) -> Result<(), ShellError> { ) -> Result<(), ShellError> {
let input_at_path = value.clone().follow_cell_path(cell_path, false); let value_at_path = value.clone().follow_cell_path(cell_path, false);
if let Some(var) = block.signature.get_positional(0) { let arg = if first_path_member_int {
if let Some(var_id) = &var.var_id { value_at_path.clone().unwrap_or(Value::nothing(span))
stack.add_var( } else {
*var_id, value.clone()
if first_path_member_int { };
input_at_path.clone().unwrap_or(Value::nothing(span))
} else {
value.clone()
},
)
}
}
let input_at_path = input_at_path let input = value_at_path
.map(IntoPipelineData::into_pipeline_data) .map(IntoPipelineData::into_pipeline_data)
.unwrap_or(PipelineData::Empty); .unwrap_or(PipelineData::Empty);
let output = eval_block_fn(engine_state, stack, block, input_at_path)?; let new_value = closure.add_arg(arg).run_with_input(input)?.into_value(span);
value.upsert_data_at_cell_path(cell_path, new_value)
value.upsert_data_at_cell_path(cell_path, output.into_value(span))
} }
#[allow(clippy::too_many_arguments)]
fn upsert_single_value_by_closure( fn upsert_single_value_by_closure(
value: &mut Value, value: &mut Value,
closure: Closure, closure: ClosureEvalOnce,
span: Span, span: Span,
engine_state: &EngineState,
stack: &mut Stack,
cell_path: &[PathMember], cell_path: &[PathMember],
first_path_member_int: bool, first_path_member_int: bool,
eval_block_fn: EvalBlockFn,
) -> Result<(), ShellError> { ) -> Result<(), ShellError> {
let block = engine_state.get_block(closure.block_id); let value_at_path = value.clone().follow_cell_path(cell_path, false);
let mut stack = stack.captures_to_stack(closure.captures);
upsert_value_by_closure( let arg = if first_path_member_int {
value, value_at_path.clone().unwrap_or(Value::nothing(span))
span, } else {
engine_state, value.clone()
&mut stack, };
block,
cell_path, let input = value_at_path
first_path_member_int, .map(IntoPipelineData::into_pipeline_data)
eval_block_fn, .unwrap_or(PipelineData::Empty);
)
let new_value = closure.add_arg(arg).run_with_input(input)?.into_value(span);
value.upsert_data_at_cell_path(cell_path, new_value)
} }
#[cfg(test)] #[cfg(test)]

View File

@ -1,4 +1,4 @@
use nu_engine::{get_eval_block, CallExt}; use nu_engine::{CallExt, ClosureEval};
use nu_protocol::{ use nu_protocol::{
ast::Call, ast::Call,
engine::{Closure, EngineState, Stack}, engine::{Closure, EngineState, Stack},
@ -26,44 +26,22 @@ pub fn boolean_fold(
input: PipelineData, input: PipelineData,
accumulator: bool, accumulator: bool,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let span = call.head; let head = call.head;
let closure: Closure = call.req(engine_state, stack, 0)?;
let capture_block: Closure = call.req(engine_state, stack, 0)?; let mut closure = ClosureEval::new(engine_state, stack, closure);
let block_id = capture_block.block_id;
let block = engine_state.get_block(block_id); for value in input {
let var_id = block.signature.get_positional(0).and_then(|arg| arg.var_id); if nu_utils::ctrl_c::was_pressed(&engine_state.ctrlc) {
let mut stack = stack.captures_to_stack(capture_block.captures); break;
let orig_env_vars = stack.env_vars.clone();
let orig_env_hidden = stack.env_hidden.clone();
let ctrlc = engine_state.ctrlc.clone();
let eval_block = get_eval_block(engine_state);
for value in input.into_interruptible_iter(ctrlc) {
// with_env() is used here to ensure that each iteration uses
// a different set of environment variables.
// Hence, a 'cd' in the first loop won't affect the next loop.
stack.with_env(&orig_env_vars, &orig_env_hidden);
if let Some(var_id) = var_id {
stack.add_var(var_id, value.clone());
} }
let eval = eval_block(engine_state, &mut stack, block, value.into_pipeline_data()); let pred = closure.run_with_value(value)?.into_value(head).is_true();
match eval {
Err(e) => { if pred == accumulator {
return Err(e); return Ok(Value::bool(accumulator, head).into_pipeline_data());
}
Ok(pipeline_data) => {
if pipeline_data.into_value(span).is_true() == accumulator {
return Ok(Value::bool(accumulator, span).into_pipeline_data());
}
}
} }
} }
Ok(Value::bool(!accumulator, span).into_pipeline_data()) Ok(Value::bool(!accumulator, head).into_pipeline_data())
} }

View File

@ -1,4 +1,4 @@
use nu_engine::{command_prelude::*, get_eval_block}; use nu_engine::{command_prelude::*, ClosureEval};
use nu_protocol::engine::Closure; use nu_protocol::engine::Closure;
#[derive(Clone)] #[derive(Clone)]
@ -49,54 +49,19 @@ not supported."#
call: &Call, call: &Call,
input: PipelineData, input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let head = call.head;
let closure: Closure = call.req(engine_state, stack, 0)?; let closure: Closure = call.req(engine_state, stack, 0)?;
let span = call.head; let mut closure = ClosureEval::new(engine_state, stack, closure);
let metadata = input.metadata(); let metadata = input.metadata();
let mut stack = stack.captures_to_stack(closure.captures);
let block = engine_state.get_block(closure.block_id).clone();
let orig_env_vars = stack.env_vars.clone();
let orig_env_hidden = stack.env_hidden.clone();
let ctrlc = engine_state.ctrlc.clone();
let engine_state = engine_state.clone();
let eval_block = get_eval_block(&engine_state);
Ok(input Ok(input
.into_iter_strict(span)? .into_iter_strict(head)?
.filter_map(move |value| { .filter_map(move |value| match closure.run_with_value(value.clone()) {
stack.with_env(&orig_env_vars, &orig_env_hidden); Ok(data) => data.into_value(head).is_true().then_some(value),
Err(err) => Some(Value::error(err, head)),
if let Some(var) = block.signature.get_positional(0) {
if let Some(var_id) = &var.var_id {
stack.add_var(*var_id, value.clone());
}
}
let result = eval_block(
&engine_state,
&mut stack,
&block,
// clone() is used here because x is given to Ok() below.
value.clone().into_pipeline_data(),
);
match result {
Ok(result) => {
let result = result.into_value(span);
if result.is_true() {
Some(value)
} else {
None
}
}
Err(err) => Some(Value::error(err, span)),
}
}) })
.into_pipeline_data_with_metadata(metadata, ctrlc)) .into_pipeline_data_with_metadata(metadata, engine_state.ctrlc.clone()))
} }
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {

View File

@ -1,4 +1,4 @@
use nu_engine::{command_prelude::*, get_eval_block_with_early_return}; use nu_engine::{command_prelude::*, ClosureEvalOnce};
#[derive(Clone)] #[derive(Clone)]
pub struct Zip; pub struct Zip;
@ -98,26 +98,21 @@ impl Command for Zip {
input: PipelineData, input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let head = call.head; let head = call.head;
let ctrlc = engine_state.ctrlc.clone(); let other = call.req(engine_state, stack, 0)?;
let metadata = input.metadata();
let eval_block_with_early_return = get_eval_block_with_early_return(engine_state);
let other: PipelineData = match call.req(engine_state, stack, 0)? { let metadata = input.metadata();
let other = if let Value::Closure { val, .. } = other {
// If a closure was provided, evaluate it and consume its stream output // If a closure was provided, evaluate it and consume its stream output
Value::Closure { val, .. } => { ClosureEvalOnce::new(engine_state, stack, val).run_with_input(PipelineData::Empty)?
let block = engine_state.get_block(val.block_id); } else {
let mut stack = stack.captures_to_stack(val.captures); other.into_pipeline_data()
eval_block_with_early_return(engine_state, &mut stack, block, PipelineData::Empty)?
}
// If any other value, use it as-is.
val => val.into_pipeline_data(),
}; };
Ok(input Ok(input
.into_iter() .into_iter()
.zip(other) .zip(other)
.map(move |(x, y)| Value::list(vec![x, y], head)) .map(move |(x, y)| Value::list(vec![x, y], head))
.into_pipeline_data_with_metadata(metadata, ctrlc)) .into_pipeline_data_with_metadata(metadata, engine_state.ctrlc.clone()))
} }
} }

View File

@ -1,5 +1,5 @@
use itertools::unfold; use itertools::unfold;
use nu_engine::{command_prelude::*, get_eval_block_with_early_return}; use nu_engine::{command_prelude::*, ClosureEval};
use nu_protocol::engine::Closure; use nu_protocol::engine::Closure;
#[derive(Clone)] #[derive(Clone)]
@ -91,43 +91,19 @@ used as the next argument to the closure, otherwise generation stops.
call: &Call, call: &Call,
_input: PipelineData, _input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let head = call.head;
let initial: Value = call.req(engine_state, stack, 0)?; let initial: Value = call.req(engine_state, stack, 0)?;
let capture_block: Spanned<Closure> = call.req(engine_state, stack, 1)?; let closure: Closure = call.req(engine_state, stack, 1)?;
let block_span = capture_block.span;
let block = engine_state.get_block(capture_block.item.block_id).clone(); let mut closure = ClosureEval::new(engine_state, stack, closure);
let ctrlc = engine_state.ctrlc.clone();
let engine_state = engine_state.clone();
let mut stack = stack.captures_to_stack(capture_block.item.captures);
let orig_env_vars = stack.env_vars.clone();
let orig_env_hidden = stack.env_hidden.clone();
let eval_block_with_early_return = get_eval_block_with_early_return(&engine_state);
// A type of Option<S> is used to represent state. Invocation // A type of Option<S> is used to represent state. Invocation
// will stop on None. Using Option<S> allows functions to output // will stop on None. Using Option<S> allows functions to output
// one final value before stopping. // one final value before stopping.
let iter = unfold(Some(initial), move |state| { let iter = unfold(Some(initial), move |state| {
let arg = match state { let arg = state.take()?;
Some(state) => state.clone(),
None => return None,
};
// with_env() is used here to ensure that each iteration uses let (output, next_input) = match closure.run_with_value(arg) {
// a different set of environment variables.
// Hence, a 'cd' in the first loop won't affect the next loop.
stack.with_env(&orig_env_vars, &orig_env_hidden);
if let Some(var) = block.signature.get_positional(0) {
if let Some(var_id) = &var.var_id {
stack.add_var(*var_id, arg.clone());
}
}
let (output, next_input) = match eval_block_with_early_return(
&engine_state,
&mut stack,
&block,
arg.into_pipeline_data(),
) {
// no data -> output nothing and stop. // no data -> output nothing and stop.
Ok(PipelineData::Empty) => (None, None), Ok(PipelineData::Empty) => (None, None),
@ -154,7 +130,7 @@ used as the next argument to the closure, otherwise generation stops.
help: None, help: None,
inner: vec![], inner: vec![],
}; };
err = Some(Value::error(error, block_span)); err = Some(Value::error(error, head));
break; break;
} }
} }
@ -176,13 +152,13 @@ used as the next argument to the closure, otherwise generation stops.
inner: vec![], inner: vec![],
}; };
(Some(Value::error(error, block_span)), None) (Some(Value::error(error, head)), None)
} }
} }
} }
Ok(other) => { Ok(other) => {
let val = other.into_value(block_span); let val = other.into_value(head);
let error = ShellError::GenericError { let error = ShellError::GenericError {
error: "Invalid block return".into(), error: "Invalid block return".into(),
msg: format!("Expected record, found {}", val.get_type()), msg: format!("Expected record, found {}", val.get_type()),
@ -191,11 +167,11 @@ used as the next argument to the closure, otherwise generation stops.
inner: vec![], inner: vec![],
}; };
(Some(Value::error(error, block_span)), None) (Some(Value::error(error, head)), None)
} }
// error -> error and stop // error -> error and stop
Err(error) => (Some(Value::error(error, block_span)), None), Err(error) => (Some(Value::error(error, head)), None),
}; };
// We use `state` to control when to stop, not `output`. By wrapping // We use `state` to control when to stop, not `output`. By wrapping
@ -205,7 +181,9 @@ used as the next argument to the closure, otherwise generation stops.
Some(output) Some(output)
}); });
Ok(iter.flatten().into_pipeline_data(ctrlc)) Ok(iter
.flatten()
.into_pipeline_data(engine_state.ctrlc.clone()))
} }
} }

View File

@ -0,0 +1,236 @@
use crate::{
eval_block_with_early_return, get_eval_block_with_early_return, EvalBlockWithEarlyReturnFn,
};
use nu_protocol::{
ast::Block,
debugger::{WithDebug, WithoutDebug},
engine::{Closure, EngineState, EnvVars, Stack},
IntoPipelineData, PipelineData, ShellError, Value,
};
use std::{
borrow::Cow,
collections::{HashMap, HashSet},
sync::Arc,
};
fn eval_fn(debug: bool) -> EvalBlockWithEarlyReturnFn {
if debug {
eval_block_with_early_return::<WithDebug>
} else {
eval_block_with_early_return::<WithoutDebug>
}
}
/// [`ClosureEval`] is used to repeatedly evaluate a closure with different values/inputs.
///
/// [`ClosureEval`] has a builder API.
/// It is first created via [`ClosureEval::new`],
/// then has arguments added via [`ClosureEval::add_arg`],
/// and then can be run using [`ClosureEval::run_with_input`].
///
/// ```no_run
/// # use nu_protocol::{PipelineData, Value};
/// # use nu_engine::ClosureEval;
/// # let engine_state = unimplemented!();
/// # let stack = unimplemented!();
/// # let closure = unimplemented!();
/// let mut closure = ClosureEval::new(engine_state, stack, closure);
/// let iter = Vec::<Value>::new()
/// .into_iter()
/// .map(move |value| closure.add_arg(value).run_with_input(PipelineData::Empty));
/// ```
///
/// Many closures follow a simple, common scheme where the pipeline input and the first argument are the same value.
/// In this case, use [`ClosureEval::run_with_value`]:
///
/// ```no_run
/// # use nu_protocol::{PipelineData, Value};
/// # use nu_engine::ClosureEval;
/// # let engine_state = unimplemented!();
/// # let stack = unimplemented!();
/// # let closure = unimplemented!();
/// let mut closure = ClosureEval::new(engine_state, stack, closure);
/// let iter = Vec::<Value>::new()
/// .into_iter()
/// .map(move |value| closure.run_with_value(value));
/// ```
///
/// Environment isolation and other cleanup is handled by [`ClosureEval`],
/// so nothing needs to be done following [`ClosureEval::run_with_input`] or [`ClosureEval::run_with_value`].
pub struct ClosureEval {
engine_state: EngineState,
stack: Stack,
block: Arc<Block>,
arg_index: usize,
env_vars: Vec<EnvVars>,
env_hidden: HashMap<String, HashSet<String>>,
eval: EvalBlockWithEarlyReturnFn,
}
impl ClosureEval {
/// Create a new [`ClosureEval`].
pub fn new(engine_state: &EngineState, stack: &Stack, closure: Closure) -> Self {
let engine_state = engine_state.clone();
let stack = stack.captures_to_stack(closure.captures);
let block = engine_state.get_block(closure.block_id).clone();
let env_vars = stack.env_vars.clone();
let env_hidden = stack.env_hidden.clone();
let eval = get_eval_block_with_early_return(&engine_state);
Self {
engine_state,
stack,
block,
arg_index: 0,
env_vars,
env_hidden,
eval,
}
}
/// Sets whether to enable debugging when evaluating the closure.
///
/// By default, this is controlled by the [`EngineState`] used to create this [`ClosureEval`].
pub fn debug(&mut self, debug: bool) -> &mut Self {
self.eval = eval_fn(debug);
self
}
fn try_add_arg(&mut self, value: Cow<Value>) {
if let Some(var_id) = self
.block
.signature
.get_positional(self.arg_index)
.and_then(|var| var.var_id)
{
self.stack.add_var(var_id, value.into_owned());
self.arg_index += 1;
}
}
/// Add an argument [`Value`] to the closure.
///
/// Multiple [`add_arg`](Self::add_arg) calls can be chained together,
/// but make sure that arguments are added based on their positional order.
pub fn add_arg(&mut self, value: Value) -> &mut Self {
self.try_add_arg(Cow::Owned(value));
self
}
/// Run the closure, passing the given [`PipelineData`] as input.
///
/// Any arguments should be added beforehand via [`add_arg`](Self::add_arg).
pub fn run_with_input(&mut self, input: PipelineData) -> Result<PipelineData, ShellError> {
self.arg_index = 0;
self.stack.with_env(&self.env_vars, &self.env_hidden);
(self.eval)(&self.engine_state, &mut self.stack, &self.block, input)
}
/// Run the closure using the given [`Value`] as both the pipeline input and the first argument.
///
/// Using this function after or in combination with [`add_arg`](Self::add_arg) is most likely an error.
/// This function is equivalent to `self.add_arg(value)` followed by `self.run_with_input(value.into_pipeline_data())`.
pub fn run_with_value(&mut self, value: Value) -> Result<PipelineData, ShellError> {
self.try_add_arg(Cow::Borrowed(&value));
self.run_with_input(value.into_pipeline_data())
}
}
/// [`ClosureEvalOnce`] is used to evaluate a closure a single time.
///
/// [`ClosureEvalOnce`] has a builder API.
/// It is first created via [`ClosureEvalOnce::new`],
/// then has arguments added via [`ClosureEvalOnce::add_arg`],
/// and then can be run using [`ClosureEvalOnce::run_with_input`].
///
/// ```no_run
/// # use nu_protocol::{ListStream, PipelineData, PipelineIterator};
/// # use nu_engine::ClosureEvalOnce;
/// # let engine_state = unimplemented!();
/// # let stack = unimplemented!();
/// # let closure = unimplemented!();
/// # let value = unimplemented!();
/// let result = ClosureEvalOnce::new(engine_state, stack, closure)
/// .add_arg(value)
/// .run_with_input(PipelineData::Empty);
/// ```
///
/// Many closures follow a simple, common scheme where the pipeline input and the first argument are the same value.
/// In this case, use [`ClosureEvalOnce::run_with_value`]:
///
/// ```no_run
/// # use nu_protocol::{PipelineData, PipelineIterator};
/// # use nu_engine::ClosureEvalOnce;
/// # let engine_state = unimplemented!();
/// # let stack = unimplemented!();
/// # let closure = unimplemented!();
/// # let value = unimplemented!();
/// let result = ClosureEvalOnce::new(engine_state, stack, closure).run_with_value(value);
/// ```
pub struct ClosureEvalOnce<'a> {
engine_state: &'a EngineState,
stack: Stack,
block: &'a Block,
arg_index: usize,
eval: EvalBlockWithEarlyReturnFn,
}
impl<'a> ClosureEvalOnce<'a> {
/// Create a new [`ClosureEvalOnce`].
pub fn new(engine_state: &'a EngineState, stack: &Stack, closure: Closure) -> Self {
let block = engine_state.get_block(closure.block_id);
let eval = get_eval_block_with_early_return(engine_state);
Self {
engine_state,
stack: stack.captures_to_stack(closure.captures),
block,
arg_index: 0,
eval,
}
}
/// Sets whether to enable debugging when evaluating the closure.
///
/// By default, this is controlled by the [`EngineState`] used to create this [`ClosureEvalOnce`].
pub fn debug(mut self, debug: bool) -> Self {
self.eval = eval_fn(debug);
self
}
fn try_add_arg(&mut self, value: Cow<Value>) {
if let Some(var_id) = self
.block
.signature
.get_positional(self.arg_index)
.and_then(|var| var.var_id)
{
self.stack.add_var(var_id, value.into_owned());
self.arg_index += 1;
}
}
/// Add an argument [`Value`] to the closure.
///
/// Multiple [`add_arg`](Self::add_arg) calls can be chained together,
/// but make sure that arguments are added based on their positional order.
pub fn add_arg(mut self, value: Value) -> Self {
self.try_add_arg(Cow::Owned(value));
self
}
/// Run the closure, passing the given [`PipelineData`] as input.
///
/// Any arguments should be added beforehand via [`add_arg`](Self::add_arg).
pub fn run_with_input(mut self, input: PipelineData) -> Result<PipelineData, ShellError> {
(self.eval)(self.engine_state, &mut self.stack, self.block, input)
}
/// Run the closure using the given [`Value`] as both the pipeline input and the first argument.
///
/// Using this function after or in combination with [`add_arg`](Self::add_arg) is most likely an error.
/// This function is equivalent to `self.add_arg(value)` followed by `self.run_with_input(value.into_pipeline_data())`.
pub fn run_with_value(mut self, value: Value) -> Result<PipelineData, ShellError> {
self.try_add_arg(Cow::Borrowed(&value));
self.run_with_input(value.into_pipeline_data())
}
}

View File

@ -1,10 +1,9 @@
use crate::eval_block; use crate::ClosureEvalOnce;
use nu_path::canonicalize_with; use nu_path::canonicalize_with;
use nu_protocol::{ use nu_protocol::{
ast::{Call, Expr}, ast::{Call, Expr},
debugger::WithoutDebug,
engine::{EngineState, Stack, StateWorkingSet, PWD_ENV}, engine::{EngineState, Stack, StateWorkingSet, PWD_ENV},
Config, PipelineData, ShellError, Span, Value, VarId, Config, ShellError, Span, Value, VarId,
}; };
use std::{ use std::{
collections::HashMap, collections::HashMap,
@ -376,37 +375,12 @@ fn get_converted_value(
.and_then(|record| record.get(direction)); .and_then(|record| record.get(direction));
if let Some(conversion) = conversion { if let Some(conversion) = conversion {
let from_span = conversion.span();
match conversion.as_closure() { match conversion.as_closure() {
Ok(val) => { Ok(closure) => ClosureEvalOnce::new(engine_state, stack, closure.clone())
let block = engine_state.get_block(val.block_id); .debug(false)
.run_with_value(orig_val.clone())
if let Some(var) = block.signature.get_positional(0) { .map(|data| ConversionResult::Ok(data.into_value(orig_val.span())))
let mut stack = stack.captures_to_stack(val.captures.clone()); .unwrap_or_else(ConversionResult::ConversionError),
if let Some(var_id) = &var.var_id {
stack.add_var(*var_id, orig_val.clone());
}
let val_span = orig_val.span();
// TODO DEBUG
let result = eval_block::<WithoutDebug>(
engine_state,
&mut stack,
block,
PipelineData::new_with_metadata(None, val_span),
);
match result {
Ok(data) => ConversionResult::Ok(data.into_value(val_span)),
Err(e) => ConversionResult::ConversionError(e),
}
} else {
ConversionResult::ConversionError(ShellError::MissingParameter {
param_name: "block input".into(),
span: from_span,
})
}
}
Err(e) => ConversionResult::ConversionError(e), Err(e) => ConversionResult::ConversionError(e),
} }
} else { } else {

View File

@ -1,4 +1,5 @@
mod call_ext; mod call_ext;
mod closure_eval;
pub mod column; pub mod column;
pub mod command_prelude; pub mod command_prelude;
pub mod documentation; pub mod documentation;
@ -9,6 +10,7 @@ mod glob_from;
pub mod scope; pub mod scope;
pub use call_ext::CallExt; pub use call_ext::CallExt;
pub use closure_eval::*;
pub use column::get_columns; pub use column::get_columns;
pub use documentation::get_full_help; pub use documentation::get_full_help;
pub use env::*; pub use env::*;

View File

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