lua-users home
lua-l archive

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


>needs. You'll have to add it by hand:
>	local g={next=next}
Thanks for that.

I am trying provide an original global environment for external
scripts, like:

function funcexec(f)
  local fn = loadfile(f)
  local g = {}
  setfenv(fn,setmetatable(g, {__index=_G,next=next}))
  stat,msg=pcall(fn)
end

Is this all I need do?

+++++++++++++++++++++++++++++++++
>>>There are no references from env tables to functions, only the other way around.
>>Can you explain further please?
>
>Lua does not keep a list of all functions that have a given table as their
>environment table. Of course, it does keep track of the environment of each
>function. The same holds for metatables. Of course, garbage collection makes
>sure that of no references exist for a table, then it can be collected.
>
>>I am struggling a little with the following code
>>
>>local g={}
>>setfenv(0,setmetatable(g, {__index=_G}))
>....
>>for k,v in pairs(g)  do print(k,v) end
>
>>lua5: t3.lua:9: attempt to call a nil value
>
>The problem is that g does not contain a function called "next", which "pairs"
>needs. You'll have to add it by hand:
>	local g={next=next}
>
>The reason why "next" is not found when "pairs" is called is that "pairs" does
>a lua_rawget for "next" and so avoids the _index metamethod. Perhaps "pairs"
>should use lua_gettable instead of lua_rawget.
>--lhf