@@ -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,32 +312,52 @@ 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+ result += this . indent + ( "local " + jumpTableName + " = {\n" ) ;
317+ this . pushIndent ( ) ;
315318 // If statement to go to right entry label
316319 clauses . forEach ( function ( clause , index ) {
317- if ( index !== clauses . length - 1
318- && clause . statements . length !== 0
319- && ! TSHelper_1 . TSHelper . containsStatement ( clause . statements , ts . SyntaxKind . BreakStatement ) ) {
320- throw new TranspileError ( "Missing break, fall through is not allowed." , clause ) ;
321- }
322320 if ( ts . isCaseClause ( clause ) ) {
323- var keyword = index == 0 ? "if" : "elseif" ;
324- var condition = _this . transpileExpression ( clause . expression , true ) ;
325- result += _this . indent + ( keyword + " " + expression + "==" + condition + " then\n" ) ;
321+ result += _this . indent + "-- case:\n" ;
322+ result += _this . indent + ( "[" + _this . transpileExpression ( clause . expression , true ) + "] = function(self)\n" ) ;
326323 }
327- else {
328- // Default
329- result += _this . indent + "else\n" ;
324+ if ( ts . isDefaultClause ( clause ) ) {
325+ result += _this . indent + "-- default:\n" ;
326+ result += _this . indent + ( "[\"____default" + _this . genVarCounter + "\"] = function(self)\n" ) ;
330327 }
331328 _this . pushIndent ( ) ;
332329 _this . transpilingSwitch = true ;
333330 clause . statements . forEach ( function ( statement ) {
334331 result += _this . transpileNode ( statement ) ;
335332 } ) ;
336333 _this . transpilingSwitch = false ;
334+ var i = index + 1 ;
335+ if ( i < clauses . length && ! TSHelper_1 . TSHelper . containsStatement ( clause . statements , ts . SyntaxKind . BreakStatement ) ) {
336+ var nextClause = clauses [ i ] ;
337+ while ( i < clauses . length
338+ && ts . isCaseClause ( nextClause )
339+ && nextClause . statements . length === 0 ) {
340+ nextClause = clauses [ ++ i ] ;
341+ }
342+ if ( i !== index && nextClause ) {
343+ if ( ts . isCaseClause ( nextClause ) ) {
344+ result += _this . indent + ( "self[" + _this . transpileExpression ( nextClause . expression , true ) + "]()\n" ) ;
345+ }
346+ else {
347+ result += _this . indent + ( "self[\"____default" + _this . genVarCounter + "\"]()\n" ) ;
348+ }
349+ }
350+ }
351+ else {
352+ result += _this . indent + "-- break;\n" ;
353+ }
337354 _this . popIndent ( ) ;
355+ result += _this . indent + "end,\n" ;
338356 } ) ;
339- result += this . indent + "end\n" ;
340- result += this . indent + ( "::switchDone" + this . genVarCounter + "::\n" ) ;
357+ this . popIndent ( ) ;
358+ result += this . indent + "}\n" ;
359+ result += this . indent + ( "if " + jumpTableName + "[" + expression + "] then " + jumpTableName + "[" + expression + "](" + jumpTableName + ")\n" ) ;
360+ result += this . indent + ( "elseif " + jumpTableName + "[\"____default" + this . genVarCounter + "\"] then " + jumpTableName + "[\"____default" + this . genVarCounter + "\"](" + jumpTableName + ") end\n" ) ;
341361 result += this . indent + "--------Switch statement end--------\n" ;
342362 //Increment counter for next switch statement
343363 this . genVarCounter += clauses . length ;
0 commit comments