[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Bizarre behavior with debug.setupvalue() in Lua 5.3
- From: Paul K <paul@...>
- Date: Fri, 27 Mar 2015 21:15:02 +0000
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?
If you make "meta.print" an empty function, you won't get the error
(since it won't have _ENV as its first upvalue). You need to localize
debug.getupvalue if you want to access it after resetting _ENV.
Paul.
On Fri, Mar 27, 2015 at 2:05 PM, Sean Conner <sean@conman.org> wrote:
>
> 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 ... )
>