Skip to content

Commit

Permalink
simplify the __gc check
Browse files Browse the repository at this point in the history
  • Loading branch information
Tieske committed Jun 20, 2024
1 parent 906044c commit b87e6d6
Showing 1 changed file with 3 additions and 13 deletions.
16 changes: 3 additions & 13 deletions system/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,8 @@ do -- autotermrestore


local add_gc_method do
-- feature detection; __GC meta-method, not available in all Lua versions
local has_gc = false
local tt = setmetatable({}, { -- luacheck: ignore
__gc = function() has_gc = true end
})

-- clear table and run GC to trigger
tt = nil
collectgarbage()
collectgarbage()

-- __gc meta-method is not available in all Lua versions
local has_gc = not newproxy or false -- `__gc` was added when `newproxy` was removed

if has_gc then
-- use default GC mechanism since it is available
Expand Down Expand Up @@ -120,8 +111,7 @@ do -- autotermrestore
return nil, "global terminal backup was already set up"
end
global_backup = system.termbackup()
add_gc_method(global_backup, function(self)
system.termrestore(self) end)
add_gc_method(global_backup, function(self) pcall(system.termrestore, self) end)
return true
end

Expand Down

0 comments on commit b87e6d6

Please sign in to comment.