loop with try catch body emitted invalid lua code · Issue #1598 · TypeScriptToLua/TypeScriptToLua · GitHub
Skip to content

loop with try catch body emitted invalid lua code #1598

Open

Description

@admiralnelson
const factionKeys = [
    "bretonnia1",
    "bretonnia1",
    "bretonnia1",
    "bretonnia1",
    "bretonnia12"
]

declare interface OpaqueObject {}
declare function GetFaction(factionKey: string) : OpaqueObject

for (const f of factionKeys) {
    try {
        const nativeInterface = GetFaction(f)
    } catch {
        break
    }
}

for (let i = 0; i < factionKeys.length; i++) {
    try {
        const nativeInterface = GetFaction(f)
    } catch {
        break
    }
}

while(true) {
    try {
        const nativeInterface = GetFaction(factionKeys[0])
    } catch {
        break
    }
}

do {
    try {
        const nativeInterface = GetFaction(factionKeys[0])
    } catch {
        break
    }
} while (!false)

output

--[[ Generated with https://github.com/TypeScriptToLua/TypeScriptToLua ]]
factionKeys = {
    "bretonnia1",
    "bretonnia1",
    "bretonnia1",
    "bretonnia1",
    "bretonnia12"
}
for ____, f in ipairs(factionKeys) do
    do
        local function ____catch()
            break  -- <-- this caused syntax error
        end
        local ____try = pcall(function()
            local nativeInterface = GetFaction(nil, f)
        end)
        if not ____try then
            ____catch()
        end
    end
end
do
    i = 0
    while i < #factionKeys do
        do
            local function ____catch()
                break -- <-- this caused syntax error
            end
            local ____try = pcall(function()
                local nativeInterface = GetFaction(nil, f)
            end)
            if not ____try then
                ____catch()
            end
        end
        i = i + 1
    end
end
while true do
    do
        local function ____catch()
            break -- <-- this caused syntax error
        end
        local ____try = pcall(function()
            local nativeInterface = GetFaction(nil, factionKeys[1])
        end)
        if not ____try then
            ____catch()
        end
    end
end
repeat
    do
        do
            local function ____catch()
                break -- <-- this caused syntax error
            end
            local ____try = pcall(function()
                local nativeInterface = GetFaction(nil, factionKeys[1])
            end)
            if not ____try then
                ____catch()
            end
        end
    end
until false

playground link
https://typescripttolua.github.io/play/#code/MYewdgzgLgBAZgQ2FAluA0gUwJ4RgXhgG0AoGcmAIgCMAnTKcMFBARkoBoyKb7GxmbTt3K8GTFuy4UqdcQMnCZY-oNYAmSiQC6JEgBNMwADYJ6MFGCiZaiYJhgB5AA4IAjgFdMj6gCsjsADeAL4GRqbmcB5gyGhgMADiDABiSKjgABR26WBY2ABcMNC0lgDmAJQwhS7uXj7+yHpwILQwGaCQsHAwIN3ZcXkQlYEiMFC02DAjMjId0DBgCKgAbpgAklY2dg6ESVCpsZlw5aPBMMBLwAAWU6MycggA1qckoSTNrRnGDBYEMAAMAG5fgAeeBpAY4CAAOm+YFKUCuwJQAGoUcNRuNJtMZuQ5rBFit1ptbEgdokUhCjicZGcLlBrrdceQHs9aa89AB3K4ob4ZcZeDEyLFM5n4hZLFCrDbWUn2P57A45LJU3JQoj-bQ0ih0y43HG41kvN76ECi8gig0zcWEqXE2XbBWUw5gFUuwYarWnc5680zI3ss7c3kODIAQkQxggmHKQA

Activity

  1. jamesWalker55 commented on Nov 20, 2024

    @jamesWalker55

    Having the same issue here. Here's a minimal example.

    TypeScript:

    while (true) {
        try {
            error("foo")
        } catch (e) {
            break;
        }
    }

    Generated Lua:

    while true do
        do
            local function ____catch(e)
                break
            end
            local ____try, ____hasReturned = pcall(function()
                error("foo")
            end)
            if not ____try then
                ____catch(____hasReturned)
            end
        end
    end

    Seems having the break inside a function is causing the error.

    Playground link

  2. admiralnelson commented on Nov 28, 2024

    @admiralnelson
    Author
    for (const f of factionKeys) {
        try {
            const nativeInterface = GetFaction(f)
            break //<-- this causes the same problem
        } catch {
            break
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions