lua-users home
lua-l archive

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


Hi All,

I came across an interesting effect with debug hook: it seems like it
in some cases misses lines with "local" definitions that don't have
assignments. The effect can be demonstrated with Lua 5.1, Lua 5.2 and
LuaJIT 2.0.

Take this script:

debug.sethook(function(event, line) print(event, line) end, "l") --<-- line 1
local a
local b
local c
local d
c = 1
print("done") --<-- line 7

Lua 5.1 and Lua 5.2:

line	2
line	6
line	7
done

LuaJIT 2.0:

line	3
line	6
line	7
done

Now make a small tweak to the script:

debug.sethook(function(event, line) print(event, line) end, "l") --<-- line 1
local a
local b = 1 --<-- added assignment
local c
local d
c = 1
print("done") --<-- line 7

These are the results:

Lua 5.1 and Lua 5.2:

line	2
line	3
line	4
line	6
line	7
done

LuaJIT 2.0:

line	2
line	3
line	5
line	6
line	7
done

In the first case this is what is being produced (Lua 5.2; only line 2
is present, the rest "local" lines are missing):

	6	[2]	LOADNIL  	0 3

In the second case this is what is being produced (lines 2,3,4 are
present, line 5 is still missing):

	6	[2]	LOADNIL  	0 0
	7	[3]	LOADK    	1 -4	; 1
	8	[4]	LOADNIL  	2 1

So, it seems like both Lua and LuaJIT don't produce LOADNIL or similar
command in some cases and as a result, don't trigger debug hooks on
those lines. I first thought that the commands are getting optimized
away, but this doesn't seem to be the case as adding "b = 1" brings
"local a" and "local c" back to life.

This is more than a mere curiosity as the debugger steps over those
lines, which confuses users. It also seems to behave inconsistently
depending on whether *other* lines have that assignment or not. Is
this a feature or a bug? Thank you.

Paul.