summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2023-02-21 23:12:54 +0800
committerruki <[email protected]>2023-02-21 23:12:54 +0800
commiteec6de9e8b6456aa13b588ecce47af07fcfba965 (patch)
tree4f76bf9ac5c745708d5494c1b3b61b6bbc2f6baa
parent94c2973e49202add48796a75f9b5f64f4f8bb6f1 (diff)
add os.atexit
-rw-r--r--xmake/core/base/os.lua28
-rw-r--r--xmake/core/main.lua3
-rw-r--r--xmake/core/sandbox/modules/os.lua1
3 files changed, 30 insertions, 2 deletions
diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua
index dcd7cca8b..c05c718c9 100644
--- a/xmake/core/base/os.lua
+++ b/xmake/core/base/os.lua
@@ -270,6 +270,16 @@ function os._is_tracing_process()
return is_tracing
end
+-- run all exit callback
+function os._run_exit_cbs(ok, errors)
+ local exit_callbacks = os._EXIT_CALLBACKS
+ if exit_callbacks then
+ for _, cb in ipairs(exit_callbacks) do
+ cb(ok, errors)
+ end
+ end
+end
+
-- match files or directories
--
-- @param pattern the search pattern
@@ -650,11 +660,25 @@ end
-- exit program
function os.exit(...)
-
- -- do exit
return os._exit(...)
end
+-- register exit callback
+--
+-- e.g.
+-- os.atexit(function (ok, errors)
+-- print(ok, errors)
+-- end)
+--
+function os.atexit(on_exit)
+ local exit_callbacks = os._EXIT_CALLBACKS
+ if exit_callbacks == nil then
+ exit_callbacks = {}
+ os._EXIT_CALLBACKS = exit_callbacks
+ end
+ table.insert(exit_callbacks, on_exit)
+end
+
-- run command
function os.run(cmd)
diff --git a/xmake/core/main.lua b/xmake/core/main.lua
index 18ab0f62e..615df5355 100644
--- a/xmake/core/main.lua
+++ b/xmake/core/main.lua
@@ -207,6 +207,9 @@ end
-- exit main program
function main._exit(ok, errors)
+ -- run all exit callbacks
+ os._run_exit_cbs(ok, errors)
+
-- show errors
local retval = 0
if not ok then
diff --git a/xmake/core/sandbox/modules/os.lua b/xmake/core/sandbox/modules/os.lua
index ac860eac8..b935f47fb 100644
--- a/xmake/core/sandbox/modules/os.lua
+++ b/xmake/core/sandbox/modules/os.lua
@@ -42,6 +42,7 @@ sandbox_os.subarch = os.subarch
sandbox_os.syserror = os.syserror
sandbox_os.strerror = os.strerror
sandbox_os.exit = os.exit
+sandbox_os.atexit = os.atexit
sandbox_os.date = os.date
sandbox_os.time = os.time
sandbox_os.args = os.args