Lunatic: prohibit initial assigns creating new globals, add some must-fail tests

git-svn-id: https://svn.eduke32.com/eduke32@2294 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
helixhorned 2012-01-28 14:37:24 +00:00
parent a32872276d
commit 289476e7bd
2 changed files with 32 additions and 4 deletions

View file

@ -165,6 +165,8 @@ DBG_.loadstring = oG.loadstring
---- Set up restricted access to ffi.C from lunatic. ----
local ffiC = ffi.C
-- TODO: error(...) --> error(..., 2) to blame the caller?
local det = {} -- dummy empty table
local tmpmt = {
__index = function() error('dummy variable: read access forbidden') end,
@ -191,7 +193,7 @@ gv = {
}
local tmpmt = {
__index = ffiC,
__newindex = function() error("cannot create new fields in 'gv'") end,
__newindex = function() error("cannot create new or write into existing fields of 'gv'") end,
__metatable = true,
}
oG.setmetatable(gv, tmpmt)
@ -204,7 +206,7 @@ local tmpmt = {
error('out-of-bounds sector[] read access')
end,
__newindex = function(tab, key, val) error('cannot write to sector[] struct directly') end,
__newindex = function(tab, key, val) error('cannot write directly to sector[] struct') end,
__metatable = true,
}
oG.setmetatable(sector, tmpmt)
@ -216,7 +218,7 @@ local tmpmt = {
error('out-of-bounds wall[] read access')
end,
__newindex = function(tab, key, val) error('cannot write to wall[] struct directly') end,
__newindex = function(tab, key, val) error('cannot write directly to wall[] struct') end,
__metatable = true,
}
oG.setmetatable(wall, tmpmt)
@ -232,7 +234,7 @@ local function creategtab(ctab, maxidx, name)
error('out-of-bounds '..name..' read access')
end,
__newindex = function(tab, key, val)
error('cannot write to '..name)
error('cannot write directly to '..name)
end,
__metatable = true,
}
@ -304,5 +306,21 @@ G_.actor = actor
G_.spritesofsect = spritesofsect
G_.spritesofstat = spritesofstat
-- 'simple' code for prohibiting initial assignments to create new variables,
-- from 14.2 of PiL
function gamevar(name, initval) -- aka 'declare'
rawset(G_, name, initval or false)
end
setmetatable(
G_, {
__newindex = function (_, n)
error("attempt to write to undeclared variable "..n, 2)
end,
__index = function (_, n)
error("attempt to read undeclared variable "..n, 2)
end,
})
-- change the environment of the running Lua thread to the table G_
setfenv(0, G_)

View file

@ -14,6 +14,8 @@ end
local i
print('tweaking sector pals')
print('numsectors: ' .. gv.numsectors)
---[[
for i = 0, gv.numsectors/2 do
sector[i].floorpal = 1;
@ -35,6 +37,9 @@ for spr in spritesofsect(236) do
print('#spr', spr)
end
--this is a problem
--actor = {}
actor[562].flags = bit.bor(actor[562].flags, 2); -- pal 6 with goggles on front SEENINE
--]]
@ -48,8 +53,13 @@ checkfail('setmetatable(sprite, {})') -- set metatable forbidden
checkfail('sector[-1].ceilingpal = 4') -- oob write access
checkfail('sector[0].wallnum = 0') -- wallnum member is read-only
checkfail('gv.numsectors = 4') -- gv.numsectors is read-only
checkfail('gv.QWE = 4') -- cannot create new fields in 'gv'
checkfail('sector[4] = sector[6]') -- direct sector write access forbidden
checkfail('nextspritesect[4] = -666') -- that would be horrible...
checkfail('print(nextspritesect[4].whatfield)') -- we're indexing a plain array!
checkfail('wall[4].QWE = 123') -- creating new keys forbidden... handled by LuaJit
checkfail("require('os')") -- 'require' has been thrown away to be replaced by
-- something more restricted later
checkfail("new_global = 345") -- we should declare globals
print('--- end test script ---')