Added lume.hotswap() fn, updated version to 1.0.6

This commit is contained in:
rxi 2014-03-01 15:05:51 +00:00
parent 0d85bd630f
commit 547446074f
2 changed files with 37 additions and 1 deletions

View File

@ -203,6 +203,15 @@ Executes the lua code inside `str`.
lume.dostring("print('Hello!')") -- Prints "Hello!"
```
### lume.hotswap(modname)
Reloads an already loaded module in place; `modname` should be the same string
used when loading the module with require(). This function can be used to
immediatly see the effects of code changes without having to restart the
program.
```lua
lume.hotswap("lume") -- Reloads the lume module
```
### lume.rgba(color)
Takes the 32bit integer `color` argument and returns 4 numbers, one for each
channel, with a range of 0 - 255. Handy for using as the argument to

View File

@ -7,7 +7,7 @@
-- under the terms of the MIT license. See LICENSE for details.
--
local lume = { _version = "1.0.5" }
local lume = { _version = "1.0.6" }
function lume.clamp(x, min, max)
@ -226,6 +226,33 @@ function lume.dostring(str)
end
function lume.hotswap(modname)
local updated = {}
local function update(old, new)
if updated[old] then return end
updated[old] = true
local oldmt, newmt = getmetatable(old), getmetatable(new)
if oldmt and newmt then update(oldmt, newmt) end
for k, v in pairs(new) do
if type(v) == "table" then update(old[k], v) else old[k] = v end
end
end
local oldglobal = lume.clone(_G)
local oldmod = require(modname)
package.loaded[modname] = nil
local newmod = require(modname)
package.loaded[modname] = oldmod
if type(oldmod) == "table" then update(oldmod, newmod) end
for k, v in pairs(oldglobal) do
if v ~= _G[k] and type(v) == "table" then
update(v, _G[k])
_G[k] = v
end
end
return oldmod
end
function lume.rgba(color)
local floor = math.floor
local a = floor((color / 2 ^ 24) % 256)