lua-users home
lua-l archive

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


On 14 May 2010 12:42, steve donovan <steve.j.donovan@gmail.com> wrote:
2010/5/14 spir ☣ <denis.spir@gmail.com>:
> Yes, but there's always the same issue that now globals (eg builtin funcs) are inaccessible, since an "env" acts both as globals & locals. Thus, "d=math.square(x*x+y*y)" fails.

This is merely a technical problem ;) After creating the table, attach
a metatable with __index = _G (or whatever the enclosing scope is) -
i.e. just like package.seeall.  But unlike that case, we can set the
metatable to nil before returning it, because we only need the
'constructor' to operate inside this table scope.

This will fail if the new table contains a closure which makes use of the global env. I think you need to make the environment a proxy table which writes to the new table, and reads from the table followed by the global scope (by using a function  for __index).

    henk