@@ -79,8 +79,8 @@ const transformAsyncTry: FunctionVisitor<ts.TryStatement> = (statement, context)
7979 chainCalls . push ( lua . createExpressionStatement ( promiseAwait , statement ) ) ;
8080
8181 const hasReturn = tryScope . asyncTryHasReturn ?? catchScope ?. asyncTryHasReturn ;
82- const hasBreak = tryScope . asyncTryHasBreak ?? catchScope ?. asyncTryHasBreak ;
83- const hasContinue = tryScope . asyncTryHasContinue ?? catchScope ?. asyncTryHasContinue ;
82+ const hasBreak = tryScope . tryHasBreak ?? catchScope ?. tryHasBreak ;
83+ const hasContinue = tryScope . tryHasContinue ?? catchScope ?. tryHasContinue ;
8484
8585 // Build result in output order: flag declarations, awaiter, chain calls, post-checks
8686 const result : lua . Statement [ ] = [ ] ;
@@ -114,7 +114,12 @@ const transformAsyncTry: FunctionVisitor<ts.TryStatement> = (statement, context)
114114
115115 if ( hasBreak ) {
116116 result . push (
117- lua . createIfStatement ( lua . createIdentifier ( "____hasBroken" ) , lua . createBlock ( [ lua . createBreakStatement ( ) ] ) )
117+ lua . createIfStatement (
118+ lua . createIdentifier ( "____hasBroken" , statement ) ,
119+ lua . createBlock ( [ lua . createBreakStatement ( statement ) ] , statement ) ,
120+ undefined ,
121+ statement
122+ )
118123 ) ;
119124 }
120125
@@ -125,21 +130,30 @@ const transformAsyncTry: FunctionVisitor<ts.TryStatement> = (statement, context)
125130 const continueStatements : lua . Statement [ ] = [ ] ;
126131 switch ( hasContinue ) {
127132 case LoopContinued . WithGoto :
128- continueStatements . push ( lua . createGotoStatement ( label ) ) ;
133+ continueStatements . push ( lua . createGotoStatement ( label , statement ) ) ;
129134 break ;
130135 case LoopContinued . WithContinue :
131- continueStatements . push ( lua . createContinueStatement ( ) ) ;
136+ continueStatements . push ( lua . createContinueStatement ( statement ) ) ;
132137 break ;
133138 case LoopContinued . WithRepeatBreak :
134139 continueStatements . push (
135- lua . createAssignmentStatement ( lua . createIdentifier ( label ) , lua . createBooleanLiteral ( true ) )
140+ lua . createAssignmentStatement (
141+ lua . createIdentifier ( label , statement ) ,
142+ lua . createBooleanLiteral ( true ) ,
143+ statement
144+ )
136145 ) ;
137- continueStatements . push ( lua . createBreakStatement ( ) ) ;
146+ continueStatements . push ( lua . createBreakStatement ( statement ) ) ;
138147 break ;
139148 }
140149
141150 result . push (
142- lua . createIfStatement ( lua . createIdentifier ( "____hasContinued" ) , lua . createBlock ( continueStatements ) )
151+ lua . createIfStatement (
152+ lua . createIdentifier ( "____hasContinued" , statement ) ,
153+ lua . createBlock ( continueStatements , statement ) ,
154+ undefined ,
155+ statement
156+ )
143157 ) ;
144158 }
145159
@@ -153,7 +167,7 @@ export const transformTryStatement: FunctionVisitor<ts.TryStatement> = (statemen
153167
154168 const tsTryBlock = statement . tryBlock ;
155169 const tsCatchClause = statement . catchClause ;
156- const [ tryBlock ] = transformScopeBlock ( context , tsTryBlock , ScopeType . Try ) ;
170+ const [ tryBlock , tryScope ] = transformScopeBlock ( context , tsTryBlock , ScopeType . Try ) ;
157171
158172 if (
159173 ( context . options . luaTarget === LuaTarget . Lua50 || context . options . luaTarget === LuaTarget . Lua51 ) &&
@@ -180,11 +194,13 @@ export const transformTryStatement: FunctionVisitor<ts.TryStatement> = (statemen
180194 result . push ( lua . createVariableDeclarationStatement ( tryReturnIdentifiers , tryCall , tsTryBlock ) ) ;
181195
182196 const hasCatch = tsCatchClause && tsCatchClause . block . statements . length > 0 ;
197+ let catchScope : Scope | undefined ;
183198 if ( hasCatch ) {
184199 // local ____catchSuccess
185200 result . push ( lua . createVariableDeclarationStatement ( catchSuccessIdentifier , undefined , tsCatchClause ) ) ;
186201
187- const [ catchFunction ] = transformCatchClause ( context , tsCatchClause ) ;
202+ const [ catchFunction , cScope ] = transformCatchClause ( context , tsCatchClause ) ;
203+ catchScope = cScope ;
188204
189205 const catchIdentifier = lua . createIdentifier ( "____catch" , tsCatchClause ) ;
190206 result . push ( lua . createVariableDeclarationStatement ( catchIdentifier , catchFunction , tsCatchClause ) ) ;
@@ -287,6 +303,71 @@ export const transformTryStatement: FunctionVisitor<ts.TryStatement> = (statemen
287303 ) ;
288304 result . push ( ifTrySuccessStatement ) ;
289305
306+ // local ____hasBroken
307+ // local ____hasContinued
308+ const hasBreak = tryScope . tryHasBreak ?? catchScope ?. tryHasBreak ;
309+ const hasContinue = tryScope . tryHasContinue ?? catchScope ?. tryHasContinue ;
310+
311+ if ( hasBreak || hasContinue !== undefined ) {
312+ const flagDecls : lua . Identifier [ ] = [ ] ;
313+ if ( hasBreak ) flagDecls . push ( lua . createIdentifier ( "____hasBroken" , statement ) ) ;
314+ if ( hasContinue !== undefined ) flagDecls . push ( lua . createIdentifier ( "____hasContinued" , statement ) ) ;
315+ result . unshift ( lua . createVariableDeclarationStatement ( flagDecls , undefined , statement ) ) ;
316+ }
317+
318+ // if ____hasBroken then
319+ // break
320+ // end
321+ if ( hasBreak ) {
322+ result . push (
323+ lua . createIfStatement (
324+ lua . createIdentifier ( "____hasBroken" , statement ) ,
325+ lua . createBlock ( [ lua . createBreakStatement ( statement ) ] , statement ) ,
326+ undefined ,
327+ statement
328+ )
329+ ) ;
330+ }
331+
332+ // if ____hasContinued then
333+ // goto __continueN (Lua 5.2+)
334+ // continue (Luau)
335+ // __continueN = true; break (Lua 5.0/5.1)
336+ // end
337+ if ( hasContinue !== undefined ) {
338+ const loopScope = findScope ( context , ScopeType . Loop ) ;
339+ const label = `__continue${ loopScope ?. id ?? "" } ` ;
340+
341+ const continueStatements : lua . Statement [ ] = [ ] ;
342+ switch ( hasContinue ) {
343+ case LoopContinued . WithGoto :
344+ continueStatements . push ( lua . createGotoStatement ( label , statement ) ) ;
345+ break ;
346+ case LoopContinued . WithContinue :
347+ continueStatements . push ( lua . createContinueStatement ( statement ) ) ;
348+ break ;
349+ case LoopContinued . WithRepeatBreak :
350+ continueStatements . push (
351+ lua . createAssignmentStatement (
352+ lua . createIdentifier ( label , statement ) ,
353+ lua . createBooleanLiteral ( true ) ,
354+ statement
355+ )
356+ ) ;
357+ continueStatements . push ( lua . createBreakStatement ( statement ) ) ;
358+ break ;
359+ }
360+
361+ result . push (
362+ lua . createIfStatement (
363+ lua . createIdentifier ( "____hasContinued" , statement ) ,
364+ lua . createBlock ( continueStatements , statement ) ,
365+ undefined ,
366+ statement
367+ )
368+ ) ;
369+ }
370+
290371 return lua . createDoStatement ( result , statement ) ;
291372} ;
292373
0 commit comments