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

View File

@@ -4,5 +4,7 @@
#
inspect.lua
main.lua
globaldb.lua
model.lua

View File

@@ -0,0 +1,22 @@
local globaldb=class('globaldb')
function globaldb.get(db, key)
local result = db[key]
if type(result) == 'table' then
return result
else
result = {}
db[key] = result
end
return result
end
function globaldb.accessor(db)
return function(key)
return globaldb.get(db, key)
end
end
function globaldb.create()
return {}
end

View File

@@ -28,12 +28,12 @@
-- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--
local inspect = class('inspect')
local inspect = {}
local tostring = tostring
inspect.KEY = setmetatable({}, {__tostring = function() return 'inspect.KEY' end})
inspect.METATABLE = setmetatable({}, {__tostring = function() return 'inspect.METATABLE' end})
inspect.KEY = {}
inspect.METATABLE = {}
local function rawpairs(t)
return next, t, nil
@@ -236,8 +236,10 @@ function Inspector:putKey(k)
end
function Inspector:putTable(t)
if t == inspect.KEY or t == inspect.METATABLE then
self:puts(tostring(t))
if t == inspect.KEY then
self:puts("inspect.KEY")
elseif t == inspect.METATABLE then
self:puts("inspect.METATABLE")
elseif self:alreadyVisited(t) then
self:puts('<table ', self:getId(t), '>')
elseif self.level >= self.depth then
@@ -331,5 +333,12 @@ function inspect.inspect(root, options)
return table.concat(inspector.buffer)
end
_G.inspect = inspect.inspect
function inspect.iprint(...)
local n = select("#", ...)
for i = 1,n do
local v = select(i, ...)
print(inspect.inspect(v))
end
end
_G.iprint = inspect.iprint

View File

@@ -1,48 +0,0 @@
-- given a global_db, makes an accessor that fetches named
-- tables from that global_db.
function make_global_accessor(db)
return function(name)
local global = rawget(db, name)
if type(global) ~= "table" then
global = {}
rawset(db, name, global)
end
return global
end
end
-- The sandbox list - a list of the builtin functions that gets imported
-- into world models. This is a very weak form of sandboxing, a world
-- model can still easily use functions that are not on this list. If you
-- want stronger sandboxing, you need to not link in certain builtins at all.
--
-- Omitted from this list: "debug", "dofile", "getfenv", "io", "jit",
-- "load", "loadfile", "loadstring", "module", "newproxy", "os", "package",
-- "pcall", "require", "xpcall"
--
local sandbox_list = { "assert", "bit", "error", "getmetatable", "inspect",
"ipairs", "math", "next", "pairs", "print", "rawequal", "rawget", "rawset",
"select", "setmetatable", "tonumber", "tostring", "type", "unpack" }
-- Make a world model.
--
function make_world_model()
genv = {}
genv._G = genv
meta = {}
meta.class_db = {}
meta.global_db = {}
meta.source_db = {}
meta.tangible_db = {}
meta.__newindex = function() error("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(sandbox_list) do
genv[name] = _G[name]
end
setmetatable(genv, meta)
return genv
end

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