1
0
Fork 0
forked from len0rd/rockbox

lua fix yellow and add temploader

temp loader allows some lua requires to be loaded and
later garbage collected unfortunately the module needs to be formatted
in such a way to pass back a call table in order to keep the functions
within from being garbage collected too early

BE AWARE this bypasses the module loader which would allow code reuse
so if you aren't careful this memory saving tool could spell disaster
for free RAM if you load the same code multiple times

Change-Id: I0b6f81e481b8c779edbd620c8403794f8353926f
This commit is contained in:
William Wilgus 2021-05-03 23:06:40 -04:00
parent 489a5f3ff7
commit 9b2f23319c
4 changed files with 104 additions and 60 deletions

View file

@ -0,0 +1,30 @@
--[[
temp loader allows some lua requires to be loaded and later garbage collected
unfortunately the module needs to be formatted in such a way to pass back a
call table in order to keep the functions within from being garbage collected
too early
BE AWARE this bypasses the module loader which would allow code reuse
so if you aren't careful this memory saving tool could spell disaster
for free RAM if you load the same code multiple times
--]]
local function tempload(modulename)
--http://lua-users.org/wiki/LuaModulesLoader
local errmsg = ""
-- Find source
local modulepath = string.gsub(modulename, "%.", "/")
for path in string.gmatch(package.path, "([^;]+)") do
local filename = string.gsub(path, "%?", modulepath)
local file = io.open(filename, "r")
if file then
-- Compile and return the module
return assert(loadstring(assert(file:read("*a")), filename))()
end
errmsg = errmsg.."\n\tno file '"..filename.."' (temp loader)"
end
return errmsg
end
return tempload