@@ -255,23 +255,26 @@ public bool VisitTableConstructorExpressionNode(TableConstructorExpressionNode n
255255 // For the last element, we need to take into account variable arguments and multiple return values.
256256 if ( listItem == lastField )
257257 {
258+ bool isFixedItems = true ;
258259 switch ( listItem . Expression )
259260 {
260261 case CallFunctionExpressionNode call :
261262 CompileCallFunctionExpression ( call , context , false , - 1 ) ;
263+ isFixedItems = false ;
262264 break ;
263265 case CallTableMethodExpressionNode method :
264266 CompileTableMethod ( method , context , false , - 1 ) ;
265267 break ;
266268 case VariableArgumentsExpressionNode varArg :
267269 CompileVariableArgumentsExpression ( varArg , context , - 1 ) ;
270+ isFixedItems = false ;
268271 break ;
269272 default :
270273 listItem . Expression . Accept ( this , context ) ;
271274 break ;
272275 }
273276
274- context . PushInstruction ( Instruction . SetList ( tableRegisterIndex , 0 , arrayBlock ) , listItem . Position ) ;
277+ context . PushInstruction ( Instruction . SetList ( tableRegisterIndex , ( ushort ) ( isFixedItems ? context . StackTopPosition - tableRegisterIndex : 0 ) , arrayBlock ) , listItem . Position ) ;
275278 currentArrayChunkSize = 0 ;
276279 }
277280 else
@@ -635,7 +638,7 @@ public bool VisitTableMethodDeclarationStatementNode(TableMethodDeclarationState
635638 // assign global variable
636639 var first = node . MemberPath [ 0 ] ;
637640 var tableIndex = GetOrLoadIdentifier ( first . Name , context , first . Position , true ) ;
638-
641+
639642 for ( int i = 1 ; i < node . MemberPath . Length - 1 ; i ++ )
640643 {
641644 var member = node . MemberPath [ i ] ;
@@ -736,7 +739,7 @@ public bool VisitIfStatementNode(IfStatementNode node, ScopeCompilationContext c
736739 {
737740 using var endJumpIndexList = new PooledList < int > ( 8 ) ;
738741 var hasElse = node . ElseNodes . Length > 0 ;
739-
742+ var stackPositionToClose = ( byte ) ( context . StackPosition + 1 ) ;
740743 // if
741744 using ( var scopeContext = context . CreateChildScope ( ) )
742745 {
@@ -750,15 +753,15 @@ public bool VisitIfStatementNode(IfStatementNode node, ScopeCompilationContext c
750753 childNode . Accept ( this , scopeContext ) ;
751754 }
752755
756+ stackPositionToClose = scopeContext . HasCapturedLocalVariables ? stackPositionToClose : ( byte ) 0 ;
753757 if ( hasElse )
754758 {
755759 endJumpIndexList . Add ( scopeContext . Function . Instructions . Length ) ;
756- var a = scopeContext . HasCapturedLocalVariables ? scopeContext . StackPosition : ( byte ) 0 ;
757- scopeContext . PushInstruction ( Instruction . Jmp ( a , 0 ) , node . Position , true ) ;
760+ scopeContext . PushInstruction ( Instruction . Jmp ( stackPositionToClose , 0 ) , node . Position , true ) ;
758761 }
759762 else
760763 {
761- scopeContext . TryPushCloseUpValue ( scopeContext . StackPosition , node . Position ) ;
764+ scopeContext . TryPushCloseUpValue ( stackPositionToClose , node . Position ) ;
762765 }
763766
764767 scopeContext . Function . Instructions [ ifPosition ] . SBx = scopeContext . Function . Instructions . Length - 1 - ifPosition ;
@@ -779,16 +782,16 @@ public bool VisitIfStatementNode(IfStatementNode node, ScopeCompilationContext c
779782 childNode . Accept ( this , scopeContext ) ;
780783 }
781784
785+ stackPositionToClose = scopeContext . HasCapturedLocalVariables ? stackPositionToClose : ( byte ) 0 ;
782786 // skip if node doesn't have else statements
783787 if ( hasElse )
784788 {
785789 endJumpIndexList . Add ( scopeContext . Function . Instructions . Length ) ;
786- var a = scopeContext . HasCapturedLocalVariables ? scopeContext . StackPosition : ( byte ) 0 ;
787- scopeContext . PushInstruction ( Instruction . Jmp ( a , 0 ) , node . Position ) ;
790+ scopeContext . PushInstruction ( Instruction . Jmp ( stackPositionToClose , 0 ) , node . Position ) ;
788791 }
789792 else
790793 {
791- scopeContext . TryPushCloseUpValue ( scopeContext . StackPosition , node . Position ) ;
794+ scopeContext . TryPushCloseUpValue ( stackPositionToClose , node . Position ) ;
792795 }
793796
794797 scopeContext . Function . Instructions [ elseifPosition ] . SBx = scopeContext . Function . Instructions . Length - 1 - elseifPosition ;
@@ -821,14 +824,14 @@ public bool VisitRepeatStatementNode(RepeatStatementNode node, ScopeCompilationC
821824 context . Function . LoopLevel ++ ;
822825
823826 using var scopeContext = context . CreateChildScope ( ) ;
824-
827+ var stackPosition = scopeContext . StackPosition ;
825828 foreach ( var childNode in node . Nodes )
826829 {
827830 childNode . Accept ( this , scopeContext ) ;
828831 }
829832
830833 CompileConditionNode ( node . ConditionNode , scopeContext , true ) ;
831- var a = scopeContext . HasCapturedLocalVariables ? scopeContext . StackPosition : ( byte ) 0 ;
834+ var a = scopeContext . HasCapturedLocalVariables ? ( byte ) ( stackPosition + 1 ) : ( byte ) 0 ;
832835 scopeContext . PushInstruction ( Instruction . Jmp ( a , startIndex - scopeContext . Function . Instructions . Length - 1 ) , node . Position ) ;
833836 scopeContext . TryPushCloseUpValue ( scopeContext . StackPosition , node . Position ) ;
834837
@@ -848,6 +851,7 @@ public bool VisitWhileStatementNode(WhileStatementNode node, ScopeCompilationCon
848851 context . Function . LoopLevel ++ ;
849852
850853 using var scopeContext = context . CreateChildScope ( ) ;
854+ var stackPosition = scopeContext . StackPosition ;
851855
852856 foreach ( var childNode in node . Nodes )
853857 {
@@ -860,7 +864,7 @@ public bool VisitWhileStatementNode(WhileStatementNode node, ScopeCompilationCon
860864 scopeContext . Function . Instructions [ conditionIndex ] . SBx = scopeContext . Function . Instructions . Length - 1 - conditionIndex ;
861865
862866 CompileConditionNode ( node . ConditionNode , scopeContext , false ) ;
863- var a = scopeContext . HasCapturedLocalVariables ? scopeContext . StackPosition : ( byte ) 0 ;
867+ var a = scopeContext . HasCapturedLocalVariables ? ( byte ) ( 1 + stackPosition ) : ( byte ) 0 ;
864868 scopeContext . PushInstruction ( Instruction . Jmp ( a , conditionIndex - context . Function . Instructions . Length ) , node . Position ) ;
865869 scopeContext . TryPushCloseUpValue ( scopeContext . StackPosition , node . Position ) ;
866870
0 commit comments