Amirhossein Akhlaghpour 00469de93e
Limit recursion to avoid stack overflow (#7657)
Add recursion limit to `def` and `block`.
Summary of this PR , it will detect if `def` call itself or not .
Then execute by using `stack` which I think best choice to use with this
design and core as it is available in all crates and mutable and
calculate the recursion limit on calling `def`.
Set 50 as recursion limit on `Config`.
Add some tests too .

Fixes #5899

Co-authored-by: Reilly Wood <reilly.wood@icloud.com>
2023-01-04 18:38:50 -08:00

75 lines
1.5 KiB
Rust

use std::ops::{Index, IndexMut};
use crate::{Signature, Span, VarId};
use super::Pipeline;
#[derive(Debug, Clone)]
pub struct Block {
pub signature: Box<Signature>,
pub pipelines: Vec<Pipeline>,
pub captures: Vec<VarId>,
pub redirect_env: bool,
pub span: Option<Span>, // None option encodes no span to avoid using test_span()
pub recursive: Option<bool>, // does the block call itself?
}
impl Block {
pub fn len(&self) -> usize {
self.pipelines.len()
}
pub fn is_empty(&self) -> bool {
self.pipelines.is_empty()
}
}
impl Index<usize> for Block {
type Output = Pipeline;
fn index(&self, index: usize) -> &Self::Output {
&self.pipelines[index]
}
}
impl IndexMut<usize> for Block {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
&mut self.pipelines[index]
}
}
impl Default for Block {
fn default() -> Self {
Self::new()
}
}
impl Block {
pub fn new() -> Self {
Self {
signature: Box::new(Signature::new("")),
pipelines: vec![],
captures: vec![],
redirect_env: false,
span: None,
recursive: None,
}
}
}
impl<T> From<T> for Block
where
T: Iterator<Item = Pipeline>,
{
fn from(pipelines: T) -> Self {
Self {
signature: Box::new(Signature::new("")),
pipelines: pipelines.collect(),
captures: vec![],
redirect_env: false,
span: None,
recursive: None,
}
}
}