-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimp-module-manager.lua
More file actions
64 lines (60 loc) · 2.28 KB
/
imp-module-manager.lua
File metadata and controls
64 lines (60 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
--- Module Manager for IMP (Indaxia Modules & Packages for Lua)
Imp = {
---@class ImpModuleContainer
---@field loaded boolean
---@field context fun(): any
---@field module any
---@type { [string]: ImpModuleContainer }
containers = {},
recursionDepth = 0,
--- Declares IMP module name and it's context to run and get the module
---@generic T
---@param name string Exported module name
---@param context fun(): T Function that is called once and returns the module contents
---@return T definition LuaLS trick to help the inspector
export = function(name, context)
while Imp.containers[name] ~= nil do
name = name .. "*"
end
if (name ~= nil and type(context) == "function") then
Imp.containers[name] = {
loaded = false,
context = context,
module = nil
}
else
Imp.critical("wrong module declaration: '" .. name .. "'. Provide the name and a context function callback that returns a module")
end
return { name = name }
end,
--- Loads IMP module with it's dependencies and returns it
---@generic T
---@param definition T Module definition from Imp.export()
---@return T module Required module
import = function(definition)
if(type(definition) ~= "table") then
Imp.critical("wrong module definition type (" .. type(definition) .. "). Use the variable from Imp.export() result")
elseif(definition.name == nil) then
Imp.critical("wrong module definition name (" .. type(definition.name) .. "). Use the variable from Imp.export() result")
else
if(Imp.recursionDepth > 64) then
Imp.critical("dependency loop detected for the module \"" .. definition.name .. "\"")
end
local container = Imp.containers[definition.name]
if (type(container) ~= "table") then
Imp.critical("module '" .. definition.name .. "' is not declared. Usage: Name = Imp.export(" .. definition.name .. ", ...)")
elseif (not container.loaded) then
Imp.recursionDepth = Imp.recursionDepth + 1
container.module = container.context()
Imp.recursionDepth = Imp.recursionDepth - 1
container.loaded = true
end
return container.module
end
return {}
end,
critical = function(message)
print("IMP Error: " .. message)
os.exit(1)
end
}