@@ -221,7 +221,7 @@ var LuaTranspiler = /** @class */ (function () {
221221 } ;
222222 LuaTranspiler . prototype . transpileBreak = function ( ) {
223223 if ( this . transpilingSwitch ) {
224- return this . indent + ( "goto switchDone" + this . genVarCounter + "\n" ) ;
224+ return '' ;
225225 }
226226 else {
227227 return this . indent + "break\n" ;
@@ -312,33 +312,54 @@ var LuaTranspiler = /** @class */ (function () {
312312 var expression = this . transpileExpression ( node . expression , true ) ;
313313 var clauses = node . caseBlock . clauses ;
314314 var result = this . indent + "-------Switch statement start-------\n" ;
315+ var jumpTableName = "____switch" + this . genVarCounter ;
316+ this . genVarCounter ++ ;
317+ result += this . indent + ( "local " + jumpTableName + " = {\n" ) ;
318+ this . pushIndent ( ) ;
315319 // If statement to go to right entry label
316320 clauses . forEach ( function ( clause , index ) {
317321 if ( ts . isCaseClause ( clause ) ) {
318- var keyword = index == 0 ? "if" : "elseif" ;
319- var condition = _this . transpileExpression ( clause . expression , true ) ;
320- result += _this . indent + ( keyword + " " + expression + "==" + condition + " then\n" ) ;
322+ result += _this . indent + "-- case:\n" ;
323+ result += _this . indent + ( "[" + _this . transpileExpression ( clause . expression , true ) + "] = function(self)\n" ) ;
321324 }
322- else {
323- // Default
324- result += _this . indent + "else\n" ;
325+ if ( ts . isDefaultClause ( clause ) ) {
326+ result += _this . indent + "-- default:\n" ;
327+ result += _this . indent + ( "[\"____default" + _this . genVarCounter + "\"] = function(self)\n" ) ;
325328 }
326329 _this . pushIndent ( ) ;
327- // Labels for fallthrough
328- result += _this . indent + ( "::switchCase" + ( _this . genVarCounter + index ) + "::\n" ) ;
329330 _this . transpilingSwitch = true ;
330331 clause . statements . forEach ( function ( statement ) {
331332 result += _this . transpileNode ( statement ) ;
332333 } ) ;
333334 _this . transpilingSwitch = false ;
334- // If this goto is reached, fall through to the next case
335- if ( index < clauses . length - 1 ) {
336- result += _this . indent + ( "goto switchCase" + ( _this . genVarCounter + index + 1 ) + "\n" ) ;
335+ var i = index + 1 ;
336+ if ( i < clauses . length && ! TSHelper_1 . TSHelper . containsStatement ( clause . statements , ts . SyntaxKind . BreakStatement ) ) {
337+ var nextClause = clauses [ i ] ;
338+ while ( i < clauses . length
339+ && ts . isCaseClause ( nextClause )
340+ && nextClause . statements . length === 0 ) {
341+ i ++ ;
342+ nextClause = clauses [ i ] ;
343+ }
344+ if ( i !== index && nextClause ) {
345+ if ( ts . isCaseClause ( nextClause ) ) {
346+ result += _this . indent + ( "self[" + _this . transpileExpression ( nextClause . expression , true ) + "]()\n" ) ;
347+ }
348+ else {
349+ result += _this . indent + ( "self[\"____default" + _this . genVarCounter + "\"]()\n" ) ;
350+ }
351+ }
352+ }
353+ else {
354+ result += _this . indent + "-- break;\n" ;
337355 }
338356 _this . popIndent ( ) ;
357+ result += _this . indent + "end,\n" ;
339358 } ) ;
340- result += this . indent + "end\n" ;
341- result += this . indent + ( "::switchDone" + this . genVarCounter + "::\n" ) ;
359+ this . popIndent ( ) ;
360+ result += this . indent + "}\n" ;
361+ result += this . indent + ( "if " + jumpTableName + "[" + expression + "] then " + jumpTableName + "[" + expression + "](" + jumpTableName + ")\n" ) ;
362+ result += this . indent + ( "elseif " + jumpTableName + "[\"____default" + this . genVarCounter + "\"] then " + jumpTableName + "[\"____default" + this . genVarCounter + "\"](" + jumpTableName + ") end\n" ) ;
342363 result += this . indent + "--------Switch statement end--------\n" ;
343364 //Increment counter for next switch statement
344365 this . genVarCounter += clauses . length ;
0 commit comments