lua-users home
lua-l archive

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


> It should work similar to usual function except
> all variables used inside are implicitly local
> no access to any upvalues or global namespace _G
> So it could interact with arguments it was passed.
> 
> This allows to isolate parts of code from whole program.
> 
> In lua 5.3.4 there is no way to do it.
> 
> [...]

I don't see why one needs this, and how it is related to sandboxing.

A function can only access an upvalue if it is physically written inside
its scope. Any code coming from outside cannot access any upvalue in
your code. It looks like you want to protect your code from itself. As
already said, if you don't want to access an upvalue in your own code,
just don't do it. (For globals, _ENV seems to solve the issue.)

Anyway, if your code is so convoluted that you don't know what is what,
just write that function as the first thing in its chunk, before any
local declaration:

    -- first line in your chunk
    do
      local _ENV = nil
      function aSoCalledPureFunction ()
        ...
      end
    end

-- Roberto