@@ -7,6 +7,7 @@ pub(crate) struct Evaluator<'src: 'run, 'run> {
77 is_dependency : bool ,
88 non_const_assignments : Table < ' src , Name < ' src > > ,
99 overrides : & ' run HashMap < Number , String > ,
10+ recursion_depth : usize ,
1011 scope : Scope < ' src , ' run > ,
1112}
1213
@@ -26,6 +27,7 @@ impl<'src, 'run> Evaluator<'src, 'run> {
2627 ) -> RunResult < ' src , Settings > {
2728 let mut evaluator = Self {
2829 assignments : Some ( assignments) ,
30+ recursion_depth : 0 ,
2931 context : None ,
3032 env : BTreeMap :: new ( ) ,
3133 is_dependency : false ,
@@ -176,6 +178,7 @@ impl<'src, 'run> Evaluator<'src, 'run> {
176178
177179 let mut evaluator = Self {
178180 assignments : Some ( & module. assignments ) ,
181+ recursion_depth : 0 ,
179182 context : Some ( context) ,
180183 env : BTreeMap :: new ( ) ,
181184 is_dependency : false ,
@@ -240,6 +243,14 @@ impl<'src, 'run> Evaluator<'src, 'run> {
240243 function : & FunctionDefinition < ' src > ,
241244 arguments : & [ Expression < ' src > ] ,
242245 ) -> RunResult < ' src , String > {
246+ let recursion_depth = self . recursion_depth + 1 ;
247+
248+ if recursion_depth == RECURSION_LIMIT {
249+ return Err ( Error :: RecursionLimit {
250+ last : function. name ,
251+ } ) ;
252+ }
253+
243254 let context = * self . context . as_ref ( ) . unwrap ( ) ;
244255
245256 let mut scope = Scope :: root ( ) ;
@@ -264,6 +275,7 @@ impl<'src, 'run> Evaluator<'src, 'run> {
264275 is_dependency : false ,
265276 non_const_assignments : Table :: new ( ) ,
266277 overrides : self . overrides ,
278+ recursion_depth,
267279 scope,
268280 } ;
269281
@@ -614,6 +626,7 @@ impl<'src, 'run> Evaluator<'src, 'run> {
614626 is_dependency,
615627 non_const_assignments : Table :: new ( ) ,
616628 overrides : context. overrides ,
629+ recursion_depth : 0 ,
617630 scope : scope. child ( ) ,
618631 }
619632 }
0 commit comments