diff --git a/lib/src/compiler/ast/block.dart b/lib/src/compiler/ast/block.dart index 9df2468..1f8ac38 100644 --- a/lib/src/compiler/ast/block.dart +++ b/lib/src/compiler/ast/block.dart @@ -10,7 +10,7 @@ import 'stat.dart'; // explist ::= exp {‘,’ exp} class Block extends Node { List stats; - List retExps; + List? retExps; Block({required this.stats,required this.retExps}); @@ -26,9 +26,9 @@ class Block extends Node { } sb.write(']'); } - if(retExps.isNotEmpty){ + if(retExps != null){ sb.write(',\nRetExps:['); - for(var exp in retExps){ + for(var exp in retExps!){ _expToStr(exp,sb); } sb.writeln(']'); diff --git a/lib/src/compiler/codegen/block_processor.dart b/lib/src/compiler/codegen/block_processor.dart index 10c3773..7f249cb 100644 --- a/lib/src/compiler/codegen/block_processor.dart +++ b/lib/src/compiler/codegen/block_processor.dart @@ -13,8 +13,8 @@ class BlockProcessor { StatProcessor.processStat(fi, stat); } - if (node.retExps.isNotEmpty) { - processRetStat(fi, node.retExps, node.lastLine); + if (node.retExps != null) { + processRetStat(fi, node.retExps!, node.lastLine); } } diff --git a/lib/src/compiler/parser/block_parser.dart b/lib/src/compiler/parser/block_parser.dart index f9762b3..8d78c24 100644 --- a/lib/src/compiler/parser/block_parser.dart +++ b/lib/src/compiler/parser/block_parser.dart @@ -42,9 +42,9 @@ class BlockParser { // retstat ::= return [explist] [‘;’] // explist ::= exp {‘,’ exp} - static List parseRetExps(Lexer lexer) { + static List? parseRetExps(Lexer lexer) { if (lexer.LookAhead() != TokenKind.TOKEN_KW_RETURN) { - return List.empty(); + return null; } lexer.nextToken(); diff --git a/test/block/empty_return.lua b/test/block/empty_return.lua new file mode 100644 index 0000000..9758ab7 --- /dev/null +++ b/test/block/empty_return.lua @@ -0,0 +1,10 @@ +x = true + +function empty_return() + if true then + return + end + + -- Should not get here. + x = false +end diff --git a/test/block/empty_return_statement_test.dart b/test/block/empty_return_statement_test.dart new file mode 100644 index 0000000..80b823b --- /dev/null +++ b/test/block/empty_return_statement_test.dart @@ -0,0 +1,21 @@ +import 'dart:io'; + +import 'package:lua_dardo/lua.dart'; +import 'package:test/test.dart'; + +void main() { + test('Empty return statement exits function early', () { + Directory.current = './test/block/'; + + final ls = LuaState.newState(); + ls.openLibs(); + ls.doFile("empty_return.lua"); + + ls.getGlobal("empty_return"); + ls.pCall(0, 0, 1); + + ls.getGlobal("x"); + final result = ls.toBoolean(-1); + expect(result, true); + }); +}