-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdualenv.lua
More file actions
32 lines (28 loc) · 830 Bytes
/
dualenv.lua
File metadata and controls
32 lines (28 loc) · 830 Bytes
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
local FORCE_NIL = {} -- special uniq value to remember what is removed : do not restore the original value
local function newdualenv(ro, rw)
rw = rw or {}
local mt = {}
mt.__index = function(_self, k)
local v = rw[k]
if v ~= nil then
if v == FORCE_NIL then -- do not return the ro[k] value
return nil
end
return v
end
return ro[k]
end
mt.__newindex = function(_self, k, v)
if v == nil and ro[k] ~= nil then -- if the value was nil and something exists in the ro table
rw[k] = FORCE_NIL -- remember the nil write with the special value
else
rw[k] = v
end
end
mt.__metatable = false -- basic security, locking the metatable
local t = setmetatable({}, mt)
return t, rw
end
local M = {new = newdualenv}
setmetatable(M, {__call = function(_self, ...) return newdualenv(...) end})
return M