mirror of
https://github.com/ZDoom/raze-gles.git
synced 2025-01-23 15:40:39 +00:00
Lunatic: overridden 'require', currently for base modules only.
git-svn-id: https://svn.eduke32.com/eduke32@2828 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
parent
b86dbc0818
commit
91b7a10bd0
2 changed files with 51 additions and 9 deletions
|
@ -502,6 +502,42 @@ end
|
||||||
|
|
||||||
---=== Set up restricted global environment ===---
|
---=== 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
|
-- _G tweaks -- pull in only 'safe' stuff
|
||||||
local G_ = {} -- our soon-to-be global environment
|
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.
|
-- this table, since user code could later do pairs=nil.
|
||||||
local oG = _G
|
local oG = _G
|
||||||
|
|
||||||
G_.coroutine = coroutine
|
G_.require = our_require
|
||||||
|
---G_.coroutine = coroutine
|
||||||
G_.assert = assert
|
G_.assert = assert
|
||||||
G_.tostring = tostring
|
G_.tostring = tostring
|
||||||
G_.tonumber = tonumber
|
G_.tonumber = tonumber
|
||||||
|
@ -525,7 +562,7 @@ G_.pcall = pcall
|
||||||
--require
|
--require
|
||||||
--rawset
|
--rawset
|
||||||
--jit
|
--jit
|
||||||
G_.bit = bit
|
---G_.bit = bit
|
||||||
--package
|
--package
|
||||||
G_.error = error
|
G_.error = error
|
||||||
--debug
|
--debug
|
||||||
|
@ -534,17 +571,17 @@ G_.error = error
|
||||||
--load
|
--load
|
||||||
G_.unpack = unpack
|
G_.unpack = unpack
|
||||||
G_.pairs = pairs
|
G_.pairs = pairs
|
||||||
G_.table = table
|
---G_.table = table
|
||||||
G_._VERSION = _VERSION
|
G_._VERSION = _VERSION
|
||||||
--newproxy --NOT STD?
|
--newproxy --NOT STD?
|
||||||
--collectgarbage
|
--collectgarbage
|
||||||
--dofile
|
--dofile
|
||||||
G_.next = next
|
G_.next = next
|
||||||
G_.math = math
|
---G_.math = math
|
||||||
--loadstring
|
--loadstring
|
||||||
--_G
|
--_G
|
||||||
G_.select = select
|
G_.select = select
|
||||||
G_.string = string
|
---G_.string = string
|
||||||
G_.type = type
|
G_.type = type
|
||||||
--getmetatable
|
--getmetatable
|
||||||
--getfenv
|
--getfenv
|
||||||
|
|
|
@ -2,6 +2,11 @@
|
||||||
|
|
||||||
--do return end
|
--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 ===---')
|
print('---=== ELua Test script ===---')
|
||||||
|
|
||||||
local function printf(fmt, ...)
|
local function printf(fmt, ...)
|
||||||
|
@ -50,8 +55,6 @@ for i = 0, gv.numsectors/2 do
|
||||||
sector[i].ceilingpal = 2;
|
sector[i].ceilingpal = 2;
|
||||||
end
|
end
|
||||||
|
|
||||||
checkfail('gv.sprite[0].yrepeat = 100') -- direct gv array access forbidden
|
|
||||||
|
|
||||||
local vol, lev
|
local vol, lev
|
||||||
vol, lev = TEMP_getvollev()
|
vol, lev = TEMP_getvollev()
|
||||||
print('volume='..vol..', level='..lev)
|
print('volume='..vol..', level='..lev)
|
||||||
|
@ -92,6 +95,7 @@ end
|
||||||
--tostring = nil -- REMEMBER
|
--tostring = nil -- REMEMBER
|
||||||
--DBG_.printkv('_G in test.elua', _G)
|
--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('local i = sprite["qwe"]') -- indexing struct array with non-numeric type
|
||||||
checkfail('print(sprite[100000].ceilingpal)') -- oob read access
|
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
|
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
|
-- something more restricted later
|
||||||
checkfail("new_global = 345") -- we should declare globals
|
checkfail("new_global = 345") -- we should declare globals
|
||||||
checkfail('gv.CEILING = 3') -- can't redefine constants in 'gv'
|
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
|
-- This is problematic, even though pretty much every access will yield a
|
||||||
-- "missing declaration" error.
|
-- "missing declaration" error.
|
||||||
-- See http://luajit.org/ext_ffi_api.html#ffi_C about what stuff ffi.C contains.
|
-- See http://luajit.org/ext_ffi_api.html#ffi_C about what stuff ffi.C contains.
|
||||||
|
|
Loading…
Reference in a new issue