mirror of
https://github.com/kikito/lua-sandbox.git
synced 2024-12-18 03:04:20 +00:00
chore(*) lua > 5.1 compatibility
* add a setfenv implementation
This commit is contained in:
parent
a4c0a9ad3d
commit
3d3a8c7549
@ -9,8 +9,6 @@ It's possible to provide extra functions via the `options.env` parameter.
|
|||||||
|
|
||||||
Infinite loops are prevented via the `debug` library.
|
Infinite loops are prevented via the `debug` library.
|
||||||
|
|
||||||
For now, sandbox.lua only works with Lua 5.1.x.
|
|
||||||
|
|
||||||
Usage
|
Usage
|
||||||
=====
|
=====
|
||||||
|
|
||||||
|
21
sandbox.lua
21
sandbox.lua
@ -28,6 +28,27 @@ local sandbox = {
|
|||||||
]]
|
]]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
-- Lua 5.2, 5.3 and above
|
||||||
|
-- https://leafo.net/guides/setfenv-in-lua52-and-above.html#setfenv-implementation
|
||||||
|
local setfenv = setfenv or function(fn, env)
|
||||||
|
local i = 1
|
||||||
|
while true do
|
||||||
|
local name = debug.getupvalue(fn, i)
|
||||||
|
if name == "_ENV" then
|
||||||
|
debug.upvaluejoin(fn, i, (function()
|
||||||
|
return env
|
||||||
|
end), 1)
|
||||||
|
break
|
||||||
|
elseif not name then
|
||||||
|
break
|
||||||
|
end
|
||||||
|
|
||||||
|
i = i + 1
|
||||||
|
end
|
||||||
|
|
||||||
|
return fn
|
||||||
|
end
|
||||||
|
|
||||||
-- The base environment is merged with the given env option (or an empty table, if no env provided)
|
-- The base environment is merged with the given env option (or an empty table, if no env provided)
|
||||||
--
|
--
|
||||||
local BASE_ENV = {}
|
local BASE_ENV = {}
|
||||||
|
Loading…
Reference in New Issue
Block a user