@@ -223,24 +223,10 @@ export const transformTryStatement: FunctionVisitor<ts.TryStatement> = (statemen
223223 result . push ( ...context . transformStatements ( statement . finallyBlock ) ) ;
224224 }
225225
226- // if ____hasReturnOrError then
227- // return ____returnValue
228- // end
229- const tryReturnValues : lua . Expression [ ] = [ ] ;
230- if ( isInMultiReturnFunction ( context , statement ) ) {
231- tryReturnValues . push ( createUnpackCall ( context , lua . cloneIdentifier ( returnValueIdentifier , statement ) ) ) ;
232- } else {
233- tryReturnValues . push ( lua . cloneIdentifier ( returnValueIdentifier , statement ) ) ;
234- }
235-
236- const returnStatement = createReturnStatement ( context , tryReturnValues , statement ) ;
237- const ifTryReturnStatement = lua . createIfStatement (
238- lua . cloneIdentifier ( hasReturnOrErrorIdentifier , statement ) ,
239- lua . createBlock ( [ returnStatement ] , statement ) ,
240- undefined ,
226+ const trySuccessBlock = lua . createBlock (
227+ [ createReturnIfHasReturnOrError ( context , statement , hasReturnOrErrorIdentifier , returnValueIdentifier ) ] ,
241228 statement
242229 ) ;
243- const trySuccessBlock = lua . createBlock ( [ ifTryReturnStatement ] , statement ) ;
244230
245231 // error(____hasReturnOrError, 0)
246232 const rethrow = lua . createExpressionStatement (
@@ -255,24 +241,10 @@ export const transformTryStatement: FunctionVisitor<ts.TryStatement> = (statemen
255241
256242 let ifCatchSuccessStatement : lua . IfStatement | undefined ;
257243 if ( hasCatch ) {
258- // if ____hasReturnOrError then
259- // return ____returnValue
260- // end
261- const catchReturnValues : lua . Expression [ ] = [ ] ;
262- if ( isInMultiReturnFunction ( context , statement ) ) {
263- catchReturnValues . push ( createUnpackCall ( context , lua . cloneIdentifier ( returnValueIdentifier , statement ) ) ) ;
264- } else {
265- catchReturnValues . push ( lua . cloneIdentifier ( returnValueIdentifier , statement ) ) ;
266- }
267-
268- const catchReturnStatement = createReturnStatement ( context , catchReturnValues , statement ) ;
269- const ifCatchReturnStatement = lua . createIfStatement (
270- lua . cloneIdentifier ( hasReturnOrErrorIdentifier , statement ) ,
271- lua . createBlock ( [ catchReturnStatement ] , statement ) ,
272- undefined ,
244+ const catchSuccessBlock = lua . createBlock (
245+ [ createReturnIfHasReturnOrError ( context , statement , hasReturnOrErrorIdentifier , returnValueIdentifier ) ] ,
273246 statement
274247 ) ;
275- const catchSuccessBlock = lua . createBlock ( [ ifCatchReturnStatement ] , statement ) ;
276248
277249 ifCatchSuccessStatement = lua . createIfStatement (
278250 lua . cloneIdentifier ( catchSuccessIdentifier , statement ) ,
@@ -282,6 +254,20 @@ export const transformTryStatement: FunctionVisitor<ts.TryStatement> = (statemen
282254 ) ;
283255 }
284256
257+ let elseBranch : lua . Block | lua . IfStatement | undefined ;
258+ if ( hasCatch ) {
259+ // try {} catch(e) { ... }
260+ // Non-empty catch: check catch success, otherwise re-throw
261+ elseBranch = ifCatchSuccessStatement ;
262+ } else if ( ! tsCatchClause ) {
263+ // try {} finally {}
264+ // No catch clause: re-throw uncaught error after finally
265+ elseBranch = throwBlock ;
266+ } else {
267+ // try {} catch(e) {}
268+ // Empty catch block: error is intentionally swallowed
269+ }
270+
285271 // if ____trySuccess then
286272 // if ____hasReturnOrError then
287273 // return ____returnValue
@@ -296,7 +282,7 @@ export const transformTryStatement: FunctionVisitor<ts.TryStatement> = (statemen
296282 const ifTrySuccessStatement = lua . createIfStatement (
297283 lua . cloneIdentifier ( trySuccessIdentifier , statement ) ,
298284 trySuccessBlock ,
299- ifCatchSuccessStatement ?? ( tsCatchClause ? undefined : throwBlock ) ,
285+ elseBranch ,
300286 statement
301287 ) ;
302288 result . push ( ifTrySuccessStatement ) ;
@@ -334,3 +320,26 @@ function transformCatchClause(
334320
335321 return [ catchFunction , catchScope ] ;
336322}
323+
324+ // if ____hasReturnOrError then
325+ // return ____returnValue
326+ // end
327+ function createReturnIfHasReturnOrError (
328+ context : TransformationContext ,
329+ statement : ts . TryStatement ,
330+ hasReturnOrErrorIdentifier : lua . Identifier ,
331+ returnValueIdentifier : lua . Identifier
332+ ) : lua . IfStatement {
333+ const returnValues : lua . Expression [ ] = [ ] ;
334+ if ( isInMultiReturnFunction ( context , statement ) ) {
335+ returnValues . push ( createUnpackCall ( context , lua . cloneIdentifier ( returnValueIdentifier , statement ) ) ) ;
336+ } else {
337+ returnValues . push ( lua . cloneIdentifier ( returnValueIdentifier , statement ) ) ;
338+ }
339+ return lua . createIfStatement (
340+ lua . cloneIdentifier ( hasReturnOrErrorIdentifier , statement ) ,
341+ lua . createBlock ( [ createReturnStatement ( context , returnValues , statement ) ] , statement ) ,
342+ undefined ,
343+ statement
344+ ) ;
345+ }
0 commit comments