Skip to content

Commit

Permalink
Fix problem if use break in foreach (#1106)
Browse files Browse the repository at this point in the history
  • Loading branch information
murshex authored Apr 19, 2024
1 parent 611a5a3 commit 19ac20d
Showing 1 changed file with 27 additions and 15 deletions.
42 changes: 27 additions & 15 deletions Assets/XLua/Src/LuaTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,14 +279,20 @@ public IEnumerable GetKeys()
var L = luaEnv.L;
var translator = luaEnv.translator;
int oldTop = LuaAPI.lua_gettop(L);
LuaAPI.lua_getref(L, luaReference);
LuaAPI.lua_pushnil(L);
while (LuaAPI.lua_next(L, -2) != 0)
try
{
yield return translator.GetObject(L, -2);
LuaAPI.lua_pop(L, 1);
LuaAPI.lua_getref(L, luaReference);
LuaAPI.lua_pushnil(L);
while (LuaAPI.lua_next(L, -2) != 0)
{
yield return translator.GetObject(L, -2);
LuaAPI.lua_pop(L, 1);
}
}
finally
{
LuaAPI.lua_settop(L, oldTop);
}
LuaAPI.lua_settop(L, oldTop);
}

#if THREAD_SAFE || HOTFIX_ENABLE
Expand All @@ -297,19 +303,25 @@ public IEnumerable<T> GetKeys<T>()
var L = luaEnv.L;
var translator = luaEnv.translator;
int oldTop = LuaAPI.lua_gettop(L);
LuaAPI.lua_getref(L, luaReference);
LuaAPI.lua_pushnil(L);
while (LuaAPI.lua_next(L, -2) != 0)
try
{
if (translator.Assignable<T>(L, -2))
LuaAPI.lua_getref(L, luaReference);
LuaAPI.lua_pushnil(L);
while (LuaAPI.lua_next(L, -2) != 0)
{
T v;
translator.Get(L, -2, out v);
yield return v;
if (translator.Assignable<T>(L, -2))
{
T v;
translator.Get(L, -2, out v);
yield return v;
}
LuaAPI.lua_pop(L, 1);
}
LuaAPI.lua_pop(L, 1);
}
LuaAPI.lua_settop(L, oldTop);
finally
{
LuaAPI.lua_settop(L, oldTop);
}
}

[Obsolete("use no boxing version: Get<TKey, TValue> !")]
Expand Down

0 comments on commit 19ac20d

Please sign in to comment.