The final type of hook is a return hook. Return hooks get executed every time a function returns, that is, when the return keyword is encountered. To subscribe to a return event, provide debug.sethook with two arguments: the handler function, and the "r" string.
The handler function takes only one argument, a string constant with the "return" value. This callback is similar to the function callback:
function Normalize(x, y, z)
local dot = x * x + y * y + z * z
if dot == 0 then
return nil
end
local len = math.sqrt(dot)
return {
x = x / len,
y = y / len,
z = z / len
}
end
function trace(event)
local info = debug.getinfo(2)
if info.what == "Lua" then
print ("event: " .. event)
print (" function: " .. info.name)
print (" defined on: " .. info.linedefined)
end
end
debug.sethook(trace, "r")
local norm = Normalize(9, 2, 6)