lua-users home
lua-l archive

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


Lucas Ackerman escribió:
> Ok, so that is already doable, but it's not what I'm asking for.  In 4.0,
> you can get away with just hiding globals, and in your case you
> don't need to do this from within Lua.  But if someone does want to
> do this within Lua (I can think of any number of reasons -
> demarshalling object references, more flexible sandbox setup, etc),
> with a more recent build that uses lexical scoping, then they need to
> be able to hide the enclosing scope, which was the real point of this
> suggestion.

You can do this from inside Lua, in exactly the same way.

function unsafe()... end

function run_in_sandbox(fn, sandbox)
  local setg, g = setglobals, setglobals(sandbox)
  local rv = fn()
  setg(g)
  return rv
end

What's the problem? unsafe cannot get at any of the local variables in
run_in_sandbox
because the scoping is lexical, not dynamic.

It is more likely that unsafe will have been defined in some external
string or file; in those cases, it has no access to any lexically scoped
variables. The only case which I can think of in which you might have a
problem is if you were compiling a wrapper around a piece of user defined
code. However, you can usually get around that using the technique above;
simply, define the user-code outside of the "dangerous" locals.

Rici