[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Using Lua for config files
- From: Petite Abeille <petite.abeille@...>
- Date: Thu, 19 Dec 2013 20:26:39 +0100
On Dec 19, 2013, at 1:59 PM, John Hind <john.hind@zen.co.uk> wrote:
> Rather than controlling what users can do, I would prefer to control the effect they can have. Why should they not have the benefit of a full featured language to configure your program?
Through their eggnog induced torpor, the elves have this to say:
Let it be, let it be, let it be, let it be
There will be an answer, let it be
What about some sort of Hippocratic Lua script: primum non nocere?
For example:
(1) Hook it
local naughty = {}
local aHook = function( anEvent, aLine )
naughty[ aLine ] = ( naughty[ aLine ] or 0 ) + 1
assert( naughty[ aLine ] <= 10, 'naughty!' )
end
(2) Set it
debug.setmetatable( '', nil )
debug.setmetatable( 1, nil )
debug.setmetatable( function() end, nil )
debug.setmetatable( true, nil )
debug.sethook ( aHook, 'l' )
local anEnvironment = {}
anEnvironment._G = anEnvironment
(3) Load it
local aConfig = assert( load( io.open( 'TestConfig.txt', 'r' ):read( 1000 ), 'config', 't', anEnvironment ) )
aConfig()
debug.sethook()
Marvel in puzzlement:
for aKey, aValue in pairs( anEnvironment ) do
print( aKey, aValue )
end
> 1 1
> 2 2
> 3 3
> 4 4
> 5 5
> foo bar
> cool 1
> list table: 0x7feffb408fe0
> _G table: 0x7feffb407f00
—8<—
--something = ("x"):rep(100000000)
list = {}
for anIndex = 1, 5 do
_G[ anIndex ] = anIndex
end
if true then
foo = 'bar'
else
foo = 'baz'
end
local function nice()
return 1
end
cool = nice()
local function naughty()
naughty()
end
--naughty()
--while true do end
—>8—
What could ever possibly go wrong ask the elves?