Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/LuaTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1568,7 +1568,7 @@ export class LuaTransformer {
);
}

public transformForStatement(statement: ts.ForStatement): tstl.Statement[] {
public transformForStatement(statement: ts.ForStatement): tstl.DoStatement {
const result: tstl.Statement[] = [];

if (statement.initializer) {
Expand Down Expand Up @@ -1596,7 +1596,7 @@ export class LuaTransformer {
// while (condition) do ... end
result.push(tstl.createWhileStatement(tstl.createBlock(body), condition));

return result;
return tstl.createDoStatement(result, statement);
}

public transformForOfInitializer(initializer: ts.ForInitializer, expression: tstl.Expression): tstl.Statement {
Expand Down
16 changes: 9 additions & 7 deletions test/translation/lua/continue.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
local i = 0;
while i < 10 do
do
if i < 5 then
goto __continue1;
do
local i = 0;
while i < 10 do
do
if i < 5 then
goto __continue1;
end
end
::__continue1::
i = i + 1;
end
::__continue1::
i = i + 1;
end
22 changes: 12 additions & 10 deletions test/translation/lua/continueConcurrent.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
local i = 0;
while i < 10 do
do
if i < 5 then
goto __continue1;
end
if i == 7 then
goto __continue1;
do
local i = 0;
while i < 10 do
do
if i < 5 then
goto __continue1;
end
if i == 7 then
goto __continue1;
end
end
::__continue1::
i = i + 1;
end
::__continue1::
i = i + 1;
end
32 changes: 18 additions & 14 deletions test/translation/lua/continueNested.lua
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
local i = 0;
while i < 5 do
do
if (i % 2) == 0 then
goto __continue1;
end
local j = 0;
while j < 2 do
do
local i = 0;
while i < 5 do
do
if (i % 2) == 0 then
goto __continue1;
end
do
if j == 1 then
goto __continue3;
local j = 0;
while j < 2 do
do
if j == 1 then
goto __continue3;
end
end
::__continue3::
j = j + 1;
end
end
::__continue3::
j = j + 1;
end
::__continue1::
i = i + 1;
end
::__continue1::
i = i + 1;
end
38 changes: 21 additions & 17 deletions test/translation/lua/continueNestedConcurrent.lua
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
local i = 0;
while i < 5 do
do
if (i % 2) == 0 then
goto __continue1;
end
local j = 0;
while j < 2 do
do
local i = 0;
while i < 5 do
do
if (i % 2) == 0 then
goto __continue1;
end
do
if j == 1 then
goto __continue3;
local j = 0;
while j < 2 do
do
if j == 1 then
goto __continue3;
end
end
::__continue3::
j = j + 1;
end
end
::__continue3::
j = j + 1;
end
if i == 4 then
goto __continue1;
if i == 4 then
goto __continue1;
end
end
::__continue1::
i = i + 1;
end
::__continue1::
i = i + 1;
end
12 changes: 7 additions & 5 deletions test/translation/lua/for.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
local i = 1;
while i <= 100 do
do
do
local i = 1;
while i <= 100 do
do
end
::__continue1::
i = i + 1;
end
::__continue1::
i = i + 1;
end
9 changes: 9 additions & 0 deletions test/unit/loops.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,15 @@ export class LuaLoopTests
Expect(result).toBe(JSON.stringify(expected));
}

@TestCase("for scope")
public forScope(): void {
const code =
`let i = 42;
for (let i = 0; i < 10; ++i) {}
return i;`;
Expect(util.transpileAndExecute(code)).toBe(42);
}

@TestCase({ ["test1"]: 0, ["test2"]: 1, ["test3"]: 2 }, { ["test1"]: 1, ["test2"]: 2, ["test3"]: 3 })
@Test("forin[Object]")
public forinObject(inp: any, expected: any): void
Expand Down