lua-users home
lua-l archive

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


Hi!

Is it supported to enumerate/query the local variables of a function from within a return hook?

I have some difficulties on all Lua versions since 5.1:

Lua 5.1:

    func	a	10
    func	b	table: 0x18338e0
    func	c	function: 0x182e890
    func	d	true
    func	e	string

(apparently all locals are already gone).

Lua 5.2:

    func	a	10
    func	b	table: 0x1b3ed10
    func	c	function: 0x1b3f4c0
    func	d	true
    func	e	string

    HOOK	a	table: 0x1b3fbc0
    HOOK	b	function: 0x1b3c850
    HOOK	c	3
    HOOK	d	c
    HOOK	e	c

(local variable names are still there, values are wrong)

Lua 5.3:

    func	a	10
    func	b	table: 0x8fbc80
    func	c	function: 0x8fbcc0
    func	d	true
    func	e	string

    HOOK	a	table: 0x8f77b0
    HOOK	b	function: 0x8f7590
    HOOK	c	3
    HOOK	d	c
    HOOK	e	c

(same as 5.2)

LuaJIT seems to manage:

    func	a	10
    func	b	table: 0x40622d08
    func	c	function: 0x4061f780
    func	d	true
    func	e	string

    HOOK	a	10
    HOOK	b	table: 0x40622d08
    HOOK	c	function: 0x4061f780
    HOOK	d	true
    HOOK	e	string

Test script attached.

For now I'll probably try to use the line hook, discarding the results of all calls except the last for each function.

Thx,
Philipp

local function func()
  local a = 10
  local b = {}
  local c = function() end
  local d = true
  local e = "string"
  local ii, name, value = 2, debug.getlocal( 1, 1 )
  while name do
    if #name == 1 then
      print( "func", name, value )
    end
    ii, name, value = ii+1, debug.getlocal( 1, ii )
  end
  print()
end

local function hook()
  if debug.getinfo( 2, "f" ).func == func then
    local i, name, value = 2, debug.getlocal( 2, 1 )
    while name do
      if #name == 1 then
        print( "HOOK", name, value )
      end
      i, name, value = i+1, debug.getlocal( 2, i )
    end
  end
end

debug.sethook( hook, "r" )
func()
debug.sethook()