lua-users home
lua-l archive

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

On 7/10/2009, at 11:08 PM, Robert G. Jakabosky wrote:
On Wednesday 07, Geoff Leyland wrote:
On 7/10/2009, at 10:24 PM, Jerome Vuarand wrote:
2009/10/7 Geoff Leyland <>:
Is there a way to return an exit code from a Lua script without

I'm using luacov, and if you call os.exit(), it doesn't write any
statistics.  I'd also like my script to return 1 or 0.  As far as I
tell, the return value from a top-level script does not seem to be
used for
an exit code, but hopefully someone can tell me I'm wrong.

Looking at the code in lua.c, it seems the return value of the script
is ignored. It should be easy to patch if a custom Lua executable is
an option to you.

Thanks, I thought someone might say that.  Shame really, it'd be a
nice feature to have in standard Lua (mind you this is the first time
it's come up in a good few years of using Lua)

luacov should wrap os.exit() and dump that stats before calling the normal

I don't know much about how luacov is implemented, but if they have a function that can be called from Lua to dump the stats, then you can write a custom
os.exit() wrapper:

local exit=os.exit
os.exit = function(...)
 luacov.dumpstats() -- I don't know if a funciton like this exists.

luacov does a trick with gc and a temporary file to get its save_stats called on exit:

local luacovlock = os.tmpname()

function on_exit()
   stats.save_stats(data, statsfile)

on_exit_trick =, "w")
debug.setmetatable(on_exit_trick, { __gc = on_exit } )

This doesn't seem to happen on os.exit() - I guess os.exit() forces shutdown without gc?

I could call luacov's save_stats directly or I could use luacov's tick facility, but I only use luacov sometimes, as lua -lluacov on the command line, so it would be nice to have my script know nothing about luacov, and still be able to return a 0 or 1. And it so nearly works.