Skip to content

Commit

Permalink
unify cleanup behavior between script clear and load (#1438)
Browse files Browse the repository at this point in the history
  • Loading branch information
ngwese authored Sep 20, 2021
1 parent cdf5882 commit c1f435b
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions lua/core/script.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@ local Script = {}
Script.clear = function()
print("# script clear")

if cleanup ~= nil then
local ok, err
ok, err = pcall(cleanup)
if not ok then
print("### cleanup failed with error: "..err)
end
end

-- allow mods to do cleanup
hook.script_post_cleanup()

-- unload asl package entry so `require 'asl'` works
-- todo(pq): why is this not needed generally (e.g., for 'ui', 'util', etc.)?
if package.loaded['asl'] ~= nil then
package.loaded['asl'] = nil
end

-- script local state
local state = { }

Expand Down Expand Up @@ -133,31 +150,16 @@ Script.load = function(filename)
end
end

print("# script load: " .. filename)

local f=io.open(filename,"r")
if f==nil then
print("file not found: "..filename)
print("# script load failed, file not found: " .. filename)
else
io.close(f)
local ok, err
ok, err = pcall(cleanup)
if ok then print("# cleanup")
else
print("### cleanup failed with error: "..err)
end

-- allow mods to do cleanup
hook.script_post_cleanup()

-- unload asl package entry so `require 'asl'` works
-- todo(pq): why is this not needed generally (e.g., for 'ui', 'util', etc.)?
if package.loaded['asl'] ~= nil then
package.loaded['asl'] = nil
end

Script.clear() -- clear script variables and functions

print("# script load: " .. filename)

norns.state.script = filename
norns.state.name = name
norns.state.shortname = norns.state.name:match( "([^/]+)$" )
Expand Down

0 comments on commit c1f435b

Please sign in to comment.