From 547446074ff8a5ec1c12907f13ee7a45baf70880 Mon Sep 17 00:00:00 2001 From: rxi Date: Sat, 1 Mar 2014 15:05:51 +0000 Subject: [PATCH] Added lume.hotswap() fn, updated version to 1.0.6 --- README.md | 9 +++++++++ lume.lua | 29 ++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9e644a5..810d190 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lume.lua b/lume.lua index 79d42f0..2fc91ec 100644 --- a/lume.lua +++ b/lume.lua @@ -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)