Browse Source

Fix for issues #43, #44 and #45

Xanathar 11 years ago
parent
commit
6a8b894501

+ 40 - 0
src/MoonSharp.Interpreter.Tests/EndToEnd/SimpleTests.cs

@@ -1041,6 +1041,30 @@ namespace MoonSharp.Interpreter.Tests
 			Assert.AreEqual(10, res.Number);
 		}
 
+		[Test]
+		public void VarArgsSum2()
+		{
+			string script = @"
+					function x(m, ...)
+						local t = pack(...);
+						local sum = 0;
+
+						for i = 1, #t do
+							sum = sum + t[i];
+						end
+	
+						return sum * m;
+					end
+
+					return x(5,1,2,3,4);
+								";
+
+			DynValue res = Script.RunString(script);
+
+			Assert.AreEqual(DataType.Number, res.Type);
+			Assert.AreEqual(50, res.Number);
+		}
+
 		[Test]
 		public void VarArgsSumTb()
 		{
@@ -1199,6 +1223,22 @@ namespace MoonSharp.Interpreter.Tests
 			Assert.AreEqual("aABCz", res.String);
 		}
 
+		[Test]
+		public void HomonymArguments()
+		{
+			string script = @"    
+				function test(_,value,_) return _; end
+
+				return test(1, 2, 3);	
+			";
+
+			Script S = new Script(CoreModules.None);
+			DynValue res = S.DoString(script);
+
+			Assert.AreEqual(DataType.Number, res.Type);
+			Assert.AreEqual(3, res.Number);
+		}
+
 
 	}
 }

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

@@ -475,6 +475,19 @@ namespace MoonSharp.Interpreter.Tests
 			Assert.AreEqual("id$$", res.String);
 		}
 
+		[Test]
+		public void TestUnpack()
+		{
+			string script = @"
+				return unpack({3,4})
+			";
 
+			DynValue res = Script.RunString(script);
+
+			Assert.AreEqual(DataType.Tuple, res.Type);
+			Assert.AreEqual(2, res.Tuple.Length);
+			Assert.AreEqual(3, res.Tuple[0].Number);
+			Assert.AreEqual(4, res.Tuple[1].Number);
+		}
 	}
 }

+ 1 - 0
src/MoonSharp.Interpreter.Tests/EndToEnd/UserDataPropertiesTests.cs

@@ -15,6 +15,7 @@ namespace MoonSharp.Interpreter.Tests.EndToEnd
 			public int? NIntProp { get; set; }
 			public object ObjProp { get; set; }
 			public static string StaticProp { get; set; }
+			private string PrivateProp { get { return "Hello"; } }
 
 			public static IEnumerable<int> Numbers
 			{

+ 1 - 1
src/MoonSharp.Interpreter/CoreLib/TableModule.cs

@@ -24,7 +24,7 @@ namespace MoonSharp.Interpreter.CoreLib
 
 			DynValue[] v = new DynValue[ij - ii + 1];
 
-			int tidx = 1;
+			int tidx = 0;
 			for (int i = ii; i <= ij; i++)
 				v[tidx++] = t.Get(i);
 

+ 9 - 1
src/MoonSharp.Interpreter/Interop/UserDataDescriptor.cs

@@ -45,7 +45,7 @@ namespace MoonSharp.Interpreter.Interop
 
 				foreach (PropertyInfo pi in type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static))
 				{
-					if (CheckVisibility(pi.GetCustomAttributes(true), pi.GetGetMethod().IsPublic || pi.GetSetMethod().IsPublic))
+					if (CheckVisibility(pi.GetCustomAttributes(true), IsPropertyInfoPublic(pi)))
 					{
 						var pd = new UserDataPropertyDescriptor(pi, this.AccessMode);
 						m_Properties.Add(pd.Name, pd);
@@ -54,6 +54,14 @@ namespace MoonSharp.Interpreter.Interop
 			}
 		}
 
+		private bool IsPropertyInfoPublic(PropertyInfo pi)
+		{
+			MethodInfo getter = pi.GetGetMethod();
+			MethodInfo setter = pi.GetSetMethod();
+
+			return (getter != null && getter.IsPublic) || (setter != null && setter.IsPublic);
+		}
+
 		private bool CheckVisibility(object[] attributes, bool isPublic)
 		{
 			MoonSharpVisibleAttribute va = attributes.OfType<MoonSharpVisibleAttribute>().SingleOrDefault();

+ 8 - 1
src/MoonSharp.Interpreter/Tree/Expressions/FunctionDefinitionExpression.cs

@@ -107,10 +107,17 @@ namespace MoonSharp.Interpreter.Tree.Expressions
 
 		private SymbolRef[] DefineArguments(List<string> paramnames, ScriptLoadingContext lcontext)
 		{
+			HashSet<string> names = new HashSet<string>();
+
 			SymbolRef[] ret = new SymbolRef[paramnames.Count];
 
-			for (int i = 0; i < paramnames.Count; i++)
+			for (int i = paramnames.Count - 1; i >= 0; i--)
+			{
+				if (!names.Add(paramnames[i]))
+					paramnames[i] = paramnames[i] + "@" + i.ToString();
+				
 				ret[i] = lcontext.Scope.DefineLocal(paramnames[i]);
+			}
 
 			return ret;
 		}