lua-users home
lua-l archive

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


On Sun, Aug 23, 2020 at 5:26 PM Viacheslav Usov <via.usov@gmail.com> wrote:

> This is so because the "c" hook is invoked before the function is
> called, yet the context (stack frame) of the function is already
> established.

This is indeed so (in Lua 5.3 at least), contrarily to the
documentation that states: "The call hook: is called when the
interpreter calls a function. The hook is called just after Lua enters
the new function, before the function gets its arguments."

Example:

local debug = require 'debug'

debug.sethook(
    function()
        for i = 1, 9 do print(i, debug.getlocal(2, i)) end
    end,
    'c'
)

local function f(a, b, c) end

f('foo', 'bar', 'baz')

Output:

1       a       foo
2       b       bar
3       c       baz
4       (*temporary)    table: 0000016BA4367798
5       nil
6       nil
7       nil
8       nil
9       nil

So the arguments are definitely on stack and available for inspection.
It is actually good, because a call hook without access to arguments
would be much less useful. The meaning of "just after Lua enters the
new function" is subject to interpretation.

I suggest another description instead: "The call hook: is called when
the interpreter calls a function. The hook is called as if the new
function had just begun execution, with its stack frame (specifically,
arguments) observable from the hook."

Cheers,
V.