__gc metamethod and error()

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

__gc metamethod and error()

Pavel
setmetatable ({}, {__gc = function() print(1) error("error")
print(2) end})

It looks like at the end, garbage collector also collects
something required by error() function first. So there is no
error message from the code above, lua 5.3.5.

Reply | Threaded
Open this post in threaded view
|

Re: __gc metamethod and error()

Andrew Gierth
>>>>> "temp" == temp 213 qwe <[hidden email]> writes:

 temp> setmetatable ({}, {__gc = function() print(1) error("error")
 temp> print(2) end})

 temp> It looks like at the end, garbage collector also collects
 temp> something required by error() function first. So there is no
 temp> error message from the code above, lua 5.3.5.

"at the end"? If you mean when calling lua_close on the state, then the
error() thrown is just ignored. __gc metamethods are always called via a
pcall; in normal operation any error caught by that pcall is rethrown,
interrupting the current round of finalization calls, but while closing
the state it would be pointless to propagate the error (propagate it to
what?) so it gets ignored instead.

--
Andrew.

Reply | Threaded
Open this post in threaded view
|

Re: __gc metamethod and error()

Pavel
On Tue, 01 Jan 2019 23:51:04 +0000
  Andrew Gierth <[hidden email]> wrote:
>>>>>> <[hidden email]> writes:
>
> temp> setmetatable ({}, {__gc = function() print(1) error("error") print(2) end})
> temp> It looks like at the end, garbage collector also collects something required by error() function first. So there is no error message from the code above, lua 5.3.5.
>
> "at the end"? If you mean when calling lua_close on the state, then the error() thrown is just ignored. __gc metamethods are always called via a pcall; in normal operation any error caught by that pcall is rethrown, interrupting the current round of finalization calls, but while closing the state it would be pointless to propagate the error (propagate it to what?) so it gets ignored instead.


I just wanted to use it as a hook to the "end of the script".
And find out that error() function does not work as usual.
I supposed it should be equal to automatic starting garbage
collector as last step.

t = {}
setmetatable (t, {__gc = function() print(1) error("error")
print(2) end})

--
t = nil
collectgarbage()