- Joined
- Apr 24, 2012
- Messages
- 5,113
I'm just moving my small Github repo here.
This is a tiny module system for Wc3's Lua. Since Wc3 lacks the concept of modules (which is similar to web apps), it is better if there's a way to segregate code and concern through a module system.
Usage:
You can also use module.load over the provided require.
Relevant: Modules for Wc3 Lua
This is a tiny module system for Wc3's Lua. Since Wc3 lacks the concept of modules (which is similar to web apps), it is better if there's a way to segregate code and concern through a module system.
Lua:
local moduleLoaded = {}
local cachedModule = {}
local moduleBody = {}
local origin = ""
local function loadModule(name)
if((not moduleLoaded[name]) and origin ~= name) then
local parent = origin
origin = name
cachedModule[name] = moduleBody[name](loadModule)
moduleLoaded[name] = true
origin = parent
end
return cachedModule[name]
end
local function moduleCreate(name, body)
moduleBody[name] = body
moduleLoaded[name] = false
cachedModule[name] = nil
end
module = {
create = moduleCreate,
load = loadModule
}
Usage:
Lua:
module.create("A", function() return 100 end)
module.create("B", function() return 200 end)
module.create("add", function ()
return function (a, b)
return a + b
end
end)
module.create("print", function ()
return print
end)
module.create("addAB", function (require)
local A = require("A")
local B = require("B")
local add = require("add")
return add(A, B)
end)
module.create("init", function (require)
require("print")(require("addAB"))
end)
module.load("init")
You can also use module.load over the provided require.
Relevant: Modules for Wc3 Lua