github.com/hirochachacha/plua@v0.0.0-20170217012138-c82f520cc725/stdlib/debug/testdata/sethook.lua (about)

     1  function f()
     2  	a = 1
     3  	for i = 0, 0 do
     4  		a = a + i
     5  	end
     6  
     7  	return a + 10
     8  end
     9  
    10  lines = {22, 2, 3, 4, 3, 7, 24}
    11  
    12  function test(f)
    13  	local i = 1
    14  	local function hook(event, line)
    15  		assert(debug.getinfo(1).namewhat == "hook")
    16  		assert(line == lines[i])
    17  		i = i + 1
    18  	end
    19  
    20  	debug.sethook(hook, "l")
    21  
    22  	f()
    23  
    24  	debug.sethook()
    25  end
    26  
    27  test(f)
    28  
    29  t = {
    30  	{name = "sethook", namewhat = "field", linedefined = -1},
    31  	{name = "foo", namewhat = "global", linedefined = 34},
    32  	{what = "main", linedefined = 0},
    33  }
    34  
    35  local i = 1
    36  
    37  function foo() end
    38  
    39  debug.sethook(function()
    40  	d = debug.getinfo(2)
    41  	tt = t[i]
    42  	if tt ~= nil then
    43  		for k, v in pairs(tt) do
    44  			assert(v, d[k])
    45  		end
    46  		i = i + 1
    47  	end
    48  end, "r")
    49  
    50  foo()
    51  
    52  debug.sethook()