Skip to content
Snippets Groups Projects
Commit 6de6bb00 authored by Albert Gräf's avatar Albert Gräf
Browse files

lexpr: Lua 5.2 compatibility

parent be30ba02
No related branches found
No related tags found
No related merge requests found
local lexpr = pd.Class:new():register("lexpr") local lexpr = pd.Class:new():register("lexpr")
-- AG: Lua 5.2 compatibility (cf. https://stackoverflow.com/a/14554565)
if not setfenv then -- Lua 5.2
-- based on http://lua-users.org/lists/lua-l/2010-06/msg00314.html
-- this assumes f is a function
local function findenv(f)
local level = 1
repeat
local name, value = debug.getupvalue(f, level)
if name == '_ENV' then return level, value end
level = level + 1
until name == nil
return nil end
getfenv = function (f) return(select(2, findenv(f)) or _G) end
setfenv = function (f, t)
local level = findenv(f)
if level then debug.setupvalue(f, level, t) end
return f end
end
local function sandbox(e, f) -- only supports nullary f() with one return local function sandbox(e, f) -- only supports nullary f() with one return
local g = getfenv(f) local g = getfenv(f)
setfenv(f, e) setfenv(f, e)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment