lua-users home
lua-l archive

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


And of course, now all loaded functions got the proxy as their
environment...  Sigh.  Actually testing the code led to the following
version that seems to work... Yay!

function dofile_protected(fname)
    local globals = getfenv(2)  -- globals of caller...
    local proxy = {}
    setmetatable(proxy, {
        __index = function() return nil end,
        __newindex = function(_, index, value)
                if type(value) == "function" then
                    setfenv(value, globals)
                    globals[index] = value
                else
                    -- non-function assignment
                end
            end,
        })

    -- load and execute script file in proxy context
    local f = loadfile(fname)
    setfenv(f, proxy)
    f()
end


Bye,
Wim