lua-users home
lua-l archive

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


On Tue, Nov 29, 2011 at 3:47 AM, Miles Bader <miles@gnu.org> wrote:
> David Manura <dm.lua@math2.org> writes:
>> -- load.lua
>> local env; env = setmetatable({
>>   import = function(name, value) env[name] = value end
>> }, {__index = _G})
>> assert(loadfile('foo.lua', nil, env))()
>
> Hmmm, that would work, but would require one to keep tight control on
> file loading [...]

It can even be preferable.  Consider for instance trying to pass
`import` as a callback function like `pcall(import, ...)`.  Your
original code probably would fail there because it would attempt to
write into the C environment of `pcall`.  There was once a Lua bug
similar to that [1].

[1] http://www.lua.org/bugs.html#5.1.3-11   `pcall(module, "xuxu")`