|
| 1 | +import * as tstl from "../../../src"; |
1 | 2 | import * as util from "../../util"; |
2 | 3 |
|
3 | 4 | // In Lua 5.0, 5.1, and LuaJIT, break must be the last statement in a block. |
4 | 5 | // Any code after break is a syntax error (e.g. `while true do break; local b = 8 end` |
5 | 6 | // fails with "'end' expected near 'local'"). Lua 5.2+ relaxed this restriction. |
6 | 7 | // TSTL should strip dead code after break on all targets to avoid these errors. |
7 | 8 |
|
| 9 | +function expectNoDeadCode(builder: util.TestBuilder) { |
| 10 | + const lua = builder.getMainLuaCodeChunk(); |
| 11 | + expect(lua).not.toContain("local b = 8"); |
| 12 | +} |
| 13 | + |
| 14 | +const affectedVersions: Record<tstl.LuaTarget, ((builder: util.TestBuilder) => void) | boolean> = { |
| 15 | + [tstl.LuaTarget.Universal]: false, |
| 16 | + [tstl.LuaTarget.Lua50]: builder => builder.tap(expectNoDeadCode).expectToMatchJsResult(), |
| 17 | + [tstl.LuaTarget.Lua51]: builder => builder.tap(expectNoDeadCode).expectToMatchJsResult(), |
| 18 | + [tstl.LuaTarget.Lua52]: false, |
| 19 | + [tstl.LuaTarget.Lua53]: false, |
| 20 | + [tstl.LuaTarget.Lua54]: false, |
| 21 | + [tstl.LuaTarget.Lua55]: false, |
| 22 | + [tstl.LuaTarget.LuaJIT]: builder => builder.tap(expectNoDeadCode), |
| 23 | + [tstl.LuaTarget.Luau]: false, |
| 24 | +}; |
| 25 | + |
8 | 26 | util.testEachVersion( |
9 | 27 | "for dead code after break", |
10 | 28 | () => util.testFunction` |
11 | | - for (let i = 0; i < 10; i++) { break; const b = 8; } |
| 29 | + let result = 0; |
| 30 | + for (let i = 0; i < 10; i++) { result = i; break; const b = 8; } |
| 31 | + return result; |
12 | 32 | `, |
13 | | - util.expectEachVersionExceptJit(builder => builder.expectNoExecutionError()) |
| 33 | + affectedVersions |
14 | 34 | ); |
15 | 35 |
|
16 | 36 | util.testEachVersion( |
17 | 37 | "for..in dead code after break", |
18 | 38 | () => util.testFunction` |
19 | | - for (let a in {"a": 5, "b": 8}) { break; const b = 8; } |
| 39 | + let result = ""; |
| 40 | + for (let a in {"a": 5, "b": 8}) { result = a; break; const b = 8; } |
| 41 | + return result; |
20 | 42 | `, |
21 | | - util.expectEachVersionExceptJit(builder => builder.expectNoExecutionError()) |
| 43 | + affectedVersions |
22 | 44 | ); |
23 | 45 |
|
24 | 46 | util.testEachVersion( |
25 | 47 | "for..of dead code after break", |
26 | 48 | () => util.testFunction` |
27 | | - for (let a of [1,2,4]) { break; const b = 8; } |
| 49 | + let result = 0; |
| 50 | + for (let a of [1,2,4]) { result = a; break; const b = 8; } |
| 51 | + return result; |
28 | 52 | `, |
29 | | - util.expectEachVersionExceptJit(builder => builder.expectNoExecutionError()) |
| 53 | + affectedVersions |
30 | 54 | ); |
31 | 55 |
|
32 | 56 | util.testEachVersion( |
33 | 57 | "while dead code after break", |
34 | 58 | () => util.testFunction` |
| 59 | + let result = "done"; |
35 | 60 | while (true) { break; const b = 8; } |
| 61 | + return result; |
36 | 62 | `, |
37 | | - util.expectEachVersionExceptJit(builder => builder.expectNoExecutionError()) |
| 63 | + affectedVersions |
38 | 64 | ); |
39 | 65 |
|
40 | 66 | util.testEachVersion( |
41 | 67 | "switch dead code after break", |
42 | 68 | () => util.testFunction` |
| 69 | + let result = "none"; |
43 | 70 | switch ("abc" as string) { |
44 | 71 | case "def": |
| 72 | + result = "def"; |
45 | 73 | break; |
46 | 74 | let abc = 4; |
47 | 75 | case "abc": |
| 76 | + result = "abc"; |
48 | 77 | break; |
49 | 78 | let def = 6; |
50 | 79 | } |
| 80 | + return result; |
51 | 81 | `, |
52 | | - util.expectEachVersionExceptJit(builder => builder.expectNoExecutionError()) |
| 82 | + affectedVersions |
53 | 83 | ); |
54 | 84 |
|
55 | 85 | util.testEachVersion( |
56 | 86 | "do-while dead code after break", |
57 | 87 | () => util.testFunction` |
| 88 | + let result = "done"; |
58 | 89 | do { break; const b = 8; } while (true); |
| 90 | + return result; |
59 | 91 | `, |
60 | | - util.expectEachVersionExceptJit(builder => builder.expectNoExecutionError()) |
| 92 | + affectedVersions |
61 | 93 | ); |
0 commit comments