diff --git a/vmake-2.0.0-18.rockspec b/vmake-2.0.1-19.rockspec similarity index 85% rename from vmake-2.0.0-18.rockspec rename to vmake-2.0.1-19.rockspec index fa664be..a63a31b 100644 --- a/vmake-2.0.0-18.rockspec +++ b/vmake-2.0.1-19.rockspec @@ -1,9 +1,9 @@ package = "vmake" -version = "2.0.0-18" +version = "2.0.1-19" source = { url = "git://github.com/vercas/vMake", - tag = "v2.0.0-5.1", + tag = "v2.0.1", } description = { @@ -13,7 +13,7 @@ description = { } dependencies = { - "lua >= 5.1", + "lua >= 5.2", } build = { diff --git a/vmake.lua b/vmake.lua index 92a16f1..757772e 100644 --- a/vmake.lua +++ b/vmake.lua @@ -847,7 +847,66 @@ do end if setfenv then - error("TODO") + local cache = {} + + function withEnvironment(f, env) + if cache[f] and cache[f][env] then + return cache[f][env] + end + + local bytecode = string.dump(f) + + -- TODO: Get function info. + + local new, err = loadstring(bytecode) + + if not new then + error("vMake internal error: Failed to copy function " .. "" .. " to change its environment: " .. err, 1) + end + + setfenv(new, env) + + local i, nameN = 1 + + repeat + nameN = debug.getupvalue(new, i) + + if not nameN then + break + end + + local j, nameO, temp = 1 + + repeat + nameO, temp = debug.getupvalue(f, j) + + if nameO == nameN then + debug.setupvalue(new, i, temp) + + break + end + + j = j + 1 + until not nameO + + if not nameO then + error("vMake internal error: Failed to set upvalue \"" .. nameN .. "\" of function " .. "", 1) + end + + i = i + 1 + until not nameN + + if cache[f] then + cache[f][env] = new + else + cache[f] = { [env] = new } + end + + cache[new] = { [env] = new } + -- Yup, cache this one as well. + + return new + end else local cache = {}