[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Converting setfenv getfenv to Lua 5.2
- From: Georgios Petsagourakis <petsagouris+lual@...>
- Date: Sat, 30 Nov 2013 15:35:19 +0000 (UTC)
Hello all,
As an exercise, I am trying to convert hige to be compatible with 5.2
but as with orbit's htmlify_func() (I'll explain in the second part)
there is trouble involving converting the parts that are using setfenv
and getfenv.
In particular this piece of code:
-- found at
https://github.com/nrk/hige/blob/f8ccf9/hige.lua#L6-L23
local function merge_environment(...)
local numargs, out = select('#', ...), {}
for i = 1, numargs do
local t = select(i, ...)
if type(t) == 'table' then
for k, v in pairs(t) do
if (type(v) == 'function') then
out[k] = setfenv(v, setmetatable(out, {
__index = getmetatable(getfenv()).__index
}))
else
out[k] = v
end
end
end
end
return out
end
As far as orbit is concerned:
I've gotten to a point that the piece of code found here:
-- found at
https://github.com/keplerproject/orbit/blob/1f0c1f/src/orbit.lua#L330-L352
local function htmlify_func(func)
local tags = {}
local env = { H = function (name)
local tag = tags[name]
if not tag then
tag = newtag(name)
tags[name] = tag
end
return tag
end
}
local old_env = getfenv(func)
setmetatable(env, { __index = function (env, name)
if old_env[name] then
return old_env[name]
else
local tag = newtag(name)
rawset(env, name, tag)
return tag
end
end })
setfenv(func, env)
end
I can tell that it creates a new environment for the html tags
function calls by which the calls are actually redirected to
newtag() and then it sets that environment as the environment
for those functions that use these calls.
In both of these cases it is difficult to understand how to make this
functionality happen in Lua 5.2 though with setfenv and getfenv missing.
Would someone be willing to help?