lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

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
    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
              out[k] = v
      return out

As far as orbit is concerned:

I've gotten to a point that the piece of code found here:

    -- found at
    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
                     return tag
      local old_env = getfenv(func)
      setmetatable(env, { __index = function (env, name)
                                     if old_env[name] then
                                     return old_env[name]
                                     local tag = newtag(name)
                                     rawset(env, name, tag)
                                     return tag
                                    end })
      setfenv(func, env)

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?