瀏覽代碼

Fixed bad bug in table access

Xanathar 11 年之前
父節點
當前提交
3282b4a885

+ 6 - 4
src/MoonSharp.Interpreter/DataStructs/LinkedListIndex.cs

@@ -48,20 +48,21 @@ namespace MoonSharp.Interpreter.DataStructs
 		/// </summary>
 		/// </summary>
 		/// <param name="key">The key.</param>
 		/// <param name="key">The key.</param>
 		/// <param name="value">The value.</param>
 		/// <param name="value">The value.</param>
-		/// <returns>True if the value has been created</returns>
-		public bool Set(TKey key, TValue value)
+		/// <returns>The previous value of the element</returns>
+		public TValue Set(TKey key, TValue value)
 		{
 		{
 			LinkedListNode<TValue> node = Find(key);
 			LinkedListNode<TValue> node = Find(key);
 
 
 			if (node == null)
 			if (node == null)
 			{
 			{
 				Add(key, value);
 				Add(key, value);
-				return true;
+				return default(TValue);
 			}
 			}
 			else
 			else
 			{
 			{
+				TValue val = node.Value;
 				node.Value = value;
 				node.Value = value;
-				return false;
+				return val;
 			}
 			}
 		}
 		}
 
 
@@ -95,6 +96,7 @@ namespace MoonSharp.Interpreter.DataStructs
 			}
 			}
 		}
 		}
 
 
+
 		/// <summary>
 		/// <summary>
 		/// Determines whether the index contains the specified key.
 		/// Determines whether the index contains the specified key.
 		/// </summary>
 		/// </summary>

+ 18 - 12
src/MoonSharp.Interpreter/DataTypes/Table.cs

@@ -200,8 +200,23 @@ namespace MoonSharp.Interpreter
 			CheckValueOwner(key);
 			CheckValueOwner(key);
 			CheckValueOwner(value);
 			CheckValueOwner(value);
 
 
-			if (m_ValueMap.Set(key, new TablePair(key, value)))
+			PerformTableSet(m_ValueMap, key, key, value, false);
+		}
+
+		private void PerformTableSet<T>(LinkedListIndex<T, TablePair> listIndex, T key, DynValue keyDynValue, DynValue value, bool isNumber)
+		{
+			TablePair prev = listIndex.Set(key, new TablePair(keyDynValue, value));
+
+			if (prev.Value == null || prev.Value.IsNil())
+			{
 				CollectDeadKeys();
 				CollectDeadKeys();
+
+				if (isNumber)
+					m_CachedLength = -1;
+			}
+
+			if (isNumber && value.IsNil())
+				m_CachedLength = -1;
 		}
 		}
 
 
 		/// <summary>
 		/// <summary>
@@ -243,9 +258,7 @@ namespace MoonSharp.Interpreter
 		public void Set(string key, DynValue value)
 		public void Set(string key, DynValue value)
 		{
 		{
 			CheckValueOwner(value);
 			CheckValueOwner(value);
-
-			if (m_StringMap.Set(key, new TablePair(DynValue.NewString(key), value)))
-				CollectDeadKeys();
+			PerformTableSet(m_StringMap, key, DynValue.NewString(key), value, false);
 		}
 		}
 
 
 		/// <summary>
 		/// <summary>
@@ -282,14 +295,7 @@ namespace MoonSharp.Interpreter
 		public void Set(int key, DynValue value)
 		public void Set(int key, DynValue value)
 		{
 		{
 			CheckValueOwner(value);
 			CheckValueOwner(value);
-
-			if (m_ArrayMap.Set(key, new TablePair(DynValue.NewNumber(key), value)))
-			{
-				CollectDeadKeys();
-				m_CachedLength = -1;
-			}
-			else if (value.IsNil())
-				m_CachedLength = -1;
+			PerformTableSet(m_ArrayMap, key, DynValue.NewNumber(key), value, true);
 		}
 		}
 
 
 		/// <summary>
 		/// <summary>

+ 6 - 6
src/PerformanceComparison/Program.cs

@@ -30,7 +30,7 @@ namespace PerformanceComparison
 			function move(n, src, dst, via)
 			function move(n, src, dst, via)
 				if n > 0 then
 				if n > 0 then
 					move(n - 1, src, via, dst)
 					move(n - 1, src, via, dst)
-					print(src, 'to', dst)
+					check(src, 'to', dst)
 					move(n - 1, via, dst, src)
 					move(n - 1, via, dst, src)
 				end
 				end
 			end
 			end
@@ -91,7 +91,7 @@ end
 		static StringBuilder g_MoonSharpStr = new StringBuilder();
 		static StringBuilder g_MoonSharpStr = new StringBuilder();
 		static StringBuilder g_NLuaStr = new StringBuilder();
 		static StringBuilder g_NLuaStr = new StringBuilder();
 
 
-		public static DynValue Print(ScriptExecutionContext executionContext, CallbackArguments values)
+		public static DynValue Check(ScriptExecutionContext executionContext, CallbackArguments values)
 		{
 		{
 			foreach (var val in values.GetArray())
 			foreach (var val in values.GetArray())
 			{
 			{
@@ -103,7 +103,7 @@ end
 		}
 		}
 
 
 
 
-		public static void NPrint(params object[] values)
+		public static void NCheck(params object[] values)
 		{
 		{
 			foreach (var val in values)
 			foreach (var val in values)
 			{
 			{
@@ -112,7 +112,7 @@ end
 			g_NLuaStr.AppendLine();
 			g_NLuaStr.AppendLine();
 		}
 		}
 
 
-		public static void PrintX(int from, string mid, int to)
+		public static void XCheck(int from, string mid, int to)
 		{
 		{
 			g_MoonSharpStr.Append(from);
 			g_MoonSharpStr.Append(from);
 			g_MoonSharpStr.Append(mid);
 			g_MoonSharpStr.Append(mid);
@@ -123,7 +123,7 @@ end
 		static Lua lua = new Lua();
 		static Lua lua = new Lua();
 		static string testString = "world";
 		static string testString = "world";
 
 
-		static void xxMain(string[] args)
+		static void Main(string[] args)
 		{
 		{
 			Stopwatch sw;
 			Stopwatch sw;
 
 
@@ -139,7 +139,7 @@ end
 			sw = Stopwatch.StartNew();
 			sw = Stopwatch.StartNew();
 
 
 			var script = new Script();
 			var script = new Script();
-			script.Globals.Set("print", DynValue.NewCallback(new CallbackFunction(Print)));
+			script.Globals.Set("check", DynValue.NewCallback(new CallbackFunction(Check)));
 			CallbackFunction.DefaultAccessMode = InteropAccessMode.Preoptimized;
 			CallbackFunction.DefaultAccessMode = InteropAccessMode.Preoptimized;
 
 
 			//script.Globals["print"] = (Action<int, string, int>)PrintX;
 			//script.Globals["print"] = (Action<int, string, int>)PrintX;

+ 1 - 1
src/PerformanceComparison/Sample.cs

@@ -20,7 +20,7 @@ namespace PerformanceComparison
 		//     3
 		//     3
 		//     hello world
 		//     hello world
 		//     Done
 		//     Done
-		public static void Main()
+		public static void xxMain()
 		{
 		{
 			string code = @"
 			string code = @"
 				x = 3
 				x = 3