diff options
Diffstat (limited to 'lualibs-util-sto.lua')
-rw-r--r-- | lualibs-util-sto.lua | 58 |
1 files changed, 26 insertions, 32 deletions
diff --git a/lualibs-util-sto.lua b/lualibs-util-sto.lua index 191d6cd..8aafca4 100644 --- a/lualibs-util-sto.lua +++ b/lualibs-util-sto.lua @@ -103,12 +103,22 @@ end local function f_empty () return "" end -- t,k local function f_self (t,k) t[k] = k return k end local function f_table (t,k) local v = { } t[k] = v return v end +local function f_number(t,k) t[k] = 0 return 0 end -- t,k,v local function f_ignore() end -- t,k,v -local t_empty = { __index = f_empty } -local t_self = { __index = f_self } -local t_table = { __index = f_table } -local t_ignore = { __newindex = f_ignore } +local f_index = { + ["empty"] = f_empty, + ["self"] = f_self, + ["table"] = f_table, + ["number"] = f_number, +} + +local t_index = { + ["empty"] = { __index = f_empty }, + ["self"] = { __index = f_self }, + ["table"] = { __index = f_table }, + ["number"] = { __index = f_number }, +} function table.setmetatableindex(t,f) if type(t) ~= "table" then @@ -116,46 +126,30 @@ function table.setmetatableindex(t,f) end local m = getmetatable(t) if m then - if f == "empty" then - m.__index = f_empty - elseif f == "key" then - m.__index = f_self - elseif f == "table" then - m.__index = f_table - else - m.__index = f - end + m.__index = f_index[f] or f else - if f == "empty" then - setmetatable(t, t_empty) - elseif f == "key" then - setmetatable(t, t_self) - elseif f == "table" then - setmetatable(t, t_table) - else - setmetatable(t,{ __index = f }) - end + setmetatable(t,t_index[f] or { __index = f }) end return t end +local f_index = { + ["ignore"] = f_ignore, +} + +local t_index = { + ["ignore"] = { __newindex = f_ignore }, +} + function table.setmetatablenewindex(t,f) if type(t) ~= "table" then f, t = t, { } end local m = getmetatable(t) if m then - if f == "ignore" then - m.__newindex = f_ignore - else - m.__newindex = f - end + m.__newindex = f_index[f] or f else - if f == "ignore" then - setmetatable(t, t_ignore) - else - setmetatable(t,{ __newindex = f }) - end + setmetatable(t,t_index[f] or { __newindex = f }) end return t end |