steed/stead/vars.lua

50 lines
893 B
Lua
Raw Normal View History

2010-06-18 12:04:41 +03:00
isForSave = hook(isForSave,
function (f, k, v, s, ...) -- k - key, v - value, s -- parent table
local i,o
2010-06-18 12:04:41 +03:00
if type(s.var) == 'table' then
for i,o in ipairs(s.var) do
if o == k then
2010-06-18 12:04:41 +03:00
return true
end
end
end
return f(k, v, s, unpack(arg))
end)
2010-06-18 12:16:07 +03:00
function __vars_fill(v)
2010-06-18 12:04:41 +03:00
local k,o
if type(v.var) == 'table' then
2010-06-18 12:16:07 +03:00
local k,o
local vars = {}
2010-06-18 12:04:41 +03:00
for k,o in pairs(v.var) do
if tonumber(k) and type(o) == 'string' then
stead.table.insert(vars, o)
else
2010-06-18 12:50:06 +03:00
if v[k] then
error ("Variable overwrites object property: "..tostring(k));
end
2010-06-18 12:04:41 +03:00
v[k] = o
stead.table.insert(vars, k);
end
end
v.var = vars;
end
2010-06-18 12:16:07 +03:00
end
2010-06-18 13:43:44 +03:00
vars_object = obj {
nam = 'vars',
ini = function(s)
2010-06-20 17:35:44 +03:00
local fill = function(k, o)
__vars_fill(o)
end
2010-06-18 13:43:44 +03:00
__vars_fill(_G)
2010-06-20 17:35:44 +03:00
for_each_object(fill);
2010-06-18 13:43:44 +03:00
end
}
2010-06-19 20:43:55 +03:00
obj = hook(obj,
function(f, v, ...)
2010-06-18 12:16:07 +03:00
__vars_fill(v)
2010-06-19 20:43:55 +03:00
return f(v, unpack(arg))
2010-06-18 12:04:41 +03:00
end)