gpt4 book ai didi

lua - 如何理解 "lua_Hook is called when it jumps back in the code(This event only happens while Lua is executing a Lua function.)"?

转载 作者:行者123 更新时间:2023-12-04 03:45:11 25 4
gpt4 key购买 nike

根据文档 ( https://www.lua.org/manual/5.3/manual.html#lua_sethook ),它说 [强调我的]:

Argument f is the hook function. mask specifies on which events thehook will be called: it is formed by a bitwise OR of the constantsLUA_MASKCALL, LUA_MASKRET, LUA_MASKLINE, and LUA_MASKCOUNT. The countargument is only meaningful when the mask includes LUA_MASKCOUNT. Foreach event, the hook is called as explained below:

The call hook: is called when the interpreter calls a function. Thehook is called just after Lua enters the new function, before thefunction gets its arguments.

The return hook: is called when theinterpreter returns from a function. The hook is called just beforeLua leaves the function. There is no standard way to access the valuesto be returned by the function.

The line hook: is called when theinterpreter is about to start the execution of a new line of code, orwhen it jumps back in the code (even to the same line). (This eventonly happens while Lua is executing a Lua function.)

如何理解lua_Hook在代码中跳回时调用(该事件只在Lua执行Lua函数时发生?

最佳答案

我们可以看看源码(Lua 5.4):

int luaG_traceexec (lua_State *L, const Instruction *pc) {
-- some parts removed
if (mask & LUA_MASKLINE) {
if (npci == 0 || /* call linehook when enter a new function, */
pc <= L->oldpc || /* when jump back (loop), or when */
changedline(p, pcRel(L->oldpc, p), npci)) { /* enter new line */
int newline = luaG_getfuncline(p, npci);
luaD_hook(L, LUA_HOOKLINE, newline, 0, 0); /* call line hook */
}
}
return 1; /* keep 'trap' on */
}

可以看出,LUA_HOOKLINE在三种情况下被调用:(1)进入一个新函数,(2)跳回(循环),或(3)进入新行。

通过调用 changedline 检查“Enter new line”:

static int changedline (const Proto *p, int oldpc, int newpc) {
while (oldpc++ < newpc) {
if (p->lineinfo[oldpc] != 0)
return (luaG_getfuncline(p, oldpc - 1) != luaG_getfuncline(p, newpc));
}
return 0; /* no line changes in the way */
}

如果新指令与前一条指令在不同的行上,则返回 true。

luaG_getfuncline 如果有调试信息可用,获取指令的行号。正在检查的函数是基于当前调用信息值的事件lua函数。

关于lua - 如何理解 "lua_Hook is called when it jumps back in the code(This event only happens while Lua is executing a Lua function.)"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65304759/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com