A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita
audio
rust
zig
deno
mpris
rockbox
mpd
1--[[
2/***************************************************************************
3 * __________ __ ___.
4 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
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
27
28modules that add things to _G table are unaffected by using this function
29except if later you use require or temp_loader those tables will again
30be reloaded with fresh data since nothing was recorded about the module being loaded
31
32modulename - same as require()
33newinstance == true -- get a new copy (from disk) of the module
34... other args for the module
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--]]
40
41local function tempload(modulename, newinstance, ...)
42 --http://lua-users.org/wiki/LuaModulesLoader
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
48 -- Find source
49 local modulepath = string.gsub(modulename, "%.", "/")
50 for path in string.gmatch(package.path, "([^;]+)") do
51 local filename = string.gsub(path, "%?", modulepath)
52 --attempt to open and compile module
53 local file, err = loadfile(filename)
54 if file then
55 -- execute the compiled chunk
56 return file(... or modulename)
57 end
58 errmsg = table.concat({errmsg, "\n\tno file '", filename, "' (temp loader)"})
59 end
60 return nil, errmsg
61end
62
63return tempload