lua-users home
lua-l archive

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

I came across this issue and can't figure out if this is my bug or
Lua's feature. I have this simple script that shows a stack trace.
When I have normal calls in the stack, all function names are properly
reported, but with a tail call in the stack, the name of the function
called is not reported as I'd expect it:

Without tail call:

tail	@D:/Lua/rem/tail.lua	12	13	Lua	upvalue
tail	@D:/Lua/rem/tail.lua	12	15	Lua	upvalue
tail	@D:/Lua/rem/tail.lua	12	15	Lua	local
nil	@D:/Lua/rem/tail.lua	0	18	main	

With tail call:

nil	@D:/Lua/rem/tail.lua	12	13	Lua	
	=(tail call)	-1	-1	tail	
	=(tail call)	-1	-1	tail	
nil	@D:/Lua/rem/tail.lua	0	18	main	

Why did "tail" disappear in the first line? And why is it no longer
reported as upvalue?

Here is the script;

local function stack(start)
  for i = (start or 0), 100 do
    local source = debug.getinfo(i, "Snl")
    if not source then break end
    print(, source.source, source.linedefined,
       source.currentline, source.what, source.namewhat)
    if source.what == 'main' then break end

local function tail(a)
  if a <= 1 then return 1
  else return tail(a-1) end <-- change this to tail(a-1)+0 to make it
a non-tail call