aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDavid Walter Seikel2014-03-26 17:06:54 +1000
committerDavid Walter Seikel2014-03-26 17:06:54 +1000
commit4f51cf774a20baf6018c2bdda8edba126823afb4 (patch)
tree3ff5c02ea4fd8d1ac31a3a8bb1aa96af5f7dc97a
parentTest isBoolean(). (diff)
downloadSledjHamr-4f51cf774a20baf6018c2bdda8edba126823afb4.zip
SledjHamr-4f51cf774a20baf6018c2bdda8edba126823afb4.tar.gz
SledjHamr-4f51cf774a20baf6018c2bdda8edba126823afb4.tar.bz2
SledjHamr-4f51cf774a20baf6018c2bdda8edba126823afb4.tar.xz
White space clean ups.
-rw-r--r--ClientHamr/GuiLua/skang.lua230
-rw-r--r--ClientHamr/GuiLua/test.lua2
2 files changed, 117 insertions, 115 deletions
diff --git a/ClientHamr/GuiLua/skang.lua b/ClientHamr/GuiLua/skang.lua
index cb97a76..6561bb8 100644
--- a/ClientHamr/GuiLua/skang.lua
+++ b/ClientHamr/GuiLua/skang.lua
@@ -64,60 +64,60 @@ Thing = {}
64 64
65-- Trying to capture best practices here for creating modules, especially since module() is broken and deprecated. 65-- Trying to capture best practices here for creating modules, especially since module() is broken and deprecated.
66moduleBegin = function (name, author, copyright, version, timestamp, skin) 66moduleBegin = function (name, author, copyright, version, timestamp, skin)
67 local _M = {} -- This is what we return to require(). 67 local _M = {} -- This is what we return to require().
68 local level = 2 68 local level = 2
69 69
70 package.loaded[name] = _M -- Stuff the result into where require() can find it, instead of returning it at the end. 70 package.loaded[name] = _M -- Stuff the result into where require() can find it, instead of returning it at the end.
71 -- Returning it at the end does the same thing. 71 -- Returning it at the end does the same thing.
72 -- This is so that we can have all the module stuff at the top, in this function. 72 -- This is so that we can have all the module stuff at the top, in this function.
73 -- Should do this before any further require(), so that circular references don't blow out. 73 -- Should do this before any further require(), so that circular references don't blow out.
74 74
75 -- Save the callers environment. 75 -- Save the callers environment.
76 local savedEnvironment = getfenv(level) 76 local savedEnvironment = getfenv(level)
77 77
78 -- Clone the environment into _M, so the module can access everything as usual after the setfenv() below. 78 -- Clone the environment into _M, so the module can access everything as usual after the setfenv() below.
79 --[[ TODO - Check if this also clones _G or _ENV. And see if it leaks stuff in either direction. 79 --[[ TODO - Check if this also clones _G or _ENV. And see if it leaks stuff in either direction.
80 local _G = _G -- Only sets a local _G for this function. 80 local _G = _G -- Only sets a local _G for this function.
81 _M._G = _G -- This clone loop might do this, but we don't want to be able to access the old _G from outside via this leak. 81 _M._G = _G -- This clone loop might do this, but we don't want to be able to access the old _G from outside via this leak.
82 In Lua 5.1 at least, _G was special. In 5.2, _ENV sorta replaces setfenv(), but no idea if this clone loop stomps on that. 82 In Lua 5.1 at least, _G was special. In 5.2, _ENV sorta replaces setfenv(), but no idea if this clone loop stomps on that.
83 ]] 83 ]]
84 for k, v in pairs(savedEnvironment) do 84 for k, v in pairs(savedEnvironment) do
85 _M[k] = v 85 _M[k] = v
86 end 86 end
87 87
88 _M._M = _M -- So that references to _M below the setfenv() actually go to the real _M. 88 _M._M = _M -- So that references to _M below the setfenv() actually go to the real _M.
89 _M._NAME = name 89 _M._NAME = name
90 _M._PACKAGE = string.gsub(_M._NAME, "[^.]*$", "") -- Strip the name down to the package name. 90 _M._PACKAGE = string.gsub(_M._NAME, "[^.]*$", "") -- Strip the name down to the package name.
91 91
92 -- TODO - Should parse in an entire copyright message, and strip that down into bits, to put back together. 92 -- TODO - Should parse in an entire copyright message, and strip that down into bits, to put back together.
93 _M.AUTHOR = author 93 _M.AUTHOR = author
94 _M.COPYRIGHT = copyright .. ' ' .. author 94 _M.COPYRIGHT = copyright .. ' ' .. author
95 -- TODO - Translate the version number into a version string. 95 -- TODO - Translate the version number into a version string.
96 _M.VERSION = version .. ' lookup version here ' .. timestamp 96 _M.VERSION = version .. ' lookup version here ' .. timestamp
97 -- TODO - If there's no skin passed in, try to find the file skin .. '.skang' and load that instead. 97 -- TODO - If there's no skin passed in, try to find the file skin .. '.skang' and load that instead.
98 _M.DEFAULT_SKANG = skin 98 _M.DEFAULT_SKANG = skin
99 99
100 100
101 --_G[_M._NAME] = _M -- Stuff it into a global of the same name. 101 --_G[_M._NAME] = _M -- Stuff it into a global of the same name.
102 -- Not such a good idea to stomp on global name space. 102 -- Not such a good idea to stomp on global name space.
103 -- It's also redundant coz we get stored in package.loaded[_M._NAME] anyway. 103 -- It's also redundant coz we get stored in package.loaded[_M._NAME] anyway.
104 -- This is why module() is broken. 104 -- This is why module() is broken.
105 105
106 setmetatable(_M, Thing) 106 setmetatable(_M, Thing)
107 _M.savedEnvironment = savedEnvironment 107 _M.savedEnvironment = savedEnvironment
108 -- setfenv() sets the environment for the FUNCTION, stack level deep. 108 -- setfenv() sets the environment for the FUNCTION, stack level deep.
109 -- The number is the stack level - 109 -- The number is the stack level -
110 -- 0 running thread, 1 current function, 2 function that called this function, etc 110 -- 0 running thread, 1 current function, 2 function that called this function, etc
111 setfenv(level, _M) -- Use the result for the modules internal global environment, so they don't need to qualify internal names. 111 setfenv(level, _M) -- Use the result for the modules internal global environment, so they don't need to qualify internal names.
112 -- Dunno if this causes problems with the do ... end style of joining modules. It does. So we need to restore in moduleEnd(). 112 -- Dunno if this causes problems with the do ... end style of joining modules. It does. So we need to restore in moduleEnd().
113 -- Next question, does this screw with the environment of the skang module? No it doesn't, coz that's set up at require 'skang' time. 113 -- Next question, does this screw with the environment of the skang module? No it doesn't, coz that's set up at require 'skang' time.
114 114
115 return _M 115 return _M
116end 116end
117 117
118-- Restore the environment. 118-- Restore the environment.
119moduleEnd = function (module) 119moduleEnd = function (module)
120 setfenv(2, module.savedEnvironment) 120 setfenv(2, module.savedEnvironment)
121end 121end
122 122
123-- Call this now so that from now on, this is like any other module. 123-- Call this now so that from now on, this is like any other module.
@@ -130,22 +130,24 @@ local isTrue = 't1aopswy'
130-- false 0 no nack nope zero negative nah 'no way' 'get real' 'uh uh' 'fuck off' 'bugger off' 130-- false 0 no nack nope zero negative nah 'no way' 'get real' 'uh uh' 'fuck off' 'bugger off'
131local isFalse = 'f0bgnuz' 131local isFalse = 'f0bgnuz'
132isBoolean = function (aBoolean) 132isBoolean = function (aBoolean)
133 local result = false 133 local result = false
134 134
135 if type(aBoolean) ~= 'nil' then 135 if type(aBoolean) ~= 'nil' then
136 -- The default case, presence of a value means it's true. 136 -- The default case, presence of a value means it's true.
137 result = true 137 result = true
138 if type(aBoolean) == 'boolean' then result = aBoolean 138 if type(aBoolean) == 'boolean' then result = aBoolean
139 elseif type(aBoolean) == 'function' then result = aBoolean() 139 elseif type(aBoolean) == 'function' then result = aBoolean()
140 elseif type(aBoolean) == 'number' then result = (aBoolean ~= 0) 140 elseif type(aBoolean) == 'number' then result = (aBoolean ~= 0)
141 elseif type(aBoolean) == 'string' then 141 elseif type(aBoolean) == 'string' then
142 if '' == aBoolean then result = false else 142 if '' == aBoolean then
143 if 1 == string.find(string.lower(aBoolean), '^[' .. isTrue .. ']') then result = true end 143 result = false
144 if 1 == string.find(string.lower(aBoolean), '^[' .. isFalse .. ']') then result = false end 144 else
145 end 145 if 1 == string.find(string.lower(aBoolean), '^[' .. isTrue .. ']') then result = true end
146 if 1 == string.find(string.lower(aBoolean), '^[' .. isFalse .. ']') then result = false end
146 end 147 end
147 end 148 end
148 return result 149 end
150 return result
149end 151end
150 152
151 153
@@ -214,7 +216,7 @@ Thing.action = 'nada' -- An optional action to perform.
214Thing.tell = '' -- The skang command that created this Thing. 216Thing.tell = '' -- The skang command that created this Thing.
215 217
216Thing.append = function (self,data) -- Append to the value of this Thing. 218Thing.append = function (self,data) -- Append to the value of this Thing.
217 end 219end
218 220
219Thing.isValid = function (self) -- Check if this Thing is valid, return resulting error messages in errors. 221Thing.isValid = function (self) -- Check if this Thing is valid, return resulting error messages in errors.
220 -- Anything that overrides this method, should call this super method first. 222 -- Anything that overrides this method, should call this super method first.
@@ -230,7 +232,7 @@ Thing.isValid = function (self) -- Check if this Thing is valid, return resultin
230end 232end
231 233
232Thing.remove = function (self) -- Delete this Thing. 234Thing.remove = function (self) -- Delete this Thing.
233 end 235end
234 236
235Thing.errors = {} -- A list of errors returned by isValid(). 237Thing.errors = {} -- A list of errors returned by isValid().
236 238
@@ -242,48 +244,48 @@ Thing.isStubbed = false -- Is this Thing stubbed elsewhere?
242Thing.hasCrashed = 0 -- How many times this Thing has crashed. 244Thing.hasCrashed = 0 -- How many times this Thing has crashed.
243 245
244Thing.__index = function (table, key) 246Thing.__index = function (table, key)
245 -- This only works for keys that don't exist. By definition a value of nil means it doesn't exist. 247 -- This only works for keys that don't exist. By definition a value of nil means it doesn't exist.
246 local thing = things[key] 248 local thing = things[key]
247 -- First see if this is a Thing. 249 -- First see if this is a Thing.
248 if thing then return thing.value or thing.default end 250 if thing then return thing.value or thing.default end
249 251
250 -- Then see if we can inherit it from Thing. 252 -- Then see if we can inherit it from Thing.
251 thing = Thing[key] 253 thing = Thing[key]
252 if thing then return thing end 254 if thing then return thing end
253 255
254 -- If all else fails, return nil. 256 -- If all else fails, return nil.
255 return nil 257 return nil
256 end 258end
257 259
258Thing.__newindex = function (table, key, value) 260Thing.__newindex = function (table, key, value)
259 -- This only works for keys that don't exist. By definition a value of nil means it doesn't exist. 261 -- This only works for keys that don't exist. By definition a value of nil means it doesn't exist.
260 local thing = things[key] 262 local thing = things[key]
261 263
262 if thing then 264 if thing then
263 local name = thing.names[1] 265 local name = thing.names[1]
264 -- This is a proxy table, the values never exist in the real table. 266 -- This is a proxy table, the values never exist in the real table.
265 thing.value = value 267 thing.value = value
266 if 'function' == type(value) then 268 if 'function' == type(value) then
267 thing.func = value 269 thing.func = value
268 local types = '' 270 local types = ''
269 for i, v in ipairs(thing.types) do 271 for i, v in ipairs(thing.types) do
270 if 1 ~= i then types = types .. v .. ', ' end 272 if 1 ~= i then types = types .. v .. ', ' end
271 end 273 end
272 print(thing.module._NAME .. '.' .. name .. '(' .. types .. ') -> ' .. thing.help) 274 print(thing.module._NAME .. '.' .. name .. '(' .. types .. ') -> ' .. thing.help)
273 else 275 else
274 -- NOTE - invalid values are still stored, this is by design. 276 -- NOTE - invalid values are still stored, this is by design.
275 if not thing:isValid() then 277 if not thing:isValid() then
276 for i, v in ipairs(thing.errors) do 278 for i, v in ipairs(thing.errors) do
277 print('ERROR - ' .. v) 279 print('ERROR - ' .. v)
278 end
279 end
280-- print(thing.types[1] .. ' ' .. thing.module._NAME .. '.' .. name .. ' = ' .. (value or 'nil') .. ' -> ' .. thing.help)
281 -- TODO - Go through it's linked things and set them to.
282 end
283 else
284 rawset(table, key, value) -- Stuff it normally.
285 end 280 end
281 end
282-- print(thing.types[1] .. ' ' .. thing.module._NAME .. '.' .. name .. ' = ' .. (value or 'nil') .. ' -> ' .. thing.help)
283 -- TODO - Go through it's linked things and set them to.
286 end 284 end
285 else
286 rawset(table, key, value) -- Stuff it normally.
287 end
288end
287 289
288 -- TODO - Seemed like a good idea at the time, but do we really need it? 290 -- TODO - Seemed like a good idea at the time, but do we really need it?
289--Thing.__call = function (func, ...) 291--Thing.__call = function (func, ...)
@@ -301,38 +303,38 @@ Thing.__newindex = function (table, key, value)
301-- acl - Access Control List defining security restrains. 303-- acl - Access Control List defining security restrains.
302-- boss - the Thing or person that owns this Thing, otherwise it is self owned. 304-- boss - the Thing or person that owns this Thing, otherwise it is self owned.
303thing = function (module, names, help, default, types, widget, required, acl, boss) 305thing = function (module, names, help, default, types, widget, required, acl, boss)
304 -- Break out the names. 306 -- Break out the names.
305 local n = {} 307 local n = {}
306 local i = 1 308 local i = 1
307 for v in string.gmatch(names, '([^,]+)') do 309 for v in string.gmatch(names, '([^,]+)') do
308 n[i] = v 310 n[i] = v
309 i = i + 1 311 i = i + 1
310 end 312 end
311 local name = n[1] 313 local name = n[1]
312 314
313 -- Find type, default to string, then break out the other types. 315 -- Find type, default to string, then break out the other types.
314 local t = {type(default)} 316 local t = {type(default)}
315 if 'nil' == t[1] then t[1] = 'string' end 317 if 'nil' == t[1] then t[1] = 'string' end
316 i = 2 318 i = 2
317 if types then 319 if types then
318 for v in string.gmatch(types, '([^,]+)') do 320 for v in string.gmatch(types, '([^,]+)') do
319 t[i] = v 321 t[i] = v
320 i = i + 1 322 i = i + 1
321 end
322 else
323 types = ''
324 end 323 end
324 else
325 types = ''
326 end
325 327
326 -- Set it all up. 328 -- Set it all up.
327 -- TODO - might want to merge into pre existing Thing instead of over writing like this. 329 -- TODO - might want to merge into pre existing Thing instead of over writing like this.
328 local thing = {module = module, names = n, help = help, default = default, types = t, widget = widget, required = isBoolean(required), acl = acl, boss = boss, } 330 local thing = {module = module, names = n, help = help, default = default, types = t, widget = widget, required = isBoolean(required), acl = acl, boss = boss, }
329 setmetatable(thing, Thing) 331 setmetatable(thing, Thing)
330 -- Stash the Thing under all of it's names. 332 -- Stash the Thing under all of it's names.
331 for i, v in ipairs(thing.names) do 333 for i, v in ipairs(thing.names) do
332 things[v] = thing 334 things[v] = thing
333 end 335 end
334 -- This triggers the Thing.__newindex metamethod above. 336 -- This triggers the Thing.__newindex metamethod above.
335 module[name] = default 337 module[name] = default
336end 338end
337 339
338--[[ TODO - It might be worth it to combine parameters and commands, since in Lua, functions are first class types like numbers and strings. 340--[[ TODO - It might be worth it to combine parameters and commands, since in Lua, functions are first class types like numbers and strings.
diff --git a/ClientHamr/GuiLua/test.lua b/ClientHamr/GuiLua/test.lua
index 619e875..6a38cde 100644
--- a/ClientHamr/GuiLua/test.lua
+++ b/ClientHamr/GuiLua/test.lua
@@ -35,7 +35,7 @@ skang.thing(_M, 'bar', 'Help text', "Default")
35 35
36-- We can use inline functions if we don't need the function internally. 36-- We can use inline functions if we don't need the function internally.
37skang.thing(_M, 'ffunc', 'Help Text', function (arg1, arg2) 37skang.thing(_M, 'ffunc', 'Help Text', function (arg1, arg2)
38 print('Inside test.ffunc ' .. arg1 .. ', ' .. arg2) 38 print('Inside test.ffunc ' .. arg1 .. ', ' .. arg2)
39end, 'number,string') 39end, 'number,string')
40 40
41print('Ending soon') 41print('Ending soon')