chore(*) lua > 5.1 compatibility

* add a setfenv implementation
This commit is contained in:
eskerda 2020-12-11 17:21:33 +01:00
parent a4c0a9ad3d
commit 3d3a8c7549
2 changed files with 21 additions and 2 deletions

View File

@ -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
===== =====

View File

@ -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 = {}