diff options
Diffstat (limited to 'apps/plugins/lua/include_lua/temploader.lua')
-rw-r--r-- | apps/plugins/lua/include_lua/temploader.lua | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/apps/plugins/lua/include_lua/temploader.lua b/apps/plugins/lua/include_lua/temploader.lua new file mode 100644 index 0000000000..69eae468a8 --- /dev/null +++ b/apps/plugins/lua/include_lua/temploader.lua | |||
@@ -0,0 +1,30 @@ | |||
1 | --[[ | ||
2 | temp loader allows some lua requires to be loaded and later garbage collected | ||
3 | unfortunately the module needs to be formatted in such a way to pass back a | ||
4 | call table in order to keep the functions within from being garbage collected | ||
5 | too early | ||
6 | |||
7 | BE AWARE this bypasses the module loader which would allow code reuse | ||
8 | so if you aren't careful this memory saving tool could spell disaster | ||
9 | for free RAM if you load the same code multiple times | ||
10 | --]] | ||
11 | |||
12 | |||
13 | local function tempload(modulename) | ||
14 | --http://lua-users.org/wiki/LuaModulesLoader | ||
15 | local errmsg = "" | ||
16 | -- Find source | ||
17 | local modulepath = string.gsub(modulename, "%.", "/") | ||
18 | for path in string.gmatch(package.path, "([^;]+)") do | ||
19 | local filename = string.gsub(path, "%?", modulepath) | ||
20 | local file = io.open(filename, "r") | ||
21 | if file then | ||
22 | -- Compile and return the module | ||
23 | return assert(loadstring(assert(file:read("*a")), filename))() | ||
24 | end | ||
25 | errmsg = errmsg.."\n\tno file '"..filename.."' (temp loader)" | ||
26 | end | ||
27 | return errmsg | ||
28 | end | ||
29 | |||
30 | return tempload | ||