lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Wed, Jul 19, 2017 at 09:00:49PM +0300, Egor Skriptunoff wrote:
> Hi!
> 
> I have the following Lua program:
> 
> setmetatable({}, {__gc = function() print("Hello") end})
> 
> When I run it on my computer, It prints "Hello".
> It also prints "Hello" when I run it on most "run Lua code online" websites:
> rextester.com, jdoodle.com, tutorialspoint.com, ideone.com
> 
> But it prints nothing when I try to run in on www.lua.org/cgi-bin/demo
> Why?

The lua(1) utility will destroy the Lua state after it finishes executing
the specified program(s). But if a program calls os.exit, control never
returns to the utility, and the process might terminate without destroying
the Lua state, in which case nothing further would be garbage collected.

In Lua 5.2 and 5.3, os.exit() takes a second, optional argument to force
destruction of the Lua state. The default behavior is to exit without
destruction. See https://www.lua.org/manual/5.3/manual.html#pdf-os.exit

Similarly, if the demo environment is a long-running process that reuses a
persistent Lua state, it might just be the case that program results are
captured and returned without first forcing collection of all the generated
garbage.

It all depends on how the demo environment is implemented.