Skip to main content

nu_engine/
eval_ir.rs

1use std::{borrow::Cow, fs::File, sync::Arc};
2
3use nu_path::{dots::expand_ndots_safe, expand_path, expand_path_with, expand_tilde};
4#[cfg(feature = "os")]
5use nu_protocol::process::check_exit_status_future;
6use nu_protocol::{
7    DeclId, ENV_VARIABLE_ID, Flag, IntoPipelineData, IntoSpanned, ListStream, OutDest,
8    PipelineData, PipelineExecutionData, PositionalArg, Range, Record, RegId, ShellError, Signals,
9    Signature, Span, Spanned, Type, Value, VarId,
10    ast::{Bits, Block, Boolean, CellPath, Comparison, Math, Operator},
11    combined_type_string,
12    debugger::DebugContext,
13    engine::{
14        Argument, Closure, EngineState, EnvName, ErrorHandler, Matcher, Redirection, Stack,
15        StateWorkingSet,
16    },
17    ir::{Call, DataSlice, Instruction, IrAstRef, IrBlock, Literal, RedirectMode},
18    shell_error::generic::GenericError,
19    shell_error::io::IoError,
20};
21use nu_utils::IgnoreCaseExt;
22
23use crate::{
24    ENV_CONVERSIONS, convert_env_vars, eval::is_automatic_env_var, eval_block_with_early_return,
25};
26
27/// For `def --wrapped` and `known extern` rest params (`SyntaxShape::ExternalArgument`), expand
28/// tilde and ndots in bare glob values that are not actual glob patterns. This mirrors what
29/// `run-external` does in `eval_external_arguments`, so that `$args | to nuon` returns expanded
30/// paths instead of the raw `~` / `...` tokens.
31fn expand_external_glob_arg(val: Value) -> Value {
32    if let Value::Glob {
33        val: ref s,
34        no_expand,
35        internal_span,
36        ..
37    } = val
38        && !no_expand
39        && !nu_glob::is_glob(s)
40    {
41        let expanded = expand_ndots_safe(expand_tilde(s.as_str()));
42        let expanded_str = expanded.to_string_lossy().into_owned();
43        if expanded_str != *s {
44            return Value::string(expanded_str, internal_span);
45        }
46    }
47    val
48}
49
50pub fn eval_ir_block<D: DebugContext>(
51    engine_state: &EngineState,
52    stack: &mut Stack,
53    block: &Block,
54    input: PipelineData,
55) -> Result<PipelineExecutionData, ShellError> {
56    // Rust does not check recursion limits outside of const evaluation.
57    // But nu programs run in the same process as the shell.
58    // To prevent a stack overflow in user code from crashing the shell,
59    // we limit the recursion depth of function calls.
60    let maximum_call_stack_depth: u64 = engine_state.config.recursion_limit as u64;
61    if stack.recursion_count > maximum_call_stack_depth {
62        return Err(ShellError::RecursionLimitReached {
63            recursion_limit: maximum_call_stack_depth,
64            span: block.span,
65        });
66    }
67
68    if let Some(ir_block) = &block.ir_block {
69        D::enter_block(engine_state, block);
70
71        let args_base = stack.arguments.get_base();
72        let error_handler_base = stack.error_handlers.get_base();
73        let finally_handler_base = stack.finally_run_handlers.get_base();
74
75        // Allocate and initialize registers. I've found that it's not really worth trying to avoid
76        // the heap allocation here by reusing buffers - our allocator is fast enough
77        let mut registers = Vec::with_capacity(ir_block.register_count as usize);
78        for _ in 0..ir_block.register_count {
79            registers.push(PipelineExecutionData::empty());
80        }
81
82        // Initialize file storage.
83        let mut files = vec![None; ir_block.file_count as usize];
84
85        let result = eval_ir_block_impl::<D>(
86            &mut EvalContext {
87                engine_state,
88                stack,
89                data: &ir_block.data,
90                block_span: &block.span,
91                args_base,
92                error_handler_base,
93                finally_handler_base,
94                redirect_out: None,
95                redirect_err: None,
96                matches: vec![],
97                registers: &mut registers[..],
98                files: &mut files[..],
99            },
100            ir_block,
101            input,
102        );
103
104        stack.error_handlers.leave_frame(error_handler_base);
105        stack.finally_run_handlers.leave_frame(finally_handler_base);
106        stack.arguments.leave_frame(args_base);
107
108        D::leave_block(engine_state, block);
109
110        result
111    } else {
112        // FIXME blocks having IR should not be optional
113        let error = if let Some(span) = block.span {
114            ShellError::Generic(
115                GenericError::new(
116                    "Can't evaluate block in IR mode",
117                    "block is missing compiled representation",
118                    span,
119                )
120                .with_help("the IrBlock is probably missing due to a compilation error"),
121            )
122        } else {
123            ShellError::Generic(
124                GenericError::new_internal(
125                    "Can't evaluate block in IR mode",
126                    "block is missing compiled representation",
127                )
128                .with_help("the IrBlock is probably missing due to a compilation error"),
129            )
130        };
131        Err(error)
132    }
133}
134
135/// All of the pointers necessary for evaluation
136struct EvalContext<'a> {
137    engine_state: &'a EngineState,
138    stack: &'a mut Stack,
139    data: &'a Arc<[u8]>,
140    /// The span of the block
141    block_span: &'a Option<Span>,
142    /// Base index on the argument stack to reset to after a call
143    args_base: usize,
144    /// Base index on the error handler stack to reset to after a call
145    error_handler_base: usize,
146    /// Base index on the finally handler stack to reset to after a call
147    finally_handler_base: usize,
148    /// State set by redirect-out
149    redirect_out: Option<Redirection>,
150    /// State set by redirect-err
151    redirect_err: Option<Redirection>,
152    /// Scratch space to use for `match`
153    matches: Vec<(VarId, Value)>,
154    /// Intermediate pipeline data storage used by instructions, indexed by RegId
155    registers: &'a mut [PipelineExecutionData],
156    /// Holds open files used by redirections
157    files: &'a mut [Option<Arc<File>>],
158}
159
160impl<'a> EvalContext<'a> {
161    /// Replace the contents of a register with a new value
162    #[inline]
163    fn put_reg(&mut self, reg_id: RegId, new_value: PipelineExecutionData) {
164        // log::trace!("{reg_id} <- {new_value:?}");
165        self.registers[reg_id.get() as usize] = new_value;
166    }
167
168    /// Borrow the contents of a register.
169    #[inline]
170    fn borrow_reg(&self, reg_id: RegId) -> &PipelineData {
171        &self.registers[reg_id.get() as usize]
172    }
173
174    /// Replace the contents of a register with `Empty` and then return the value that it contained
175    #[inline]
176    fn take_reg(&mut self, reg_id: RegId) -> PipelineExecutionData {
177        // log::trace!("<- {reg_id}");
178        std::mem::replace(
179            &mut self.registers[reg_id.get() as usize],
180            PipelineExecutionData::empty(),
181        )
182    }
183
184    /// Clone data from a register. Must be collected first.
185    fn clone_reg(&mut self, reg_id: RegId, error_span: Span) -> Result<PipelineData, ShellError> {
186        // NOTE: here just clone the inner PipelineData
187        // it's suitable for current usage.
188        match &self.registers[reg_id.get() as usize].body {
189            PipelineData::Empty => Ok(PipelineData::empty()),
190            PipelineData::Value(val, meta) => Ok(PipelineData::value(val.clone(), meta.clone())),
191            _ => Err(ShellError::IrEvalError {
192                msg: "Must collect to value before using instruction that clones from a register"
193                    .into(),
194                span: Some(error_span),
195            }),
196        }
197    }
198
199    /// Clone a value from a register. Must be collected first.
200    fn clone_reg_value(&mut self, reg_id: RegId, fallback_span: Span) -> Result<Value, ShellError> {
201        match self.clone_reg(reg_id, fallback_span)? {
202            PipelineData::Empty => Ok(Value::nothing(fallback_span)),
203            PipelineData::Value(val, _) => Ok(val),
204            _ => unreachable!("clone_reg should never return stream data"),
205        }
206    }
207
208    /// Take and implicitly collect a register to a value
209    ///
210    /// It doesn't check exit status when collecting.
211    fn collect_reg(&mut self, reg_id: RegId, fallback_span: Span) -> Result<Value, ShellError> {
212        // NOTE: collect_reg is used to collect the reg to a variable.
213        // So it's good to pick the inner PipelineData directly, and drop the ExitStatus queue.
214        #[cfg(feature = "os")]
215        let body = {
216            let mut data = self.take_reg(reg_id);
217            data.exit.clear();
218            data.body
219        };
220        #[cfg(not(feature = "os"))]
221        let body = self.take_reg(reg_id).body;
222        let span = body.span().unwrap_or(fallback_span);
223        body.into_value(span)
224    }
225
226    /// Get a string from data or produce evaluation error if it's invalid UTF-8
227    fn get_str(&self, slice: DataSlice, error_span: Span) -> Result<&'a str, ShellError> {
228        std::str::from_utf8(&self.data[slice]).map_err(|_| ShellError::IrEvalError {
229            msg: format!("data slice does not refer to valid UTF-8: {slice:?}"),
230            span: Some(error_span),
231        })
232    }
233}
234
235/// Eval an IR block on the provided slice of registers.
236fn eval_ir_block_impl<D: DebugContext>(
237    ctx: &mut EvalContext<'_>,
238    ir_block: &IrBlock,
239    input: PipelineData,
240) -> Result<PipelineExecutionData, ShellError> {
241    if !ctx.registers.is_empty() {
242        ctx.registers[0] = PipelineExecutionData::from(input);
243    }
244
245    // Program counter, starts at zero.
246    let mut pc = 0;
247    let need_backtrace = ctx.engine_state.get_env_var("NU_BACKTRACE").is_some();
248    let mut ret_val = None;
249
250    while pc < ir_block.instructions.len() {
251        let instruction = &ir_block.instructions[pc];
252        let span = &ir_block.spans[pc];
253        let ast = &ir_block.ast[pc];
254
255        D::enter_instruction(ctx.engine_state, ir_block, pc, ctx.registers);
256
257        let result = eval_instruction::<D>(ctx, instruction, span, ast, need_backtrace);
258
259        D::leave_instruction(
260            ctx.engine_state,
261            ir_block,
262            pc,
263            ctx.registers,
264            result.as_ref().err(),
265        );
266
267        match result {
268            Ok(InstructionResult::Continue) => {
269                pc += 1;
270            }
271            Ok(InstructionResult::Branch(next_pc)) => {
272                pc = next_pc;
273            }
274            Ok(InstructionResult::Return(reg_id)) => {
275                // need to check if the return value is set by
276                // `Shell::Return` first. If so, we need to respect that value.
277                match ret_val {
278                    Some(err) => return Err(err),
279                    None => return Ok(ctx.take_reg(reg_id)),
280                }
281            }
282            Err(err @ (ShellError::Continue { .. } | ShellError::Break { .. })) => {
283                return Err(err);
284            }
285            Err(err @ (ShellError::Return { .. } | ShellError::Exit { .. })) => {
286                if let Some(always_run_handler) =
287                    ctx.stack.finally_run_handlers.pop(ctx.finally_handler_base)
288                {
289                    // need to run finally block before return.
290                    // and record the return value firstly.
291                    prepare_error_handler(ctx, always_run_handler, None);
292                    pc = always_run_handler.handler_index;
293                    ret_val = Some(err);
294                } else {
295                    // These block control related errors should be passed through
296                    return Err(err);
297                }
298            }
299            Err(err) => {
300                #[cfg(unix)]
301                let is_terminated_by_signal = matches!(&err, ShellError::TerminatedBySignal { .. });
302                #[cfg(not(unix))]
303                let is_terminated_by_signal = false;
304
305                let is_interrupted =
306                    matches!(err, ShellError::Interrupted { .. }) || is_terminated_by_signal;
307                if let Some(error_handler) = ctx.stack.error_handlers.pop(ctx.error_handler_base) {
308                    if is_interrupted {
309                        ctx.engine_state.signals().reset();
310                    }
311                    // If an error handler is set, branch there
312                    prepare_error_handler(ctx, error_handler, Some(err.into_spanned(*span)));
313                    pc = error_handler.handler_index;
314                } else if let Some(always_run_handler) =
315                    ctx.stack.finally_run_handlers.pop(ctx.finally_handler_base)
316                {
317                    if is_interrupted {
318                        ctx.engine_state.signals().reset();
319                    }
320                    prepare_error_handler(
321                        ctx,
322                        always_run_handler,
323                        Some(err.clone().into_spanned(*span)),
324                    );
325                    pc = always_run_handler.handler_index;
326                    ret_val = Some(err);
327                } else if need_backtrace {
328                    let err = ShellError::into_chained(err, *span);
329                    return Err(err);
330                } else {
331                    return Err(err);
332                }
333            }
334        }
335    }
336
337    // Fell out of the loop, without encountering a Return.
338    Err(ShellError::IrEvalError {
339        msg: format!(
340            "Program counter out of range (pc={pc}, len={len})",
341            len = ir_block.instructions.len(),
342        ),
343        span: *ctx.block_span,
344    })
345}
346
347/// Prepare the context for an error handler
348fn prepare_error_handler(
349    ctx: &mut EvalContext<'_>,
350    error_handler: ErrorHandler,
351    error: Option<Spanned<ShellError>>,
352) {
353    if let Some(reg_id) = error_handler.error_register {
354        if let Some(error) = error {
355            // Stack state has to be updated for stuff like LAST_EXIT_CODE
356            ctx.stack.set_last_error(&error.item);
357            // Create the error value and put it in the register
358            ctx.put_reg(
359                reg_id,
360                PipelineExecutionData::from(
361                    error
362                        .item
363                        .into_full_value(
364                            &StateWorkingSet::new(ctx.engine_state),
365                            ctx.stack,
366                            error.span,
367                        )
368                        .into_pipeline_data(),
369                ),
370            );
371        } else {
372            // Set the register to empty
373            ctx.put_reg(reg_id, PipelineExecutionData::empty());
374        }
375    }
376}
377
378/// The result of performing an instruction. Describes what should happen next
379#[derive(Debug)]
380enum InstructionResult {
381    Continue,
382    Branch(usize),
383    Return(RegId),
384}
385
386/// Perform an instruction
387fn eval_instruction<D: DebugContext>(
388    ctx: &mut EvalContext<'_>,
389    instruction: &Instruction,
390    span: &Span,
391    ast: &Option<IrAstRef>,
392    need_backtrace: bool,
393) -> Result<InstructionResult, ShellError> {
394    use self::InstructionResult::*;
395
396    // Check for interrupt if necessary
397    instruction.check_interrupt(ctx.engine_state, span)?;
398
399    // See the docs for `Instruction` for more information on what these instructions are supposed
400    // to do.
401    match instruction {
402        Instruction::Unreachable => Err(ShellError::IrEvalError {
403            msg: "Reached unreachable code".into(),
404            span: Some(*span),
405        }),
406        Instruction::LoadLiteral { dst, lit } => load_literal(ctx, *dst, lit, *span),
407        Instruction::LoadValue { dst, val } => {
408            ctx.put_reg(
409                *dst,
410                PipelineExecutionData::from(Value::clone(val).into_pipeline_data()),
411            );
412            Ok(Continue)
413        }
414        Instruction::Move { dst, src } => {
415            let val = ctx.take_reg(*src);
416            ctx.put_reg(*dst, val);
417            Ok(Continue)
418        }
419        Instruction::Clone { dst, src } => {
420            let data = ctx.clone_reg(*src, *span)?;
421            ctx.put_reg(*dst, PipelineExecutionData::from(data));
422            Ok(Continue)
423        }
424        Instruction::Collect { src_dst } => {
425            let data = ctx.take_reg(*src_dst);
426            #[cfg(feature = "os")]
427            let value = collect(data, *span, true)?;
428            #[cfg(not(feature = "os"))]
429            let value = collect(data, *span)?;
430            ctx.put_reg(*src_dst, PipelineExecutionData::from(value));
431            Ok(Continue)
432        }
433        Instruction::TryCollect { src_dst } => {
434            let data = ctx.take_reg(*src_dst);
435            #[cfg(feature = "os")]
436            let value = collect(data, *span, false)?;
437            #[cfg(not(feature = "os"))]
438            let value = collect(data, *span)?;
439            ctx.put_reg(*src_dst, PipelineExecutionData::from(value));
440            Ok(Continue)
441        }
442        Instruction::Span { src_dst } => {
443            let mut data = ctx.take_reg(*src_dst);
444            data.body = data.body.with_span(*span);
445            ctx.put_reg(*src_dst, data);
446            Ok(Continue)
447        }
448        Instruction::Drop { src } => {
449            ctx.take_reg(*src);
450            Ok(Continue)
451        }
452        Instruction::Drain { src } => {
453            let data = ctx.take_reg(*src);
454            drain(ctx, data)
455        }
456        Instruction::DrainIfEnd { src } => {
457            let data = ctx.take_reg(*src);
458            let res = drain_if_end(ctx, data)?;
459            ctx.put_reg(*src, PipelineExecutionData::from(res));
460            Ok(Continue)
461        }
462        Instruction::LoadVariable { dst, var_id } => {
463            let value = get_var(ctx, *var_id, *span)?;
464            ctx.put_reg(
465                *dst,
466                PipelineExecutionData::from(value.into_pipeline_data()),
467            );
468            Ok(Continue)
469        }
470        Instruction::StoreVariable { var_id, src } => {
471            let value = ctx.collect_reg(*src, *span)?;
472            // Perform runtime type checking and conversion for variable assignment
473            if nu_experimental::ENFORCE_RUNTIME_ANNOTATIONS.get() {
474                let variable = ctx.engine_state.get_var(*var_id);
475                let converted_value = check_assignment_type(value, &variable.ty)?;
476                ctx.stack.add_var(*var_id, converted_value);
477            } else {
478                ctx.stack.add_var(*var_id, value);
479            }
480            Ok(Continue)
481        }
482        Instruction::DropVariable { var_id } => {
483            ctx.stack.remove_var(*var_id);
484            Ok(Continue)
485        }
486        Instruction::LoadEnv { dst, key } => {
487            let key = ctx.get_str(*key, *span)?;
488            if let Some(value) = get_env_var(ctx, key) {
489                let new_value = value.clone().into_pipeline_data();
490                ctx.put_reg(*dst, PipelineExecutionData::from(new_value));
491                Ok(Continue)
492            } else {
493                // FIXME: using the same span twice, shouldn't this really be
494                // EnvVarNotFoundAtRuntime? There are tests that depend on CantFindColumn though...
495                Err(ShellError::CantFindColumn {
496                    col_name: key.into(),
497                    span: Some(*span),
498                    src_span: *span,
499                })
500            }
501        }
502        Instruction::LoadEnvOpt { dst, key } => {
503            let key = ctx.get_str(*key, *span)?;
504            let value = get_env_var(ctx, key)
505                .cloned()
506                .unwrap_or(Value::nothing(*span));
507            ctx.put_reg(
508                *dst,
509                PipelineExecutionData::from(value.into_pipeline_data()),
510            );
511            Ok(Continue)
512        }
513        Instruction::StoreEnv { key, src } => {
514            let key = ctx.get_str(*key, *span)?;
515            let value = ctx.collect_reg(*src, *span)?;
516
517            let key = get_env_var_name(ctx, key);
518
519            if !is_automatic_env_var(&key) {
520                let is_config = key == "config";
521                let update_conversions = key == ENV_CONVERSIONS;
522
523                ctx.stack.add_env_var(key.into_owned(), value.clone());
524
525                if is_config {
526                    ctx.stack.update_config(ctx.engine_state)?;
527                }
528                if update_conversions {
529                    convert_env_vars(ctx.stack, ctx.engine_state, &value)?;
530                }
531                Ok(Continue)
532            } else {
533                Err(ShellError::AutomaticEnvVarSetManually {
534                    envvar_name: key.into(),
535                    span: *span,
536                })
537            }
538        }
539        Instruction::PushPositional { src } => {
540            let val = ctx.collect_reg(*src, *span)?.with_span(*span);
541            ctx.stack.arguments.push(Argument::Positional {
542                span: *span,
543                val,
544                ast: ast.clone().map(|ast_ref| ast_ref.0),
545            });
546            Ok(Continue)
547        }
548        Instruction::AppendRest { src } => {
549            let vals = ctx.collect_reg(*src, *span)?.with_span(*span);
550            ctx.stack.arguments.push(Argument::Spread {
551                span: *span,
552                vals,
553                ast: ast.clone().map(|ast_ref| ast_ref.0),
554            });
555            Ok(Continue)
556        }
557        Instruction::PushFlag { name } => {
558            let data = ctx.data.clone();
559            ctx.stack.arguments.push(Argument::Flag {
560                data,
561                name: *name,
562                short: DataSlice::empty(),
563                span: *span,
564            });
565            Ok(Continue)
566        }
567        Instruction::PushShortFlag { short } => {
568            let data = ctx.data.clone();
569            ctx.stack.arguments.push(Argument::Flag {
570                data,
571                name: DataSlice::empty(),
572                short: *short,
573                span: *span,
574            });
575            Ok(Continue)
576        }
577        Instruction::PushNamed { name, src } => {
578            let val = ctx.collect_reg(*src, *span)?.with_span(*span);
579            let data = ctx.data.clone();
580            ctx.stack.arguments.push(Argument::Named {
581                data,
582                name: *name,
583                short: DataSlice::empty(),
584                span: *span,
585                val,
586                ast: ast.clone().map(|ast_ref| ast_ref.0),
587            });
588            Ok(Continue)
589        }
590        Instruction::PushShortNamed { short, src } => {
591            let val = ctx.collect_reg(*src, *span)?.with_span(*span);
592            let data = ctx.data.clone();
593            ctx.stack.arguments.push(Argument::Named {
594                data,
595                name: DataSlice::empty(),
596                short: *short,
597                span: *span,
598                val,
599                ast: ast.clone().map(|ast_ref| ast_ref.0),
600            });
601            Ok(Continue)
602        }
603        Instruction::PushParserInfo { name, info } => {
604            let data = ctx.data.clone();
605            ctx.stack.arguments.push(Argument::ParserInfo {
606                data,
607                name: *name,
608                info: info.clone(),
609            });
610            Ok(Continue)
611        }
612        Instruction::RedirectOut { mode } => {
613            ctx.redirect_out = eval_redirection(ctx, mode, *span, RedirectionStream::Out)?;
614            Ok(Continue)
615        }
616        Instruction::RedirectErr { mode } => {
617            ctx.redirect_err = eval_redirection(ctx, mode, *span, RedirectionStream::Err)?;
618            Ok(Continue)
619        }
620        Instruction::CheckErrRedirected { src } => match ctx.borrow_reg(*src) {
621            #[cfg(feature = "os")]
622            PipelineData::ByteStream(stream, _)
623                if matches!(stream.source(), nu_protocol::ByteStreamSource::Child(_)) =>
624            {
625                Ok(Continue)
626            }
627            _ => Err(ShellError::Generic(GenericError::new(
628                "Can't redirect stderr of internal command output",
629                "piping stderr only works on external commands",
630                *span,
631            ))),
632        },
633        Instruction::OpenFile {
634            file_num,
635            path,
636            append,
637        } => {
638            let path = ctx.collect_reg(*path, *span)?;
639            let file = open_file(ctx, &path, *append)?;
640            ctx.files[*file_num as usize] = Some(file);
641            Ok(Continue)
642        }
643        Instruction::WriteFile { file_num, src } => {
644            let src = ctx.take_reg(*src);
645            let file = ctx
646                .files
647                .get(*file_num as usize)
648                .cloned()
649                .flatten()
650                .ok_or_else(|| ShellError::IrEvalError {
651                    msg: format!("Tried to write to file #{file_num}, but it is not open"),
652                    span: Some(*span),
653                })?;
654            let is_external = if let PipelineData::ByteStream(stream, ..) = &src.body {
655                stream.source().is_external()
656            } else {
657                false
658            };
659            if let Err(err) = src.body.write_to(file.as_ref()) {
660                if is_external {
661                    ctx.stack.set_last_error(&err);
662                }
663                Err(err)?
664            } else {
665                Ok(Continue)
666            }
667        }
668        Instruction::CloseFile { file_num } => {
669            if ctx.files[*file_num as usize].take().is_some() {
670                Ok(Continue)
671            } else {
672                Err(ShellError::IrEvalError {
673                    msg: format!("Tried to close file #{file_num}, but it is not open"),
674                    span: Some(*span),
675                })
676            }
677        }
678        Instruction::Call { decl_id, src_dst } => {
679            let input = ctx.take_reg(*src_dst);
680            // take out exit status future first.
681            let input_data = input.body;
682            let mut result = eval_call::<D>(ctx, *decl_id, *span, input_data)?;
683            if need_backtrace {
684                match &mut result {
685                    PipelineData::ByteStream(s, ..) => s.push_caller_span(*span),
686                    PipelineData::ListStream(s, ..) => s.push_caller_span(*span),
687                    _ => (),
688                };
689            }
690            // After eval_call, attach result's exit_status_future
691            // to `original_exit`, so all exit_status_future are tracked
692            // in the new PipelineData, and wrap it into `PipelineExecutionData`
693            #[cfg(feature = "os")]
694            {
695                let mut original_exit = input.exit;
696                // `complete` converts external process status into data (`exit_code`).
697                // Drop inherited exit futures so downstream collect/print/assignment
698                // does not re-raise the same non-zero status via `pipefail`.
699                if ctx.engine_state.get_decl(*decl_id).name() == "complete" {
700                    original_exit.clear();
701                }
702                let result_exit_status_future = result
703                    .clone_exit_status_future()
704                    .map(|f| f.with_span(*span));
705                original_exit.push(result_exit_status_future);
706                ctx.put_reg(
707                    *src_dst,
708                    PipelineExecutionData {
709                        body: result,
710                        exit: original_exit,
711                    },
712                );
713            }
714            #[cfg(not(feature = "os"))]
715            ctx.put_reg(*src_dst, PipelineExecutionData { body: result });
716            Ok(Continue)
717        }
718        Instruction::StringAppend { src_dst, val } => {
719            let string_value = ctx.collect_reg(*src_dst, *span)?;
720            let operand_value = ctx.collect_reg(*val, *span)?;
721            let string_span = string_value.span();
722
723            let mut string = string_value.into_string()?;
724            let operand = if let Value::String { val, .. } = operand_value {
725                // Small optimization, so we don't have to copy the string *again*
726                val
727            } else {
728                operand_value.to_expanded_string(", ", &ctx.stack.get_config(ctx.engine_state))
729            };
730            string.push_str(&operand);
731
732            let new_string_value = Value::string(string, string_span);
733            ctx.put_reg(
734                *src_dst,
735                PipelineExecutionData::from(new_string_value.into_pipeline_data()),
736            );
737            Ok(Continue)
738        }
739        Instruction::GlobFrom { src_dst, no_expand } => {
740            let string_value = ctx.collect_reg(*src_dst, *span)?;
741            let glob_value = if let Value::Glob { .. } = string_value {
742                // It already is a glob, so don't touch it.
743                string_value
744            } else {
745                // Treat it as a string, then cast
746                let string = string_value.into_string()?;
747                Value::glob(string, *no_expand, *span)
748            };
749            ctx.put_reg(
750                *src_dst,
751                PipelineExecutionData::from(glob_value.into_pipeline_data()),
752            );
753            Ok(Continue)
754        }
755        Instruction::ListPush { src_dst, item } => {
756            let list_value = ctx.collect_reg(*src_dst, *span)?;
757            let item = ctx.collect_reg(*item, *span)?;
758            let list_span = list_value.span();
759            let mut list = list_value.into_list()?;
760            list.push(item);
761            ctx.put_reg(
762                *src_dst,
763                PipelineExecutionData::from(Value::list(list, list_span).into_pipeline_data()),
764            );
765            Ok(Continue)
766        }
767        Instruction::ListSpread { src_dst, items } => {
768            let list_value = ctx.collect_reg(*src_dst, *span)?;
769            let items = ctx.collect_reg(*items, *span)?;
770            let list_span = list_value.span();
771            let items_span = items.span();
772            let items = match items {
773                Value::List { vals, .. } => vals,
774                Value::Nothing { .. } => Vec::new(),
775                _ => return Err(ShellError::CannotSpreadAsList { span: items_span }),
776            };
777            let mut list = list_value.into_list()?;
778            list.extend(items);
779            ctx.put_reg(
780                *src_dst,
781                PipelineExecutionData::from(Value::list(list, list_span).into_pipeline_data()),
782            );
783            Ok(Continue)
784        }
785        Instruction::RecordInsert { src_dst, key, val } => {
786            let record_value = ctx.collect_reg(*src_dst, *span)?;
787            let key = ctx.collect_reg(*key, *span)?;
788            let val = ctx.collect_reg(*val, *span)?;
789            let record_span = record_value.span();
790            let mut record = record_value.into_record()?;
791
792            let key = key.coerce_into_string()?;
793            if let Some(old_value) = record.insert(&key, val) {
794                return Err(ShellError::ColumnDefinedTwice {
795                    col_name: key,
796                    second_use: *span,
797                    first_use: old_value.span(),
798                });
799            }
800
801            ctx.put_reg(
802                *src_dst,
803                PipelineExecutionData::from(
804                    Value::record(record, record_span).into_pipeline_data(),
805                ),
806            );
807            Ok(Continue)
808        }
809        Instruction::RecordSpread { src_dst, items } => {
810            let record_value = ctx.collect_reg(*src_dst, *span)?;
811            let items = ctx.collect_reg(*items, *span)?;
812            let record_span = record_value.span();
813            let items_span = items.span();
814            let mut record = record_value.into_record()?;
815            let items = match items {
816                Value::Record { val, .. } => val.into_owned(),
817                Value::Nothing { .. } => Record::new(),
818                _ => return Err(ShellError::CannotSpreadAsRecord { span: items_span }),
819            };
820            // Not using .extend() here because it doesn't handle duplicates
821            for (key, val) in items {
822                if let Some(first_value) = record.insert(&key, val) {
823                    return Err(ShellError::ColumnDefinedTwice {
824                        col_name: key,
825                        second_use: *span,
826                        first_use: first_value.span(),
827                    });
828                }
829            }
830            ctx.put_reg(
831                *src_dst,
832                PipelineExecutionData::from(
833                    Value::record(record, record_span).into_pipeline_data(),
834                ),
835            );
836            Ok(Continue)
837        }
838        Instruction::Not { src_dst } => {
839            let bool = ctx.collect_reg(*src_dst, *span)?;
840            let negated = !bool.as_bool()?;
841            ctx.put_reg(
842                *src_dst,
843                PipelineExecutionData::from(Value::bool(negated, bool.span()).into_pipeline_data()),
844            );
845            Ok(Continue)
846        }
847        Instruction::BinaryOp { lhs_dst, op, rhs } => binary_op(ctx, *lhs_dst, op, *rhs, *span),
848        Instruction::FollowCellPath { src_dst, path } => {
849            let data = ctx.take_reg(*src_dst);
850            let path = ctx.take_reg(*path);
851            if let PipelineData::Value(Value::CellPath { val: path, .. }, _) = path.body {
852                let value = data.body.follow_cell_path(&path.members, *span)?;
853                ctx.put_reg(
854                    *src_dst,
855                    PipelineExecutionData::from(value.into_pipeline_data()),
856                );
857                Ok(Continue)
858            } else if let PipelineData::Value(Value::Error { error, .. }, _) = path.body {
859                Err(*error)
860            } else {
861                Err(ShellError::TypeMismatch {
862                    err_message: "expected cell path".into(),
863                    span: path.span().unwrap_or(*span),
864                })
865            }
866        }
867        Instruction::CloneCellPath { dst, src, path } => {
868            let value = ctx.clone_reg_value(*src, *span)?;
869            let path = ctx.take_reg(*path);
870            if let PipelineData::Value(Value::CellPath { val: path, .. }, _) = path.body {
871                let value = value.follow_cell_path(&path.members)?;
872                ctx.put_reg(
873                    *dst,
874                    PipelineExecutionData::from(value.into_owned().into_pipeline_data()),
875                );
876                Ok(Continue)
877            } else if let PipelineData::Value(Value::Error { error, .. }, _) = path.body {
878                Err(*error)
879            } else {
880                Err(ShellError::TypeMismatch {
881                    err_message: "expected cell path".into(),
882                    span: path.span().unwrap_or(*span),
883                })
884            }
885        }
886        Instruction::UpsertCellPath {
887            src_dst,
888            path,
889            new_value,
890        } => {
891            let mut data = ctx.take_reg(*src_dst).body;
892            let metadata = data.take_metadata();
893            // Change the span because we're modifying it
894            let mut value = data.into_value(*span)?;
895            let path = ctx.take_reg(*path);
896            let new_value = ctx.collect_reg(*new_value, *span)?;
897            if let PipelineData::Value(Value::CellPath { val: path, .. }, _) = path.body {
898                value.upsert_data_at_cell_path(&path.members, new_value)?;
899                ctx.put_reg(
900                    *src_dst,
901                    PipelineExecutionData::from(value.into_pipeline_data_with_metadata(metadata)),
902                );
903                Ok(Continue)
904            } else if let PipelineData::Value(Value::Error { error, .. }, _) = path.body {
905                Err(*error)
906            } else {
907                Err(ShellError::TypeMismatch {
908                    err_message: "expected cell path".into(),
909                    span: path.span().unwrap_or(*span),
910                })
911            }
912        }
913        Instruction::Jump { index } => Ok(Branch(*index)),
914        Instruction::BranchIf { cond, index } => {
915            let data = ctx.take_reg(*cond);
916            let data_span = data.span();
917            let val = match data.body {
918                PipelineData::Value(Value::Bool { val, .. }, _) => val,
919                PipelineData::Value(Value::Error { error, .. }, _) => {
920                    return Err(*error);
921                }
922                _ => {
923                    return Err(ShellError::TypeMismatch {
924                        err_message: "expected bool".into(),
925                        span: data_span.unwrap_or(*span),
926                    });
927                }
928            };
929            if val {
930                Ok(Branch(*index))
931            } else {
932                Ok(Continue)
933            }
934        }
935        Instruction::BranchIfEmpty { src, index } => {
936            let is_empty = matches!(
937                ctx.borrow_reg(*src),
938                PipelineData::Empty | PipelineData::Value(Value::Nothing { .. }, _)
939            );
940
941            if is_empty {
942                Ok(Branch(*index))
943            } else {
944                Ok(Continue)
945            }
946        }
947        Instruction::Match {
948            pattern,
949            src,
950            index,
951        } => {
952            let value = ctx.clone_reg_value(*src, *span)?;
953            ctx.matches.clear();
954            if pattern.match_value(&value, &mut ctx.matches) {
955                // Match succeeded: set variables and branch
956                for (var_id, match_value) in ctx.matches.drain(..) {
957                    ctx.stack.add_var(var_id, match_value);
958                }
959                Ok(Branch(*index))
960            } else {
961                // Failed to match, put back original value
962                ctx.matches.clear();
963                Ok(Continue)
964            }
965        }
966        Instruction::CheckMatchGuard { src } => {
967            if matches!(
968                ctx.borrow_reg(*src),
969                PipelineData::Value(Value::Bool { .. }, _)
970            ) {
971                Ok(Continue)
972            } else {
973                Err(ShellError::MatchGuardNotBool { span: *span })
974            }
975        }
976        Instruction::Iterate {
977            dst,
978            stream,
979            end_index,
980        } => eval_iterate(ctx, *dst, *stream, *end_index, *span),
981        Instruction::OnError { index } => {
982            ctx.stack.error_handlers.push(ErrorHandler {
983                handler_index: *index,
984                error_register: None,
985            });
986            Ok(Continue)
987        }
988        Instruction::OnErrorInto { index, dst } => {
989            ctx.stack.error_handlers.push(ErrorHandler {
990                handler_index: *index,
991                error_register: Some(*dst),
992            });
993            Ok(Continue)
994        }
995        Instruction::Finally { index } => {
996            ctx.stack.finally_run_handlers.push(ErrorHandler {
997                handler_index: *index,
998                error_register: None,
999            });
1000            Ok(Continue)
1001        }
1002        Instruction::FinallyInto { index, dst } => {
1003            ctx.stack.finally_run_handlers.push(ErrorHandler {
1004                handler_index: *index,
1005                error_register: Some(*dst),
1006            });
1007            Ok(Continue)
1008        }
1009        Instruction::PopErrorHandler => {
1010            ctx.stack.error_handlers.pop(ctx.error_handler_base);
1011            Ok(Continue)
1012        }
1013        Instruction::PopFinallyRun => {
1014            ctx.stack.finally_run_handlers.pop(ctx.finally_handler_base);
1015            Ok(Continue)
1016        }
1017        Instruction::ReturnEarly { src } => {
1018            let val = ctx.collect_reg(*src, *span)?;
1019            Err(ShellError::Return {
1020                span: *span,
1021                value: Box::new(val),
1022            })
1023        }
1024        Instruction::Return { src } => Ok(Return(*src)),
1025    }
1026}
1027
1028/// Load a literal value into a register
1029fn load_literal(
1030    ctx: &mut EvalContext<'_>,
1031    dst: RegId,
1032    lit: &Literal,
1033    span: Span,
1034) -> Result<InstructionResult, ShellError> {
1035    // `Literal::Empty` represents "no pipeline input" and should produce
1036    // `PipelineData::Empty`. This is distinct from `Literal::Nothing` which
1037    // represents the `null` value and should produce `PipelineData::Value(Value::Nothing)`.
1038    // Some commands (like `metadata`) distinguish between these when deciding
1039    // whether positional args are allowed.
1040    if matches!(lit, Literal::Empty) {
1041        ctx.put_reg(dst, PipelineExecutionData::empty());
1042    } else {
1043        let value = literal_value(ctx, lit, span)?;
1044        ctx.put_reg(
1045            dst,
1046            PipelineExecutionData::from(PipelineData::value(value, None)),
1047        );
1048    }
1049    Ok(InstructionResult::Continue)
1050}
1051
1052fn literal_value(
1053    ctx: &mut EvalContext<'_>,
1054    lit: &Literal,
1055    span: Span,
1056) -> Result<Value, ShellError> {
1057    Ok(match lit {
1058        Literal::Bool(b) => Value::bool(*b, span),
1059        Literal::Int(i) => Value::int(*i, span),
1060        Literal::Float(f) => Value::float(*f, span),
1061        Literal::Filesize(q) => Value::filesize(*q, span),
1062        Literal::Duration(q) => Value::duration(*q, span),
1063        Literal::Binary(bin) => Value::binary(&ctx.data[*bin], span),
1064        Literal::Block(block_id) | Literal::RowCondition(block_id) | Literal::Closure(block_id) => {
1065            let block = ctx.engine_state.get_block(*block_id);
1066            let captures = block
1067                .captures
1068                .iter()
1069                .map(|(var_id, span)| get_var(ctx, *var_id, *span).map(|val| (*var_id, val)))
1070                .collect::<Result<Vec<_>, ShellError>>()?;
1071            Value::closure(
1072                Closure {
1073                    block_id: *block_id,
1074                    captures,
1075                },
1076                span,
1077            )
1078        }
1079        Literal::Range {
1080            start,
1081            step,
1082            end,
1083            inclusion,
1084        } => {
1085            let start = ctx.collect_reg(*start, span)?;
1086            let step = ctx.collect_reg(*step, span)?;
1087            let end = ctx.collect_reg(*end, span)?;
1088            let range = Range::new(start, step, end, *inclusion, span)?;
1089            Value::range(range, span)
1090        }
1091        Literal::List { capacity } => Value::list(Vec::with_capacity(*capacity), span),
1092        Literal::Record { capacity } => Value::record(Record::with_capacity(*capacity), span),
1093        Literal::Filepath {
1094            val: path,
1095            no_expand,
1096        } => {
1097            let path = ctx.get_str(*path, span)?;
1098            if *no_expand {
1099                Value::string(path, span)
1100            } else {
1101                let path = expand_path(path, true);
1102                Value::string(path.to_string_lossy(), span)
1103            }
1104        }
1105        Literal::Directory {
1106            val: path,
1107            no_expand,
1108        } => {
1109            let path = ctx.get_str(*path, span)?;
1110            if path == "-" {
1111                Value::string("-", span)
1112            } else if *no_expand {
1113                Value::string(path, span)
1114            } else {
1115                let path = expand_path(path, true);
1116                Value::string(path.to_string_lossy(), span)
1117            }
1118        }
1119        Literal::GlobPattern { val, no_expand } => {
1120            Value::glob(ctx.get_str(*val, span)?, *no_expand, span)
1121        }
1122        Literal::String(s) => Value::string(ctx.get_str(*s, span)?, span),
1123        Literal::RawString(s) => Value::string(ctx.get_str(*s, span)?, span),
1124        Literal::CellPath(path) => Value::cell_path(CellPath::clone(path), span),
1125        Literal::Date(dt) => Value::date(**dt, span),
1126        Literal::Nothing => Value::nothing(span),
1127        // Empty is handled specially in load_literal and should never reach here
1128        Literal::Empty => Value::nothing(span),
1129    })
1130}
1131
1132fn binary_op(
1133    ctx: &mut EvalContext<'_>,
1134    lhs_dst: RegId,
1135    op: &Operator,
1136    rhs: RegId,
1137    span: Span,
1138) -> Result<InstructionResult, ShellError> {
1139    let lhs_val = ctx.collect_reg(lhs_dst, span)?;
1140    let rhs_val = ctx.collect_reg(rhs, span)?;
1141
1142    // Handle binary op errors early
1143    if let Value::Error { error, .. } = lhs_val {
1144        return Err(*error);
1145    }
1146    if let Value::Error { error, .. } = rhs_val {
1147        return Err(*error);
1148    }
1149
1150    // We only have access to one span here, but the generated code usually adds a `span`
1151    // instruction to set the output span to the right span.
1152    let op_span = span;
1153
1154    let result = match op {
1155        Operator::Comparison(cmp) => match cmp {
1156            Comparison::Equal => lhs_val.eq(op_span, &rhs_val, span)?,
1157            Comparison::NotEqual => lhs_val.ne(op_span, &rhs_val, span)?,
1158            Comparison::LessThan => lhs_val.lt(op_span, &rhs_val, span)?,
1159            Comparison::GreaterThan => lhs_val.gt(op_span, &rhs_val, span)?,
1160            Comparison::LessThanOrEqual => lhs_val.lte(op_span, &rhs_val, span)?,
1161            Comparison::GreaterThanOrEqual => lhs_val.gte(op_span, &rhs_val, span)?,
1162            Comparison::RegexMatch => {
1163                lhs_val.regex_match(ctx.engine_state, op_span, &rhs_val, false, span)?
1164            }
1165            Comparison::NotRegexMatch => {
1166                lhs_val.regex_match(ctx.engine_state, op_span, &rhs_val, true, span)?
1167            }
1168            Comparison::In => lhs_val.r#in(op_span, &rhs_val, span)?,
1169            Comparison::NotIn => lhs_val.not_in(op_span, &rhs_val, span)?,
1170            Comparison::Has => lhs_val.has(op_span, &rhs_val, span)?,
1171            Comparison::NotHas => lhs_val.not_has(op_span, &rhs_val, span)?,
1172            Comparison::StartsWith => lhs_val.starts_with(op_span, &rhs_val, span)?,
1173            Comparison::NotStartsWith => lhs_val.not_starts_with(op_span, &rhs_val, span)?,
1174            Comparison::EndsWith => lhs_val.ends_with(op_span, &rhs_val, span)?,
1175            Comparison::NotEndsWith => lhs_val.not_ends_with(op_span, &rhs_val, span)?,
1176        },
1177        Operator::Math(mat) => match mat {
1178            Math::Add => lhs_val.add(op_span, &rhs_val, span)?,
1179            Math::Subtract => lhs_val.sub(op_span, &rhs_val, span)?,
1180            Math::Multiply => lhs_val.mul(op_span, &rhs_val, span)?,
1181            Math::Divide => lhs_val.div(op_span, &rhs_val, span)?,
1182            Math::FloorDivide => lhs_val.floor_div(op_span, &rhs_val, span)?,
1183            Math::Modulo => lhs_val.modulo(op_span, &rhs_val, span)?,
1184            Math::Pow => lhs_val.pow(op_span, &rhs_val, span)?,
1185            Math::Concatenate => lhs_val.concat(op_span, &rhs_val, span)?,
1186        },
1187        Operator::Boolean(bl) => match bl {
1188            Boolean::Or => lhs_val.or(op_span, &rhs_val, span)?,
1189            Boolean::Xor => lhs_val.xor(op_span, &rhs_val, span)?,
1190            Boolean::And => lhs_val.and(op_span, &rhs_val, span)?,
1191        },
1192        Operator::Bits(bit) => match bit {
1193            Bits::BitOr => lhs_val.bit_or(op_span, &rhs_val, span)?,
1194            Bits::BitXor => lhs_val.bit_xor(op_span, &rhs_val, span)?,
1195            Bits::BitAnd => lhs_val.bit_and(op_span, &rhs_val, span)?,
1196            Bits::ShiftLeft => lhs_val.bit_shl(op_span, &rhs_val, span)?,
1197            Bits::ShiftRight => lhs_val.bit_shr(op_span, &rhs_val, span)?,
1198        },
1199        Operator::Assignment(_asg) => {
1200            return Err(ShellError::IrEvalError {
1201                msg: "can't eval assignment with the `binary-op` instruction".into(),
1202                span: Some(span),
1203            });
1204        }
1205    };
1206
1207    ctx.put_reg(
1208        lhs_dst,
1209        PipelineExecutionData::from(PipelineData::value(result, None)),
1210    );
1211
1212    Ok(InstructionResult::Continue)
1213}
1214
1215/// Evaluate a call
1216fn eval_call<D: DebugContext>(
1217    ctx: &mut EvalContext<'_>,
1218    decl_id: DeclId,
1219    head: Span,
1220    mut input: PipelineData,
1221) -> Result<PipelineData, ShellError> {
1222    let EvalContext {
1223        engine_state,
1224        stack: caller_stack,
1225        args_base,
1226        redirect_out,
1227        redirect_err,
1228        ..
1229    } = ctx;
1230
1231    let args_len = caller_stack.arguments.get_len(*args_base);
1232    let decl = engine_state.get_decl(decl_id);
1233
1234    // Set up redirect modes
1235    let mut caller_stack = caller_stack.push_redirection(redirect_out.take(), redirect_err.take());
1236
1237    let result = (|| {
1238        if let Some(block_id) = decl.block_id() {
1239            // If the decl is a custom command
1240            let block = engine_state.get_block(block_id);
1241
1242            // check types after acquiring block to avoid unnecessarily cloning Signature
1243            check_input_types(&input, &block.signature, head)?;
1244
1245            // Set up a callee stack with the captures and move arguments from the stack into variables
1246            let mut callee_stack = caller_stack.gather_captures(engine_state, &block.captures);
1247
1248            gather_arguments(
1249                engine_state,
1250                block,
1251                &mut caller_stack,
1252                &mut callee_stack,
1253                *args_base,
1254                args_len,
1255                head,
1256            )?;
1257
1258            // Add one to the recursion count, so we don't recurse too deep. Stack overflows are not
1259            // recoverable in Rust.
1260            callee_stack.recursion_count += 1;
1261
1262            let result =
1263                eval_block_with_early_return::<D>(engine_state, &mut callee_stack, block, input)
1264                    .map(|p| p.body);
1265
1266            // Move environment variables back into the caller stack scope if requested to do so
1267            if block.redirect_env {
1268                redirect_env(engine_state, &mut caller_stack, &callee_stack);
1269            }
1270
1271            result
1272        } else {
1273            check_input_types(&input, &decl.signature(), head)?;
1274            // FIXME: precalculate this and save it somewhere
1275            let span = Span::merge_many(
1276                std::iter::once(head).chain(
1277                    caller_stack
1278                        .arguments
1279                        .get_args(*args_base, args_len)
1280                        .iter()
1281                        .flat_map(|arg| arg.span()),
1282                ),
1283            );
1284
1285            let call = Call {
1286                decl_id,
1287                head,
1288                span,
1289                args_base: *args_base,
1290                args_len,
1291            };
1292
1293            // Make sure that iterating value itself can be interrupted.
1294            // e.g: 0..inf | to md
1295            if let PipelineData::Value(v, ..) = &mut input {
1296                v.inject_signals(engine_state);
1297            }
1298            // Run the call
1299            decl.run(engine_state, &mut caller_stack, &(&call).into(), input)
1300        }
1301    })();
1302
1303    drop(caller_stack);
1304
1305    // Important that this runs, to reset state post-call:
1306    ctx.stack.arguments.leave_frame(ctx.args_base);
1307    ctx.redirect_out = None;
1308    ctx.redirect_err = None;
1309
1310    result
1311}
1312
1313fn find_named_var_id(
1314    sig: &Signature,
1315    name: &[u8],
1316    short: &[u8],
1317    span: Span,
1318) -> Result<VarId, ShellError> {
1319    sig.named
1320        .iter()
1321        .find(|n| {
1322            if !n.long.is_empty() {
1323                n.long.as_bytes() == name
1324            } else {
1325                // It's possible to only have a short name and no long name
1326                n.short
1327                    .is_some_and(|s| s.encode_utf8(&mut [0; 4]).as_bytes() == short)
1328            }
1329        })
1330        .ok_or_else(|| ShellError::IrEvalError {
1331            msg: format!(
1332                "block does not have an argument named `{}`",
1333                String::from_utf8_lossy(name)
1334            ),
1335            span: Some(span),
1336        })
1337        .and_then(|flag| expect_named_var_id(flag, span))
1338}
1339
1340fn expect_named_var_id(arg: &Flag, span: Span) -> Result<VarId, ShellError> {
1341    arg.var_id.ok_or_else(|| ShellError::IrEvalError {
1342        msg: format!(
1343            "block signature is missing var id for named arg `{}`",
1344            arg.long
1345        ),
1346        span: Some(span),
1347    })
1348}
1349
1350fn expect_positional_var_id(arg: &PositionalArg, span: Span) -> Result<VarId, ShellError> {
1351    arg.var_id.ok_or_else(|| ShellError::IrEvalError {
1352        msg: format!(
1353            "block signature is missing var id for positional arg `{}`",
1354            arg.name
1355        ),
1356        span: Some(span),
1357    })
1358}
1359
1360/// Move arguments from the stack into variables for a custom command
1361fn gather_arguments(
1362    engine_state: &EngineState,
1363    block: &Block,
1364    caller_stack: &mut Stack,
1365    callee_stack: &mut Stack,
1366    args_base: usize,
1367    args_len: usize,
1368    call_head: Span,
1369) -> Result<(), ShellError> {
1370    let mut positional_iter = block
1371        .signature
1372        .required_positional
1373        .iter()
1374        .map(|p| (p, true))
1375        .chain(
1376            block
1377                .signature
1378                .optional_positional
1379                .iter()
1380                .map(|p| (p, false)),
1381        );
1382
1383    // Arguments that didn't get consumed by required/optional
1384    let mut rest = vec![];
1385    let mut rest_span: Option<Span> = None;
1386
1387    // If the rest param uses ExternalArgument shape (untyped `def --wrapped` or `known extern`),
1388    // tilde and ndots in bare glob values should be expanded, matching `run-external` behavior so
1389    // that `$args | to nuon` shows expanded paths (e.g. `/home/user`) rather than `~`.
1390    // We detect this via `allows_unknown_args`, which is set for all `def --wrapped` and
1391    // `known extern` commands, and only affects `Value::Glob` values (explicit `[...rest: string]`
1392    // produces `Value::String`, not `Value::Glob`, so those are unaffected).
1393    let expand_glob_args = block.signature.allows_unknown_args;
1394
1395    // If we encounter a spread, all further positionals should go to rest
1396    let mut always_spread = false;
1397
1398    for arg in caller_stack.arguments.drain_args(args_base, args_len) {
1399        match arg {
1400            Argument::Positional { span, val, .. } => {
1401                // Don't check next positional arg if we encountered a spread previously
1402                let next = (!always_spread).then(|| positional_iter.next()).flatten();
1403                if let Some((positional_arg, required)) = next {
1404                    let var_id = expect_positional_var_id(positional_arg, span)?;
1405                    if required {
1406                        // By checking the type of the bound variable rather than converting the
1407                        // SyntaxShape here, we might be able to save some allocations and effort
1408                        let variable = engine_state.get_var(var_id);
1409                        check_type(&val, &variable.ty)?;
1410                    }
1411                    callee_stack.add_var(var_id, val);
1412                } else {
1413                    rest_span = Some(rest_span.map_or(val.span(), |s| s.append(val.span())));
1414                    let val = if expand_glob_args {
1415                        expand_external_glob_arg(val)
1416                    } else {
1417                        val
1418                    };
1419                    rest.push(val);
1420                }
1421            }
1422            Argument::Spread {
1423                vals,
1424                span: spread_span,
1425                ..
1426            } => match vals {
1427                Value::List { vals, .. } => {
1428                    rest.extend(vals);
1429                    rest_span = Some(rest_span.map_or(spread_span, |s| s.append(spread_span)));
1430                    always_spread = true;
1431                }
1432                Value::Nothing { .. } => {
1433                    rest_span = Some(rest_span.map_or(spread_span, |s| s.append(spread_span)));
1434                    always_spread = true;
1435                }
1436                Value::Error { error, .. } => return Err(*error),
1437                _ => return Err(ShellError::CannotSpreadAsList { span: vals.span() }),
1438            },
1439            Argument::Flag {
1440                data,
1441                name,
1442                short,
1443                span,
1444            } => {
1445                let var_id = find_named_var_id(&block.signature, &data[name], &data[short], span)?;
1446                callee_stack.add_var(var_id, Value::bool(true, span))
1447            }
1448            Argument::Named {
1449                data,
1450                name,
1451                short,
1452                span,
1453                val,
1454                ..
1455            } => {
1456                let var_id = find_named_var_id(&block.signature, &data[name], &data[short], span)?;
1457                callee_stack.add_var(var_id, val)
1458            }
1459            Argument::ParserInfo { .. } => (),
1460        }
1461    }
1462
1463    // Add the collected rest of the arguments if a spread argument exists
1464    if let Some(rest_arg) = &block.signature.rest_positional {
1465        let rest_span = rest_span.unwrap_or(call_head);
1466        let var_id = expect_positional_var_id(rest_arg, rest_span)?;
1467        callee_stack.add_var(var_id, Value::list(rest, rest_span));
1468    }
1469
1470    // Check for arguments that haven't yet been set and set them to their defaults
1471    for (positional_arg, _) in positional_iter {
1472        let var_id = expect_positional_var_id(positional_arg, call_head)?;
1473        callee_stack.add_var(
1474            var_id,
1475            positional_arg
1476                .default_value
1477                .clone()
1478                .unwrap_or(Value::nothing(call_head)),
1479        );
1480    }
1481
1482    for named_arg in &block.signature.named {
1483        if let Some(var_id) = named_arg.var_id {
1484            // For named arguments, we do this check by looking to see if the variable was set yet on
1485            // the stack. This assumes that the stack's variables was previously empty, but that's a
1486            // fair assumption for a brand new callee stack.
1487            if !callee_stack.vars.iter().any(|(id, _)| *id == var_id) {
1488                let val = if named_arg.arg.is_none() {
1489                    Value::bool(false, call_head)
1490                } else if let Some(value) = &named_arg.default_value {
1491                    value.clone()
1492                } else {
1493                    Value::nothing(call_head)
1494                };
1495                callee_stack.add_var(var_id, val);
1496            }
1497        }
1498    }
1499
1500    Ok(())
1501}
1502
1503/// Type check helper. Produces `CantConvert` error if `val` is not compatible with `ty`.
1504fn check_type(val: &Value, ty: &Type) -> Result<(), ShellError> {
1505    match val {
1506        Value::Error { error, .. } => Err(*error.clone()),
1507        _ if val.is_subtype_of(ty) => Ok(()),
1508        _ => Err(ShellError::CantConvert {
1509            to_type: ty.to_string(),
1510            from_type: val.get_type().to_string(),
1511            span: val.span(),
1512            help: None,
1513        }),
1514    }
1515}
1516
1517/// Type check and convert value for assignment.
1518fn check_assignment_type(val: Value, target_ty: &Type) -> Result<Value, ShellError> {
1519    match val {
1520        Value::Error { error, .. } => Err(*error),
1521        _ if val.is_subtype_of(target_ty) => Ok(val), // No conversion needed, but compatible
1522        _ => Err(ShellError::CantConvert {
1523            to_type: target_ty.to_string(),
1524            from_type: val.get_type().to_string(),
1525            span: val.span(),
1526            help: None,
1527        }),
1528    }
1529}
1530
1531/// Type check pipeline input against command's input types
1532fn check_input_types(
1533    input: &PipelineData,
1534    signature: &Signature,
1535    head: Span,
1536) -> Result<(), ShellError> {
1537    let io_types = &signature.input_output_types;
1538
1539    // If a command doesn't have any input/output types, then treat command input type as any
1540    if io_types.is_empty() {
1541        return Ok(());
1542    }
1543
1544    // If a command only has a nothing input type, then allow any input data
1545    if io_types.iter().all(|(intype, _)| intype == &Type::Nothing) {
1546        return Ok(());
1547    }
1548
1549    match input {
1550        // early return error directly if detected
1551        PipelineData::Value(Value::Error { error, .. }, ..) => return Err(*error.clone()),
1552        // bypass run-time typechecking for custom types
1553        PipelineData::Value(Value::Custom { .. }, ..) => return Ok(()),
1554        _ => (),
1555    }
1556
1557    // Check if the input type is compatible with *any* of the command's possible input types
1558    if io_types
1559        .iter()
1560        .any(|(command_type, _)| input.is_subtype_of(command_type))
1561    {
1562        return Ok(());
1563    }
1564
1565    let input_types: Vec<Type> = io_types.iter().map(|(input, _)| input.clone()).collect();
1566    let expected_string = combined_type_string(&input_types, "and");
1567
1568    match (input, expected_string) {
1569        (PipelineData::Empty, _) => Err(ShellError::PipelineEmpty { dst_span: head }),
1570        (_, Some(expected_string)) => Err(ShellError::OnlySupportsThisInputType {
1571            exp_input_type: expected_string,
1572            wrong_type: input.get_type().to_string(),
1573            dst_span: head,
1574            src_span: input.span().unwrap_or(head),
1575        }),
1576        // expected_string didn't generate properly, so we can't show the proper error
1577        (_, None) => Err(ShellError::NushellFailed {
1578            msg: "Command input type strings is empty, despite being non-zero earlier".to_string(),
1579        }),
1580    }
1581}
1582
1583/// Get variable from [`Stack`] or [`EngineState`]
1584fn get_var(ctx: &EvalContext<'_>, var_id: VarId, span: Span) -> Result<Value, ShellError> {
1585    match var_id {
1586        // $env
1587        ENV_VARIABLE_ID => {
1588            let env_vars = ctx.stack.get_env_vars(ctx.engine_state);
1589            let env_columns = env_vars.keys();
1590            let env_values = env_vars.values();
1591
1592            let mut pairs = env_columns
1593                .map(|x| x.to_string())
1594                .zip(env_values.cloned())
1595                .collect::<Vec<(String, Value)>>();
1596
1597            pairs.sort_by(|a, b| a.0.cmp(&b.0));
1598
1599            Ok(Value::record(pairs.into_iter().collect(), span))
1600        }
1601        _ => ctx.stack.get_var(var_id, span).or_else(|err| {
1602            // $nu is handled by getting constant
1603            if let Some(const_val) = ctx.engine_state.get_constant(var_id).cloned() {
1604                Ok(const_val.with_span(span))
1605            } else {
1606                Err(err)
1607            }
1608        }),
1609    }
1610}
1611
1612/// Get an environment variable (case-insensitive lookup is handled by EnvName)
1613fn get_env_var<'a>(ctx: &'a mut EvalContext<'_>, key: &str) -> Option<&'a Value> {
1614    // Read scopes in order
1615    for overlays in ctx
1616        .stack
1617        .env_vars
1618        .iter()
1619        .rev()
1620        .chain(std::iter::once(&ctx.engine_state.env_vars))
1621    {
1622        // Read overlays in order
1623        for overlay_name in ctx.stack.active_overlays.iter().rev() {
1624            let Some(map) = overlays.get(overlay_name) else {
1625                // Skip if overlay doesn't exist in this scope
1626                continue;
1627            };
1628            let hidden = ctx.stack.env_hidden.get(overlay_name);
1629            let is_hidden = |key: &EnvName| hidden.is_some_and(|hidden| hidden.contains(key));
1630
1631            if let Some(val) = map
1632                // Check for exact match (now case-insensitive due to EnvName)
1633                .get(&EnvName::from(key))
1634                // Skip when encountering an overlay where the key is hidden
1635                .filter(|_| !is_hidden(&EnvName::from(key)))
1636            {
1637                return Some(val);
1638            }
1639        }
1640    }
1641    // Not found
1642    None
1643}
1644
1645/// Get the existing name of an environment variable (case-insensitive lookup is handled by EnvName).
1646/// This is used to implement case preservation of environment variables, so that changing an
1647/// environment variable that already exists always uses the same case.
1648fn get_env_var_name<'a>(ctx: &mut EvalContext<'_>, key: &'a str) -> Cow<'a, str> {
1649    // Read scopes in order
1650    ctx.stack
1651        .env_vars
1652        .iter()
1653        .rev()
1654        .chain(std::iter::once(&ctx.engine_state.env_vars))
1655        .flat_map(|overlays| {
1656            // Read overlays in order
1657            ctx.stack
1658                .active_overlays
1659                .iter()
1660                .rev()
1661                .filter_map(|name| overlays.get(name))
1662        })
1663        .find_map(|map| {
1664            // Check if it exists (case-insensitive due to EnvName)
1665            if map.contains_key(&EnvName::from(key)) {
1666                // Find the existing key to preserve its case
1667                map.keys()
1668                    .find(|k| k.as_str().eq_ignore_case(key))
1669                    .map(|k| Cow::Owned(k.as_str().to_owned()))
1670            } else {
1671                None
1672            }
1673        })
1674        // didn't exist, use the provided key
1675        .unwrap_or(Cow::Borrowed(key))
1676}
1677
1678/// Helper to collect values into [`PipelineData`], preserving original span and metadata
1679///
1680/// The metadata is removed if it is the file data source, as that's just meant to mark streams.
1681///
1682/// It doesn't check pipefail if `ignore_error` is true.
1683fn collect(
1684    pipe: PipelineExecutionData,
1685    fallback_span: Span,
1686    #[cfg(feature = "os")] ignore_error: bool,
1687) -> Result<PipelineData, ShellError> {
1688    let mut data = pipe.body;
1689    let span = data.span().unwrap_or(fallback_span);
1690    let metadata = data.take_metadata().and_then(|m| m.for_collect());
1691    #[cfg(feature = "os")]
1692    if nu_experimental::PIPE_FAIL.get() && !ignore_error {
1693        check_exit_status_future(pipe.exit)?;
1694    }
1695    let value = data.into_value(span)?;
1696    Ok(PipelineData::value(value, metadata))
1697}
1698
1699/// Helper for drain behavior.
1700fn drain(
1701    ctx: &mut EvalContext<'_>,
1702    data: PipelineExecutionData,
1703) -> Result<InstructionResult, ShellError> {
1704    use self::InstructionResult::*;
1705
1706    match data.body {
1707        PipelineData::ByteStream(stream, ..) => {
1708            let span = stream.span();
1709            let callback_spans = stream.get_caller_spans().clone();
1710            if let Err(mut err) = stream.drain() {
1711                ctx.stack.set_last_error(&err);
1712                if callback_spans.is_empty() {
1713                    return Err(err);
1714                } else {
1715                    for s in callback_spans {
1716                        err = ShellError::EvalBlockWithInput {
1717                            span: s,
1718                            sources: vec![err],
1719                        }
1720                    }
1721                    return Err(err);
1722                }
1723            } else {
1724                ctx.stack.set_last_exit_code(0, span);
1725            }
1726        }
1727        PipelineData::ListStream(stream, ..) => {
1728            let callback_spans = stream.get_caller_spans().clone();
1729            if let Err(mut err) = stream.drain() {
1730                if callback_spans.is_empty() {
1731                    return Err(err);
1732                } else {
1733                    for s in callback_spans {
1734                        err = ShellError::EvalBlockWithInput {
1735                            span: s,
1736                            sources: vec![err],
1737                        }
1738                    }
1739                    return Err(err);
1740                }
1741            }
1742        }
1743        PipelineData::Value(..) | PipelineData::Empty => {}
1744    }
1745
1746    let pipefail = nu_experimental::PIPE_FAIL.get();
1747    if !pipefail {
1748        return Ok(Continue);
1749    }
1750    #[cfg(feature = "os")]
1751    {
1752        check_exit_status_future(data.exit).map(|_| Continue)
1753    }
1754    #[cfg(not(feature = "os"))]
1755    Ok(Continue)
1756}
1757
1758/// Helper for drainIfEnd behavior
1759fn drain_if_end(
1760    ctx: &mut EvalContext<'_>,
1761    data: PipelineExecutionData,
1762) -> Result<PipelineData, ShellError> {
1763    let stack = &mut ctx
1764        .stack
1765        .push_redirection(ctx.redirect_out.clone(), ctx.redirect_err.clone());
1766    let result = data.body.drain_to_out_dests(ctx.engine_state, stack)?;
1767
1768    let pipefail = nu_experimental::PIPE_FAIL.get();
1769    if !pipefail {
1770        return Ok(result);
1771    }
1772    #[cfg(feature = "os")]
1773    {
1774        check_exit_status_future(data.exit).map(|_| result)
1775    }
1776    #[cfg(not(feature = "os"))]
1777    Ok(result)
1778}
1779
1780enum RedirectionStream {
1781    Out,
1782    Err,
1783}
1784
1785/// Open a file for redirection
1786fn open_file(ctx: &EvalContext<'_>, path: &Value, append: bool) -> Result<Arc<File>, ShellError> {
1787    let path_expanded =
1788        expand_path_with(path.as_str()?, ctx.engine_state.cwd(Some(ctx.stack))?, true);
1789    let mut options = File::options();
1790    if append {
1791        options.append(true);
1792    } else {
1793        options.write(true).truncate(true);
1794    }
1795    let file = options
1796        .create(true)
1797        .open(&path_expanded)
1798        .map_err(|err| IoError::new(err, path.span(), path_expanded))?;
1799    Ok(Arc::new(file))
1800}
1801
1802/// Set up a [`Redirection`] from a [`RedirectMode`]
1803fn eval_redirection(
1804    ctx: &mut EvalContext<'_>,
1805    mode: &RedirectMode,
1806    span: Span,
1807    which: RedirectionStream,
1808) -> Result<Option<Redirection>, ShellError> {
1809    match mode {
1810        RedirectMode::Pipe => Ok(Some(Redirection::Pipe(OutDest::Pipe))),
1811        RedirectMode::PipeSeparate => Ok(Some(Redirection::Pipe(OutDest::PipeSeparate))),
1812        RedirectMode::Value => Ok(Some(Redirection::Pipe(OutDest::Value))),
1813        RedirectMode::Null => Ok(Some(Redirection::Pipe(OutDest::Null))),
1814        RedirectMode::Inherit => Ok(Some(Redirection::Pipe(OutDest::Inherit))),
1815        RedirectMode::Print => Ok(Some(Redirection::Pipe(OutDest::Print))),
1816        RedirectMode::File { file_num } => {
1817            let file = ctx
1818                .files
1819                .get(*file_num as usize)
1820                .cloned()
1821                .flatten()
1822                .ok_or_else(|| ShellError::IrEvalError {
1823                    msg: format!("Tried to redirect to file #{file_num}, but it is not open"),
1824                    span: Some(span),
1825                })?;
1826            Ok(Some(Redirection::File(file)))
1827        }
1828        RedirectMode::Caller => Ok(match which {
1829            RedirectionStream::Out => ctx.stack.pipe_stdout().cloned().map(Redirection::Pipe),
1830            RedirectionStream::Err => ctx.stack.pipe_stderr().cloned().map(Redirection::Pipe),
1831        }),
1832    }
1833}
1834
1835/// Do an `iterate` instruction. This can be called repeatedly to get more values from an iterable
1836fn eval_iterate(
1837    ctx: &mut EvalContext<'_>,
1838    dst: RegId,
1839    stream: RegId,
1840    end_index: usize,
1841    span: Span,
1842) -> Result<InstructionResult, ShellError> {
1843    let mut data = ctx.take_reg(stream);
1844    if let PipelineData::ListStream(list_stream, _) = &mut data.body {
1845        // Modify the stream, taking one value off, and branching if it's empty
1846        if let Some(val) = list_stream.next_value() {
1847            ctx.put_reg(dst, PipelineExecutionData::from(val.into_pipeline_data()));
1848            ctx.put_reg(stream, data); // put the stream back so it can be iterated on again
1849            Ok(InstructionResult::Continue)
1850        } else {
1851            ctx.put_reg(dst, PipelineExecutionData::empty());
1852            Ok(InstructionResult::Branch(end_index))
1853        }
1854    } else {
1855        // Convert the PipelineData to an iterator, and wrap it in a ListStream so it can be
1856        // iterated on
1857        let metadata = data.body.take_metadata();
1858        let span = data.span().unwrap_or(span);
1859        ctx.put_reg(
1860            stream,
1861            PipelineExecutionData::from(PipelineData::list_stream(
1862                ListStream::new(data.body.into_iter(), span, Signals::EMPTY),
1863                metadata,
1864            )),
1865        );
1866        eval_iterate(ctx, dst, stream, end_index, span)
1867    }
1868}
1869
1870/// Redirect environment from the callee stack to the caller stack
1871fn redirect_env(engine_state: &EngineState, caller_stack: &mut Stack, callee_stack: &Stack) {
1872    // TODO: make this more efficient
1873    // Grab all environment variables from the callee
1874    let caller_env_vars = caller_stack.get_env_var_names(engine_state);
1875
1876    // remove env vars that are present in the caller but not in the callee
1877    // (the callee hid them)
1878    for var in caller_env_vars.iter() {
1879        if !callee_stack.has_env_var(engine_state, var) {
1880            caller_stack.hide_env_var(engine_state, var);
1881        }
1882    }
1883
1884    // add new env vars from callee to caller
1885    for (var, value) in callee_stack.get_stack_env_vars() {
1886        caller_stack.add_env_var(var, value);
1887    }
1888
1889    // set config to callee config, to capture any updates to that
1890    caller_stack.config.clone_from(&callee_stack.config);
1891}