sandbox.lua 2.98 KB
Newer Older
1 2 3 4
-- Units
kB = 1024
MB = 1024*1024
GB = 1024*1024
5 6 7 8
-- Time
sec = 1000
minute = 60 * sec
hour = 60 * minute
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

-- Function aliases
-- `env.VAR returns os.getenv(VAR)`
env = {}
setmetatable(env, {
	__index = function (t, k) return os.getenv(k) end
})

-- Quick access to interfaces
-- `net.<iface>` => `net.interfaces()[iface]`
setmetatable(net, {
	__index = function (t, k)
		local v = rawget(t, k)
		if v then return v
		else return net.interfaces()[k]
		end
	end
})

28 29
-- Syntactic sugar for module loading
-- `modules.<name> = <config>`
30
setmetatable(modules, {
31
	__newindex = function (t,k,v)
32 33 34 35 36
		if not rawget(_G, k) then
			modules.load(k)
			local mod = rawget(_G, k)
			if mod and mod['config'] then
				mod['config'](v)
37
			end
38

39
		end
40 41 42 43 44 45 46 47 48 49
	end
})

-- Syntactic sugar for cache
-- `cache.{size|storage} = value`
setmetatable(cache, {
	__newindex = function (t,k,v)
		if     k == 'size'    then t.open(v, rawget(t, 'storage'))
		elseif k == 'storage' then t.open(rawget(t, 'size'), v)
		else   rawset(t, k, v) end
50
	end
51 52
})

53 54 55 56 57 58 59
-- Register module in Lua environment
function modules_register(module)
	-- Syntactic sugar for get() and set() properties
	setmetatable(module, {
		__index = function (t, k)
			local  v = rawget(t, k)
			if     v     then return v
60
			elseif rawget(t, 'get') then return t.get(k)
61 62 63 64
			end
		end,
		__newindex = function (t, k, v)
			local  old_v = rawget(t, k)
65
			if not old_v and rawget(t, 'set') then
66 67 68 69 70 71
				t.set(k..' '..v)
			end
		end
	})
end

72
-- Make sandboxed environment
73
local function make_sandbox(defined)
74
	local __protected = { modules = true, cache = true, net = true }
75 76 77 78 79 80 81 82 83 84 85 86 87
	return setmetatable({}, {
		__index = defined,
		__newindex = function (t, k, v)
			if __protected[k] then
				for k2,v2 in pairs(v) do
					defined[k][k2] = v2
				end
			else
				defined[k] = v
			end
		end
	})
end
88

89
-- Compatibility sandbox
90 91 92 93 94 95
if setfenv then -- Lua 5.1 and less
	_G = make_sandbox(getfenv(0))
	setfenv(0, _G)
else -- Lua 5.2+
	_SANDBOX = make_sandbox(_ENV)
end
96

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
-- Interactive command evaluation
function eval_cmd(line)
	-- Compatibility sandbox code loading
	local function load_code(code)
	    if getfenv then -- Lua 5.1
	        return loadstring(code)
	    else            -- Lua 5.2+
	        return load(code, nil, 't', _ENV)
	    end
	end
	local status, err, chunk
	chunk, err = load_code('table_print('..line..')')
	if err then
		chunk, err = load_code(line)
	end
	if not err then
		chunk()
	end
	if err then
		print(err)
	end
end

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
-- Pretty printing
function table_print (tt, indent, done)
	done = done or {}
	indent = indent or 0
	if type(tt) == "table" then
		for key, value in pairs (tt) do
			io.write(string.rep (" ", indent))
			if type (value) == "table" and not done [value] then
				done [value] = true
				io.write(string.format("[%s] => {\n", tostring (key)));
				table_print (value, indent + 4, done)
				io.write(string.rep (" ", indent))
				io.write("}\n");
			else
				io.write(string.format("[%s] => %s\n",
				         tostring (key), tostring(value)))
			end
		end
	else
		io.write(tostring(tt) .. "\n")
	end
end