Allow optionally returning via a return from main, instead of calling

os.exit.

This ensures a saner teardown of the Lua state, which is mildly helpful
when instrumenting a run...
(e.g., there's a final GC cycle, among other things).
reviewable/pr6976/r4
NiLuJe 4 years ago
parent c0981a55d9
commit 9b6e339239

@ -53,7 +53,9 @@ if G_reader_settings:isTrue("debug") and G_reader_settings:isTrue("debug_verbose
-- Option parsing: -- Option parsing:
local longopts = { local longopts = {
debug = "d", debug = "d",
verbose = "d",
profile = "p", profile = "p",
teardown = "t",
help = "h", help = "h",
} }
@ -64,6 +66,7 @@ local function showusage()
print("-d start in debug mode") print("-d start in debug mode")
print("-v debug in verbose mode") print("-v debug in verbose mode")
print("-p enable Lua code profiling") print("-p enable Lua code profiling")
print("-t teardown via a return instead of calling os.exit")
print("-h show this usage help") print("-h show this usage help")
print("") print("")
print("If you give the name of a directory instead of a file path, a file") print("If you give the name of a directory instead of a file path, a file")
@ -76,6 +79,7 @@ local function showusage()
end end
local Profiler = nil local Profiler = nil
local sane_teardown
local ARGV = arg local ARGV = arg
local argidx = 1 local argidx = 1
while argidx <= #ARGV do while argidx <= #ARGV do
@ -97,6 +101,8 @@ while argidx <= #ARGV do
elseif arg == "-p" then elseif arg == "-p" then
Profiler = require("jit.p") Profiler = require("jit.p")
Profiler.start("la") Profiler.start("la")
elseif arg == "-t" then
sane_teardown = true
else else
-- not a recognized option, should be a filename -- not a recognized option, should be a filename
argidx = argidx - 1 argidx = argidx - 1
@ -335,10 +341,20 @@ local function exitReader()
if Profiler then Profiler.stop() end if Profiler then Profiler.stop() end
if type(exit_code) == "number" then if type(exit_code) == "number" then
os.exit(exit_code) return exit_code
else else
os.exit(0) return 0
end end
end end
exitReader() local ret = exitReader()
if not sane_teardown then
os.exit(ret)
else
-- NOTE: We can unfortunately not return with a custom exit code...
-- But since this should only really be used on the emulator,
-- to ensure a saner teardown of ressources when debugging,
-- it's not a great loss...
return
end

Loading…
Cancel
Save