Skip to content

Commit 44169df

Browse files
committed
Fix: NextKey (#94)
1 parent 7703559 commit 44169df

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

NeoLua.Test/Runtime.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ public void TestRuntimeLua12()
190190
[TestMethod]
191191
public void TestRuntimeLua14()
192192
{
193-
using (Lua l = new Lua())
193+
using (var l = new Lua())
194194
{
195195
var g = l.CreateEnvironment();
196196
l.PrintExpressionTree = Console.Out;
@@ -480,6 +480,19 @@ public void RegexComplex01()
480480
Assert.AreEqual(43, r[2][5]);
481481
}
482482
}
483+
484+
[TestMethod]
485+
public void LuaNextLoop()
486+
{
487+
TestCode(Lines("local t = { a = 1 , b = 2 , c = 3 };",
488+
"local r = {};",
489+
"for k, v in next, t, nil do",
490+
" table.insert(r, k ..'='..v);",
491+
"end;",
492+
"return table.unpack(r)"),
493+
"a=1", "b=2", "c=3"
494+
);
495+
}
483496
}
484497
} //class Runtime
485498

NeoLua/LuaTable.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3392,6 +3392,13 @@ object NextHashKey(int startIndex)
33923392
return entryIndex == -1 ? null : entries[entryIndex].key;
33933393
} // func NextHashKey
33943394

3395+
object NextHashKey2(int currrentIndex)
3396+
{
3397+
if (currrentIndex < 0 || currrentIndex == entries.Length - 1)
3398+
return null;
3399+
return NextHashKey(currrentIndex + 1);
3400+
} // func NextHashKey2
3401+
33953402
switch (next)
33963403
{
33973404
case null:
@@ -3415,11 +3422,10 @@ object NextHashKey(int startIndex)
34153422
}
34163423
else
34173424
goto default;
3425+
case string memberName:
3426+
return NextHashKey2(FindKey(next, GetMemberHashCode(memberName), compareString));
34183427
default:
3419-
var currentEntryIndex = FindKey(next, next.GetHashCode() & 0x7FFFFFFF, comparerObject);
3420-
if (currentEntryIndex < 0 || currentEntryIndex == entries.Length - 1)
3421-
return null;
3422-
return NextHashKey(currentEntryIndex + 1);
3428+
return NextHashKey2(FindKey(next, next.GetHashCode() & 0x7FFFFFFF, comparerObject));
34233429
}
34243430
} // func NextKey
34253431

0 commit comments

Comments
 (0)