@@ -27,8 +27,12 @@ export class Evaluator {
2727
2828 if ( blockContext . returnCalled ) {
2929 const res = blockContext . returnObject ;
30- blockContext . returnCalled = false ;
31- blockContext . returnObject = null ;
30+
31+ // stop processing return
32+ if ( ast . type == 'func' || ast . type == 'module' ) {
33+ blockContext . returnCalled = false ;
34+ blockContext . returnObject = null ;
35+ }
3236 return res ;
3337 }
3438
@@ -63,8 +67,11 @@ export class Evaluator {
6367 lastResult = this . evalNode ( node , blockContext ) ;
6468 if ( blockContext . returnCalled ) {
6569 const res = blockContext . returnObject ;
66- blockContext . returnCalled = false ;
67- blockContext . returnObject = null ;
70+ // stop processing return
71+ if ( ast . type == 'func' || ast . type == 'module' ) {
72+ blockContext . returnCalled = false ;
73+ blockContext . returnObject = null ;
74+ }
6875 return res ;
6976 }
7077
@@ -81,7 +88,8 @@ export class Evaluator {
8188
8289 private jspyFuncInvoker ( funcDef : FuncDefNode , context : BlockContext , ...args : unknown [ ] ) : unknown {
8390
84- const ast = funcDef . funcAst ;
91+ const ast = Object . assign ( { } , funcDef . funcAst ) ;
92+ ast . type = 'func' ;
8593
8694 const blockContext = {
8795 namelessFuncsCount : 0 ,
@@ -150,9 +158,9 @@ export class Evaluator {
150158 if ( node . type === 'if' ) {
151159 const ifNode = node as IfNode ;
152160 if ( this . evalNode ( ifNode . conditionNode , blockContext ) ) {
153- this . evalBlock ( { body : ifNode . ifBody } as AstBlock , blockContext ) ;
161+ this . evalBlock ( { type : 'if' , body : ifNode . ifBody } as AstBlock , blockContext ) ;
154162 } else if ( ifNode . elseBody ) {
155- this . evalBlock ( { body : ifNode . elseBody } as AstBlock , blockContext ) ;
163+ this . evalBlock ( { type : 'if' , body : ifNode . elseBody } as AstBlock , blockContext ) ;
156164 }
157165
158166 return ;
@@ -185,7 +193,7 @@ export class Evaluator {
185193
186194 for ( let item of array ) {
187195 blockContext . blockScope . set ( forNode . itemVarName , item ) ;
188- this . evalBlock ( { body : forNode . body } as AstBlock , blockContext ) ;
196+ this . evalBlock ( { type : 'for' , body : forNode . body } as AstBlock , blockContext ) ;
189197 if ( blockContext . continueCalled ) { blockContext . continueCalled = false ; }
190198 if ( blockContext . breakCalled ) { break ; }
191199 }
@@ -194,10 +202,10 @@ export class Evaluator {
194202 }
195203
196204 if ( node . type === 'while' ) {
197- const forNode = node as WhileNode ;
205+ const whileNode = node as WhileNode ;
198206
199- while ( this . evalNode ( forNode . condition , blockContext ) ) {
200- this . evalBlock ( { body : forNode . body } as AstBlock , blockContext ) ;
207+ while ( this . evalNode ( whileNode . condition , blockContext ) ) {
208+ this . evalBlock ( { type : 'while' , body : whileNode . body } as AstBlock , blockContext ) ;
201209
202210 if ( blockContext . continueCalled ) { blockContext . continueCalled = false ; }
203211 if ( blockContext . breakCalled ) { break ; }
0 commit comments