Page 1 of 1

Problems with debug.getinfo()

Posted: Thu Aug 25, 2022 11:31 am
by Pi-C
Suppose I have a table of functions indexed by name:

Code: Select all

get_name = function() 
	print("Entered function "..debug.getinfo(2).name) 
end	

a = {
  test_a = function(x) get_name() print(x) end,
  test_b = function(x) get_name() print(2*x) end,	
}
It seems that debug.getinfo will have different results depending on how it is called:

Code: Select all

-- As expected:
> a.test_a(123)
Entered function test_a
123

-- As expected:
> a["test_a"](123)
Entered function test_a
123

-- Unexpected:
> f = "test_a"
> a[f](123)
Entered function ?
123
This prevents meaningful output when looping over all functions:

Code: Select all

> for f_name, f in pairs(a) do a[f_name](123) end
Entered function ?
123
Entered function ?
246
Same thing with getting the function name by concatenating strings:

Code: Select all

> x = "a"
> a["test_"..x](123)
Entered function ?
123
Is there any way to get at the correct function name in those cases as well?