diff options
Diffstat (limited to 'apps/plugins/lua/include_lua/temploader.lua')
-rw-r--r-- | apps/plugins/lua/include_lua/temploader.lua | 61 |
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 | --[[ |
2 | temp loader allows some lua requires to be loaded and later garbage collected | 2 | /*************************************************************************** |
3 | unfortunately the module needs to be formatted in such a way to pass back a | 3 | * __________ __ ___. |
4 | call table in order to keep the functions within from being garbage collected | 4 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
5 | too 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 | -- | ||
23 | temp_loader allows some (pure) lua requires to be loaded and later garbage collected | ||
24 | unfortunately the 'required' module needs to be formatted in such a way to | ||
25 | pass back a reference to a function or call table in order to keep the functions | ||
26 | within from being garbage collected too early | ||
6 | 27 | ||
7 | BE AWARE this bypasses the module loader which would allow code reuse | 28 | modules that add things to _G table are unaffected by using this function |
8 | so if you aren't careful this memory saving tool could spell disaster | 29 | except if later you use require or temp_loader those tables will again |
9 | for free RAM if you load the same code multiple times | 30 | be reloaded with fresh data since nothing was recorded about the module being loaded |
10 | --]] | 31 | |
32 | modulename - same as require() | ||
33 | newinstance == true -- get a new copy (from disk) of the module | ||
34 | ... other args for the module | ||
11 | 35 | ||
36 | BE AWARE this bypasses the module loader | ||
37 | which would allow code reuse so if you aren't careful this memory saving tool | ||
38 | could spell disaster for free RAM if you load the same code multiple times | ||
39 | --]] | ||
12 | 40 | ||
13 | local function tempload(modulename) | 41 | local 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 |
28 | end | 61 | end |
29 | 62 | ||
30 | return tempload | 63 | return tempload |