GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||
GeezerVison is showing github.com — Fix try/catch/finally flow by SuperPaintman · Pull Request #1729 · TypeScriptToLua/TypeScriptToLua Conversation
|
| Input | Output (-before, +after) |
|---|---|
declare function compute(this: void): void function foo() { try { compute() } catch (err) { print("Reporting the error ...") throw err; } finally { print("Cleaning up resources...") } } |
function foo(self)
do
local function ____catch(err)
print("Reporting the error ...")
error(err, 0)
end
local ____try, ____hasReturned = pcall(function()
compute()
end)
+ local ____rethrow
if not ____try then
- ____catch(____hasReturned)
+ ____try, ____rethrow = pcall(function()
+ ____catch(____hasReturned)
+ end)
end
do
print("Cleaning up resources...")
end
+ if not ____try then
+ error(____rethrow, 0)
+ end
end
end
|
Logs (-before, +after)
Reporting the error ... +Cleaning up resources... stdin:8: attempt to call global 'compute' (a nil value) stack traceback: [C]: in function 'error' - stdin:5: in function '____catch' - stdin:11: in function 'foo' + stdin:20: in function 'foo' stdin:1: in main chunk
Note: This change only affects try/catch/finally blocks. try/catch and try/finally are unchanged.
We capture any errors from catch block into a ____rethrow variable, then let the finally execute, and only then re-throw the error.
--- ./testdata-old/testcase-0002.lua +++ ./testdata/testcase-0002.lua @@ -1,55 +1,67 @@ local ____exports = {} function ____exports.__main(self) local innerTry = false local innerFinally = false local outerFinally = false local function foo(self) do local function ____catch(e) error((e .. "->") .. "outer.catch", 0) end local ____try, ____hasReturned = pcall(function() do local function ____catch(e) error((e .. "->") .. "inner.catch", 0) end local ____try, ____hasReturned = pcall(function() if innerTry then error("inner.try", 0) end end) + local ____rethrow if not ____try then - ____catch(____hasReturned) + ____try, ____rethrow = pcall(function() + ____catch(____hasReturned) + end) end do if innerFinally then error("inner.finally", 0) end end + if not ____try then + error(____rethrow, 0) + end end end) + local ____rethrow if not ____try then - ____catch(____hasReturned) + ____try, ____rethrow = pcall(function() + ____catch(____hasReturned) + end) end do if outerFinally then error("outer.finally", 0) end end + if not ____try then + error(____rethrow, 0) + end end end local result = "x" do local function ____catch(e) result = e end local ____try, ____hasReturned = pcall(function() foo(nil) end) if not ____try then ____catch(____hasReturned) end end return result end return ____exports
--- ./testdata-old/testcase-0020.lua +++ ./testdata/testcase-0020.lua @@ -1,29 +1,35 @@ local ____exports = {} function ____exports.__main(self) local x = "unevaluated" local function evaluate(self, arg) x = "evaluated" return arg end local function foobar(self) do local function ____catch(e) return true, evaluate(nil, e) end local ____try, ____hasReturned, ____returnValue = pcall(function() error("foobar", 0) end) + local ____rethrow if not ____try then - ____hasReturned, ____returnValue = ____catch(____hasReturned) + ____try, ____rethrow = pcall(function() + ____hasReturned, ____returnValue = ____catch(____hasReturned) + end) end do return "finally" end + if not ____try then + error(____rethrow, 0) + end if ____hasReturned then return ____returnValue end end end return (tostring(foobar(nil)) .. " ") .. x end return ____exports
Closes: #1137
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Looks fine, just needs a npm run fix:prettier
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@Perryvw should be formatted now |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
try/finally does not behave as expected when rethrowing
There was an error while loading. Please reload this page.