From 91b7a10bd0ed3f78781597a5786d98728d1cdd96 Mon Sep 17 00:00:00 2001 From: helixhorned Date: Fri, 13 Jul 2012 18:20:38 +0000 Subject: [PATCH] Lunatic: overridden 'require', currently for base modules only. git-svn-id: https://svn.eduke32.com/eduke32@2828 1a8010ca-5511-0410-912e-c29ae57300e0 --- polymer/eduke32/source/lunatic/defs.ilua | 47 +++++++++++++++++++++--- polymer/eduke32/source/lunatic/test.elua | 13 +++++-- 2 files changed, 51 insertions(+), 9 deletions(-) diff --git a/polymer/eduke32/source/lunatic/defs.ilua b/polymer/eduke32/source/lunatic/defs.ilua index 819ea64d2..fda38bb07 100644 --- a/polymer/eduke32/source/lunatic/defs.ilua +++ b/polymer/eduke32/source/lunatic/defs.ilua @@ -502,6 +502,42 @@ end ---=== Set up restricted global environment ===--- +-- These are for this file... +local string = string +local table = table + +local allowed_modules = { + coroutine=coroutine, bit=bit, table=table, math=math, string=string, +} + +for modname, themodule in pairs(allowed_modules) do + local mt = { + __index = themodule, + __newindex = function(tab,idx,val) + error("modifying base module table forbidden") + end, + __metatable = true, + } + + allowed_modules[modname] = setmetatable({}, mt) +end + +-- The "require" function accessible to Lunatic code. +-- Base modules in allowed_modules are wrapped so that they cannot be +-- modified, (TODO:) user modules are searched in the EDuke32 search +-- path. Also, our require never messes with the global environment, +-- it only returns the module. +local function our_require(modname) + local themodule = allowed_modules[modname] + + if (themodule==nil) then + error("NOT IMPLEMENTED") + -- need to search user modules here + end + + return themodule +end + -- _G tweaks -- pull in only 'safe' stuff local G_ = {} -- our soon-to-be global environment @@ -510,7 +546,8 @@ local G_ = {} -- our soon-to-be global environment -- this table, since user code could later do pairs=nil. local oG = _G -G_.coroutine = coroutine +G_.require = our_require +---G_.coroutine = coroutine G_.assert = assert G_.tostring = tostring G_.tonumber = tonumber @@ -525,7 +562,7 @@ G_.pcall = pcall --require --rawset --jit -G_.bit = bit +---G_.bit = bit --package G_.error = error --debug @@ -534,17 +571,17 @@ G_.error = error --load G_.unpack = unpack G_.pairs = pairs -G_.table = table +---G_.table = table G_._VERSION = _VERSION --newproxy --NOT STD? --collectgarbage --dofile G_.next = next -G_.math = math +---G_.math = math --loadstring --_G G_.select = select -G_.string = string +---G_.string = string G_.type = type --getmetatable --getfenv diff --git a/polymer/eduke32/source/lunatic/test.elua b/polymer/eduke32/source/lunatic/test.elua index dc4194c33..e66a1480b 100644 --- a/polymer/eduke32/source/lunatic/test.elua +++ b/polymer/eduke32/source/lunatic/test.elua @@ -2,6 +2,11 @@ --do return end +-- error=nil -- must not affect "require" +local string = require("string") +local bit = require("bit") +local math = require("math") + print('---=== ELua Test script ===---') local function printf(fmt, ...) @@ -50,8 +55,6 @@ for i = 0, gv.numsectors/2 do sector[i].ceilingpal = 2; end -checkfail('gv.sprite[0].yrepeat = 100') -- direct gv array access forbidden - local vol, lev vol, lev = TEMP_getvollev() print('volume='..vol..', level='..lev) @@ -92,6 +95,7 @@ end --tostring = nil -- REMEMBER --DBG_.printkv('_G in test.elua', _G) +checkfail('gv.sprite[0].yrepeat = 100') -- direct gv array access forbidden checkfail('local i = sprite["qwe"]') -- indexing struct array with non-numeric type checkfail('print(sprite[100000].ceilingpal)') -- oob read access checkfail('print(gv.sprite[0])') -- NOTE: gv.sprite doesn't fail, but we can't use it @@ -108,8 +112,9 @@ checkfail("require('os')") -- 'require' has been thrown away to be replaced by -- something more restricted later checkfail("new_global = 345") -- we should declare globals checkfail('gv.CEILING = 3') -- can't redefine constants in 'gv' -checkfail('string.dump(gameevent)') -- string.dump is unavailable - +checkfail('local s=require[[string]]; local tmp=s.dump(gameevent)') -- string.dump is unavailable +checkfail('local s=require[[string]]; s.format=nil') -- disallow changing base module tables +print('') -- This is problematic, even though pretty much every access will yield a -- "missing declaration" error. -- See http://luajit.org/ext_ffi_api.html#ffi_C about what stuff ffi.C contains.