lua-users home
lua-l archive

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


The following script fails:

	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
	}
 
	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')
stack traceback:
        /tmp/t.lua:16: in main chunk
        [C]: in ?

  My intent is to have meta.print() use meta as its environment.  I can work
around it with:

	PRINT = [[
		io.stdout:write("HERE I AM, JH ... ")
		for i = 1 , select('#',...) do
		  io.stdout:write(tostring(select(i,...)),"\t")
		end
		io.stdout:write("\n")
	]]
  
	meta =
	{
	  io       = io,
	  tostring = tostring,
	  select   = select,  
	}
 
	meta.print = load(PRINT,"code","t",meta)
	name,env = debug.getupvalue(meta.print,1)
	print(name,env,meta)
	meta.print("three","two",1,true)

and when run:

_ENV    table: 0x8159068        table: 0x8159068
HERE I AM, JH ... three two     1       true

  I'm curious---what am I doing wrong in the first case?

  -spc (And for the curious---this is a testcase for some code I'm playing
	around with ... )