lua-users home
lua-l archive

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


Sorry for the garbled chronology of my replies.  My mail server is playing
tricks on me again I'm afraid...  :-(

> What I am trying to disallow are definitions outside of function
> definitions (e.g. the 'x = 5').

That should be possible.  The idea is that you create a proxy to the globals
table and disallow (ignore) all global assignments that do not assign a
function and let all global lookups return nil.  In Lua this could be done
with the following:

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
                    globals[index] = value
                else
                    -- non-function assignment
                end
            end.,
        })

    -- load and execute script file in proxy context
    local env, dofile, setfenv = getfenv(1), dofile, setfenv
    setfenv(1, proxy)
    dofile(fname)
    setfenv(1, env)
end

Bye,
Wim