lua-users home
lua-l archive

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


I think the short summary is:

* In an interactive environment where scripts essentially arrive
piece-by-piece, the defaulting to global behavior is a benefit.

* This is less clear in the case of scripts.

* Defaulting to local is problematic since it wouldn't be clear at what
scope a variable should exist. What one would probably need is for
local/global declarations to be required.

* There is potentially an issue with respect to what things should be
allowed for reading without declaration. For example, do you want to have to
declare access to "table" in order to use that namespace? I actually don't
have a problem with that, but I might want "_G" and "require" to be
automatically visible.

One approach to dealing with this would be to add a parameter to the
compiler that would take an optional function to determine whether or not
access to a particular global would be allowed. For example:

    local myExports = {
        [ "_G" ] =  true,
        [ "require" ] = true
    }

    function allowAccess( name, accessType )
        -- Returns nothing if access is allowed. Returns an error string
        -- if access is disallowed.

         if not myExports[ name ] then
            return string.format( "global %q is undefined", name )
        end

       if accessType == "write" then
            return string.format(
                "write access to global %q is not allowed", name )
        end

    end

This function would be used at every access to a global when compiling
though it could probably be optimized to only be called on the first access
of a particular kind for each name since the compiler stops at the first
error.

Mark