[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: [BUG]After the hook function calls lua_yield, using debug.getlocal to get locals does not access to the last line's local variables.
- From: 无名字 <icetoggle@...>
- Date: Wed, 22 Nov 2023 14:04:25 +0800
Example Code:
yieldhook.so:
#include <lua.h>
#include <lauxlib.h>
#include <string.h>
static void hookf (lua_State *L, lua_Debug *ar) {
    if (ar->currentline == 6)
      lua_yield(L, 0);  
}
static int db_sethook (lua_State *L) {
  lua_sethook(L, hookf, LUA_MASKLINE, 0);
  return 0;
}
int luaopen_yieldhook(lua_State *L) {
      luaL_checkversion(L);
      luaL_Reg l[] = {
              { "sethook", db_sethook },
              { NULL, NULL },
      };
      luaL_newlib(L, l);
  return 1;
}
test.lua:
local yieldhook = require 'yieldhook'
local function test()
    local a1 = 1
    local a2 = 2
    local a3 = 3
    local a4 = 4
    return a1 + a2 + a3 + a4
end
local function run()
    yieldhook.sethook(coroutine.running())
    return test()
end
local co = coroutine.create(run)
coroutine.resume(co)
local name
local i = 1
while true do
    local name, value =  debug.getlocal(co, 0, i)
    if name == nil or name == "(temporary)" or name == "(C temporary)" then
        break
    end
    i = i + 1
    print(name, value)
end
result:
a1      1
a2      2
--a3    3   is lost