This commit is contained in:
2020-11-15 16:49:42 -05:00
parent a784f12aed
commit f690fb147b
12 changed files with 431 additions and 168 deletions

41
luprex/syslua/model.lua Normal file
View File

@@ -0,0 +1,41 @@
local model=class('model')
local globaldb=class('globaldb')
-- The global environment contents for a model. A list of the builtin
-- functions and classes that get installed in the global environment of
-- a world model.
--
-- This barely counts as 'sandboxing' - it's more just a mechanism to
-- keep the user from unintentionally thinking that some functionality is
-- available when it's not. For true sandboxing, you need to not import
-- functions into the lua interpreter at all.
--
-- Omitted from this list: debug, dofile, getfenv, io, jit,
-- load, loadfile, loadstring, module, newproxy, os, package,
-- pcall, require, xpcall
--
model.global_contents = { "assert", "bit", "error", "getmetatable", "inspect",
"ipairs", "math", "next", "pairs", "print", "rawequal", "rawget", "rawset",
"select", "setmetatable", "tonumber", "tostring", "type", "unpack" }
-- make a world model.
--
function model.make()
genv = {}
genv._G = genv
meta = {}
meta.class_db = classdb.create()
meta.global_db = globaldb.create()
meta.source_db = {}
meta.tangible_db = {}
meta.__newindex = function() error("world model global environment is read-only") end
-- meta.__metatable = false
genv.class = classdb.accessor(meta.class_db)
genv.global = make_global_accessor(meta.global_db)
for i,name in ipairs(world.global_contents) do
genv[name] = _G[name]
end
setmetatable(genv, meta)
return genv
end