lua-users home
lua-l archive

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


It was thus said that the Great Paul K once stated:
> Hi Sean,
> 
> > print(debug.setupvalue(meta.print,1,meta))
> > name,env = debug.getupvalue(meta.print,1) -- line 16
> > When I run it with Lua 5.3, I get:
> > _ENV
> > lua-53: /tmp/t.lua:16: attempt to index a nil value (global 'debug')
> 
> I get the same error in Lua 5.3 and Lua 5.2. Aren't you setting the
> first upvalue for meta.print (which is _ENV) to "meta", which replaces
> _ENV for your current code with "meta", which makes "debug" not
> available?

  I would expect setting the first upvalue of meta.print() to meta would
make meta the default global environment for meta.print() and not replace
the current global environment.

  This does what I expect in Lua 5.1:

	meta =
	{
	  io       = io,
	  tostring = tostring,
	  select   = select,  
	  print    = function(...)
	    io.stdout:write("HERE I AM, JH ... ")
	    for i = 1 , select('#',...) do
	      io.stdout:write(tostring(select(i,...)),"\t")
	    end
	    io.stdout:write("\n")
	  end
	}
 
	setfenv(meta.print,meta)
	meta.print(1,2,3)

  -spc (And you can test it does by adding a call to a function not in meta
	in meta.print() ... )