lua-users home
lua-l archive

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


On 2020/12/15 20:45, 孙世龙 sunshilong wrote:
> As per the documentation(https://www.lua.org/manual/5.3/manual.html#lua_sethook),
> which says [empasise mine]:
> 
> Argument f is the hook function. mask specifies on which events the
> hook will be called: it is formed by a bitwise OR of the constants
> LUA_MASKCALL, LUA_MASKRET, LUA_MASKLINE, and LUA_MASKCOUNT. The count
> argument is only meaningful when the mask includes LUA_MASKCOUNT. For
> each event, the hook is called as explained below:
> 
> 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.
> 
> The return hook: is called when the interpreter returns from a
> function. The hook is called just before Lua leaves the function.
> There is no standard way to access the values to be returned by the
> function.
> 
> The line hook: is called when the interpreter is about to start the execution
> of a new line of code, or when it jumps back in the code (even to the
> same line).
>                                                    ^^^^^^^^^^^^^^^^^^^^^^
>  (This event only happens while Lua is executing a Lua function.)
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 
> How to comprehend that lua_Hook is called when it jumps back in the
> code(This event only happens while Lua is executing a Lua function?
> 

the line hook is called when

 a) the interpreter is about to start the execution of a new line of code, or
 b) when it jumps back in the code (even to the same line)

point a) says that line hook is called BEFORE (instead of AFTER) a line, such
as the situation of sequential execution without control flows. e.g. if you
set a break point at line 42, it will trigger after code in line 41 finished,
but before any code in line 42 event started.

point b) clarifies the hook will be called even if the execution is not sequential
but from a (backward or single line) control flow construct (i.e. loop or goto).
e.g. a single line loop like ```for i = 1, #t do print(t[i]) ``` will trigger the
break point or hook each time the loop is executed instead of just once. 

the last part just says the hook doesn't work for C functions, only for Lua code.