summaryrefslogtreecommitdiff
path: root/apps/plugins/lua/include_lua/temploader.lua
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/lua/include_lua/temploader.lua')
-rw-r--r--apps/plugins/lua/include_lua/temploader.lua61
1 files changed, 47 insertions, 14 deletions
diff --git a/apps/plugins/lua/include_lua/temploader.lua b/apps/plugins/lua/include_lua/temploader.lua
index 69eae468a8..7fd58aee99 100644
--- a/apps/plugins/lua/include_lua/temploader.lua
+++ b/apps/plugins/lua/include_lua/temploader.lua
@@ -1,30 +1,63 @@
1--[[ 1--[[
2temp loader allows some lua requires to be loaded and later garbage collected 2/***************************************************************************
3unfortunately the module needs to be formatted in such a way to pass back a 3 * __________ __ ___.
4call table in order to keep the functions within from being garbage collected 4 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
5too early 5 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
6 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
7 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
8 * \/ \/ \/ \/ \/
9 * $Id$
10 *
11 * Copyright (C) 2021 William Wilgus
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ****************************************************************************/
22--
23temp_loader allows some (pure) lua requires to be loaded and later garbage collected
24unfortunately the 'required' module needs to be formatted in such a way to
25pass back a reference to a function or call table in order to keep the functions
26within from being garbage collected too early
6 27
7BE AWARE this bypasses the module loader which would allow code reuse 28modules that add things to _G table are unaffected by using this function
8so if you aren't careful this memory saving tool could spell disaster 29except if later you use require or temp_loader those tables will again
9for free RAM if you load the same code multiple times 30be reloaded with fresh data since nothing was recorded about the module being loaded
10--]] 31
32modulename - same as require()
33newinstance == true -- get a new copy (from disk) of the module
34... other args for the module
11 35
36BE AWARE this bypasses the module loader
37which would allow code reuse so if you aren't careful this memory saving tool
38could spell disaster for free RAM if you load the same code multiple times
39--]]
12 40
13local function tempload(modulename) 41local function tempload(modulename, newinstance, ...)
14 --http://lua-users.org/wiki/LuaModulesLoader 42 --http://lua-users.org/wiki/LuaModulesLoader
15 local errmsg = "" 43 local errmsg = ""
44 -- Is there current a loaded module by this name?
45 if package.loaded[modulename] ~= nil and not newinstance then
46 return require(modulename)
47 end
16 -- Find source 48 -- Find source
17 local modulepath = string.gsub(modulename, "%.", "/") 49 local modulepath = string.gsub(modulename, "%.", "/")
18 for path in string.gmatch(package.path, "([^;]+)") do 50 for path in string.gmatch(package.path, "([^;]+)") do
19 local filename = string.gsub(path, "%?", modulepath) 51 local filename = string.gsub(path, "%?", modulepath)
20 local file = io.open(filename, "r") 52 --attempt to open and compile module
53 local file, err = loadfile(filename)
21 if file then 54 if file then
22 -- Compile and return the module 55 -- execute the compiled chunk
23 return assert(loadstring(assert(file:read("*a")), filename))() 56 return file(... or modulename)
24 end 57 end
25 errmsg = errmsg.."\n\tno file '"..filename.."' (temp loader)" 58 errmsg = table.concat({errmsg, "\n\tno file '", filename, "' (temp loader)"})
26 end 59 end
27 return errmsg 60 return nil, errmsg
28end 61end
29 62
30return tempload 63return tempload