浏览代码

Fixed issue #41

Xanathar 11 年之前
父节点
当前提交
25090962a2
共有 2 个文件被更改,包括 49 次插入6 次删除
  1. 32 0
      src/MoonSharp.Interpreter.Tests/EndToEnd/TableTests.cs
  2. 17 6
      src/MoonSharp.Interpreter/DataTypes/Table.cs

+ 32 - 0
src/MoonSharp.Interpreter.Tests/EndToEnd/TableTests.cs

@@ -444,5 +444,37 @@ namespace MoonSharp.Interpreter.Tests
 		}
 
 
+		[Test]
+		public void TestNilRemovesEntryForPairs()
+		{
+			string script = @"
+				str = ''
+
+				function showTable(t)
+					for i, j in pairs(t) do
+						str = str .. i;
+					end
+					str = str .. '$'
+				end
+
+				tb = {}
+				tb['id'] = 3
+
+				showTable(tb)
+
+				tb['id'] = nil
+
+				showTable(tb)
+
+				return str
+			";
+
+			DynValue res = Script.RunString(script);
+
+			Assert.AreEqual(DataType.String, res.Type);
+			Assert.AreEqual("id$$", res.String);
+		}
+
+
 	}
 }

+ 17 - 6
src/MoonSharp.Interpreter/DataTypes/Table.cs

@@ -359,7 +359,12 @@ namespace MoonSharp.Interpreter
 				if (node == null)
 					return TablePair.Nil;
 				else
-					return node.Value;
+				{
+					if (node.Value.Value.IsNil())
+						return NextKey(node.Value.Key);
+					else
+						return node.Value;
+				}
 			}
 
 			if (v.Type == DataType.String)
@@ -382,13 +387,19 @@ namespace MoonSharp.Interpreter
 
 		private TablePair? GetNextOf(LinkedListNode<TablePair> linkedListNode)
 		{
-			if (linkedListNode == null)
-				return null;
+			while (true)
+			{
+				if (linkedListNode == null)
+					return null;
 
-			if (linkedListNode.Next == null)
-				return TablePair.Nil;
+				if (linkedListNode.Next == null)
+					return TablePair.Nil;
+
+				linkedListNode = linkedListNode.Next;
 
-			return linkedListNode.Next.Value;
+				if (!linkedListNode.Value.Value.IsNil())
+					return linkedListNode.Value;
+			}
 		}