@@ -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 ,
@@ -173,6 +175,7 @@ impl<'src, 'run> Evaluator<'src, 'run> {
173175
174176 let mut evaluator = Self {
175177 assignments : Some ( & module. assignments ) ,
178+ recursion_depth : 0 ,
176179 context : Some ( context) ,
177180 env : BTreeMap :: new ( ) ,
178181 is_dependency : false ,
@@ -237,6 +240,14 @@ impl<'src, 'run> Evaluator<'src, 'run> {
237240 function : & FunctionDefinition < ' src > ,
238241 arguments : & [ Expression < ' src > ] ,
239242 ) -> RunResult < ' src , String > {
243+ let recursion_depth = self . recursion_depth + 1 ;
244+
245+ if recursion_depth == RECURSION_LIMIT {
246+ return Err ( Error :: RecursionLimit {
247+ last : function. name ,
248+ } ) ;
249+ }
250+
240251 let context = * self . context . as_ref ( ) . unwrap ( ) ;
241252
242253 let mut scope = Scope :: root ( ) ;
@@ -261,6 +272,7 @@ impl<'src, 'run> Evaluator<'src, 'run> {
261272 is_dependency : false ,
262273 non_const_assignments : Table :: new ( ) ,
263274 overrides : self . overrides ,
275+ recursion_depth,
264276 scope,
265277 } ;
266278
@@ -611,6 +623,7 @@ impl<'src, 'run> Evaluator<'src, 'run> {
611623 is_dependency,
612624 non_const_assignments : Table :: new ( ) ,
613625 overrides : context. overrides ,
626+ recursion_depth : 0 ,
614627 scope : scope. child ( ) ,
615628 }
616629 }
0 commit comments