Browse Source

Minor changes to interop

Xanathar 11 năm trước cách đây
mục cha
commit
5503581671

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

@@ -101,6 +101,27 @@ namespace MoonSharp.Interpreter.Tests
 			Assert.AreEqual("[==[[=[[[eheh]]=]=]", res.Tuple[2].String);
 		}
 
+		[Test]
+		public void FunctionCallWrappers()
+		{
+			string script = @"    
+				function boh(x) 
+					return 1912 + x;
+				end
+			";
+
+			Script s = new Script();
+			s.DoString(script);
+
+			DynValue res = s.Globals.Get("boh").Function.Call(82);
+
+			Assert.AreEqual(DataType.Number, res.Type);
+			Assert.AreEqual(1994, res.Number);
+		}
+
+
+
+
 		[Test]
 		public void OperatorSimple()
 		{

+ 2 - 1
src/MoonSharp.Interpreter.Tests/EndToEnd/UserDataMethodsTests.cs

@@ -41,9 +41,10 @@ namespace MoonSharp.Interpreter.Tests.EndToEnd
 				return p7;
 			}
 
-			public StringBuilder ConcatI(int p1, string p2, IComparable p3, bool p4, List<object> p5, IEnumerable<object> p6,
+			public StringBuilder ConcatI(Script s, int p1, string p2, IComparable p3, bool p4, List<object> p5, IEnumerable<object> p6,
 				StringBuilder p7, Dictionary<object, object> p8, SomeClass p9, int p10 = 1912)
 			{
+				Assert.IsNotNull(s);
 				return ConcatS(p1, p2, p3, p4, p5, p6, p7, p8, this, p10);
 			}
 

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

@@ -422,5 +422,15 @@ namespace MoonSharp.Interpreter.Tests.EndToEnd
 			Assert.AreEqual(obj, S.Globals["myobj"]);
 			Assert.AreEqual(19, obj.IntProp);
 		}
+
+		[Test]
+		public void Interop_Boh()
+		{
+			Script s = new Script();
+			long big = long.MaxValue;
+			var v = DynValue.FromObject(s, big);
+			int a = 3;
+		}
+
 	}
 }

+ 24 - 0
src/MoonSharp.Interpreter/DataTypes/Closure.cs

@@ -52,5 +52,29 @@ namespace MoonSharp.Interpreter
 				ClosureContext = emptyClosure;
 		}
 
+
+		/// <summary>
+		/// Calls this function with the specified args
+		/// </summary>
+		/// <param name="args">The arguments to pass to the function.</param>
+		/// <returns></returns>
+		/// <exception cref="System.ArgumentException">Thrown if function is not of DataType.Function</exception>
+		public DynValue Call(params object[] args)
+		{
+			return OwnerScript.Call(this, args);
+		}
+
+		/// <summary>
+		/// Calls this function with the specified args
+		/// </summary>
+		/// <param name="args">The arguments to pass to the function.</param>
+		/// <returns></returns>
+		/// <exception cref="System.ArgumentException">Thrown if function is not of DataType.Function</exception>
+		public DynValue Call(params DynValue[] args)
+		{
+			return OwnerScript.Call(this, args);
+		}
+
+
 	}
 }

+ 8 - 0
src/MoonSharp.Interpreter/DataTypes/DynValue.cs

@@ -738,6 +738,14 @@ namespace MoonSharp.Interpreter
 			return MoonSharp.Interpreter.Interop.ConversionHelper.MoonSharpValueToClrObject(this);
 		}
 
+		/// <summary>
+		/// Converts this Moon# DynValue to a CLR object of the specified type.
+		/// </summary>
+		public T ToObject<T>()
+		{
+			return (T)MoonSharp.Interpreter.Interop.ConversionHelper.MoonSharpValueToObjectOfType(this, typeof(T), null);
+		}
+
 	}
 
 

+ 3 - 0
src/MoonSharp.Interpreter/Execution/VM/Processor/Processor_Scope.cs

@@ -134,6 +134,9 @@ namespace MoonSharp.Interpreter.Execution.VM
 				return SymbolRef.Global(name, env);
 			}
 
+			// reminder: good code never reaches here, except when debugger peeks others' affairs
+			// +++
+
 			return null;
 		}
 

+ 32 - 24
src/MoonSharp.Interpreter/Interop/ConversionHelper.cs

@@ -153,10 +153,6 @@ namespace MoonSharp.Interpreter.Interop
 			return t;
 		}
 
-
-
-
-
 		internal static object MoonSharpValueToClrObject(DynValue value)
 		{
 			switch (value.Type)
@@ -176,7 +172,12 @@ namespace MoonSharp.Interpreter.Interop
 				case DataType.Tuple:
 					return value.Tuple;
 				case DataType.UserData:
-					return value.UserData.Object;
+					if (value.UserData.Object != null)
+						return value.UserData.Object;
+					else if (value.UserData.Descriptor != null)
+						return value.UserData.Descriptor.Type;
+					else
+						return null;
 				case DataType.ClrFunction:
 					return value.Callback;
 				default:
@@ -184,36 +185,43 @@ namespace MoonSharp.Interpreter.Interop
 			}
 		}
 
-		internal static object MoonSharpValueToObjectOfType(DynValue value, Type t, object defaultValue)
+		internal static object MoonSharpValueToObjectOfType(DynValue value, Type desiredType, object defaultValue)
 		{
-			if (t == typeof(DynValue))
+			if (desiredType == typeof(DynValue))
 				return value;
 
-			if (t == typeof(object))
+			if (desiredType == typeof(object))
 				return value.ToObject();
 
 			bool isString = false;
 			bool isStringBuilder = false;
 			bool isChar = false;
 
-			if (t == typeof(string))
+			if (desiredType == typeof(string))
 				isString = true;
-			else if (t == typeof(StringBuilder))
+			else if (desiredType == typeof(StringBuilder))
 				isStringBuilder = true;
-			else if (t == typeof(char) && value.String.Length > 0)
+			else if (desiredType == typeof(char) && value.String.Length > 0)
 				isChar = true;
 
 			bool isAnyString = isString || isStringBuilder || isChar;
 			string str = null;
 
+			Type nt = Nullable.GetUnderlyingType(desiredType);
+			Type nullableType = null;
+
+			if (nt != null)
+			{
+				nullableType = desiredType;
+				desiredType = nt;
+			}
+
 			switch (value.Type)
 			{
 				case DataType.Nil:
-					if (t.IsValueType)
+					if (desiredType.IsValueType)
 					{
-						Type nt = Nullable.GetUnderlyingType(t);
-						
-						if (nt != null)
+						if (nullableType != null)
 							return null;
 
 						if (defaultValue != null)
@@ -225,14 +233,14 @@ namespace MoonSharp.Interpreter.Interop
 					}
 					break;
 				case DataType.Boolean:
-					if (t == typeof(bool))
+					if (desiredType == typeof(bool))
 						return value.Boolean;
 					if (isAnyString)
 						str = value.Boolean.ToString();
 					break;
 				case DataType.Number:
-					if (NumericTypes.Contains(t))
-						return DoubleToType(t, value.Number);
+					if (NumericTypes.Contains(desiredType))
+						return DoubleToType(desiredType, value.Number);
 					if (isAnyString)
 						str = value.Number.ToString();
 					break;
@@ -241,26 +249,26 @@ namespace MoonSharp.Interpreter.Interop
 						str = value.String;
 					break;
 				case DataType.Function:
-					if (t == typeof(Closure)) return value.Function;
+					if (desiredType == typeof(Closure)) return value.Function;
 					break;
 				case DataType.ClrFunction:
-					if (t == typeof(CallbackFunction)) return value.Callback;
+					if (desiredType == typeof(CallbackFunction)) return value.Callback;
 					break;
 				case DataType.UserData:
 					if (value.UserData.Object != null)
 					{
-						if (t.IsInstanceOfType(value.UserData.Object))
+						if (desiredType.IsInstanceOfType(value.UserData.Object))
 							return value.UserData.Object;
 						if (isAnyString)
 							str = value.UserData.Object.ToString();
 					}
 					break;
 				case DataType.Table:
-					if (t == typeof(Table) || t.IsAssignableFrom(typeof(Table)))
+					if (desiredType == typeof(Table) || desiredType.IsAssignableFrom(typeof(Table)))
 						return value.Table;
 					else
 					{
-						object o = ConvertTableToType(value.Table, t);
+						object o = ConvertTableToType(value.Table, desiredType);
 						if (o != null)
 							return o;
 					}
@@ -279,7 +287,7 @@ namespace MoonSharp.Interpreter.Interop
 					return str[0];
 			}
 
-			throw ScriptRuntimeException.ConvertObjectFailed(value.Type, t);
+			throw ScriptRuntimeException.ConvertObjectFailed(value.Type, desiredType);
 		}
 
 		private static object ConvertTableToType(Table table, Type t)

+ 34 - 0
src/MoonSharp.Interpreter/Interop/LinqHelpers.cs

@@ -0,0 +1,34 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace MoonSharp.Interpreter
+{
+	/// <summary>
+	/// LINQ helper methods
+	/// </summary>
+	public static class LinqHelpers
+	{
+		public static IEnumerable<T> Convert<T>(this IEnumerable<DynValue> enumerable, DataType type)
+		{
+			return enumerable.Where(v => v.Type == type).Select(v => v.ToObject<T>());
+		}
+
+		public static IEnumerable<DynValue> OfDataType(this IEnumerable<DynValue> enumerable, DataType type)
+		{
+			return enumerable.Where(v => v.Type == type);
+		}
+
+		public static IEnumerable<object> AsObjects(this IEnumerable<DynValue> enumerable)
+		{
+			return enumerable.Select(v => v.ToObject());
+		}
+
+		public static IEnumerable<T> AsObjects<T>(this IEnumerable<DynValue> enumerable)
+		{
+			return enumerable.Select(v => v.ToObject<T>());
+		}
+
+	}
+}

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

@@ -84,7 +84,7 @@ namespace MoonSharp.Interpreter.Interop
 		{
 			if (m_Properties.ContainsKey(idxname))
 			{
-				object o = ConversionHelper.MoonSharpValueToClrObject(value);
+				object o = ConversionHelper.MoonSharpValueToObjectOfType(value, m_Properties[idxname].PropertyInfo.PropertyType, null);
 				m_Properties[idxname].SetValue(obj, o, value.Type);
 			}
 			else

+ 14 - 2
src/MoonSharp.Interpreter/Interop/UserDataMethodDescriptor.cs

@@ -48,9 +48,21 @@ namespace MoonSharp.Interpreter.Interop
 
 			object[] pars = new object[m_Arguments.Length];
 
-			for (int i = 0; i < pars.Length; i++)
+			for (int i = 0, j = 0; i < pars.Length; i++)
 			{
-				pars[i] = ConversionHelper.MoonSharpValueToObjectOfType(args[i], m_Arguments[i], m_Defaults[i]);
+				if (m_Arguments[i] == typeof(Script))
+				{
+					pars[i] = script;
+				}
+				else if (m_Arguments[i] == typeof(ScriptExecutionContext))
+				{
+					pars[i] = context;
+				}
+				else
+				{
+					pars[i] = ConversionHelper.MoonSharpValueToObjectOfType(args[j], m_Arguments[i], m_Defaults[i]);
+					j++;
+				}
 			}
 
 

+ 1 - 0
src/MoonSharp.Interpreter/MoonSharp.Interpreter.csproj

@@ -147,6 +147,7 @@
     <Compile Include="Interface\IteratorHelper.cs" />
     <Compile Include="Interop\ConversionHelper.cs" />
     <Compile Include="Interop\EnumerableWrapper.cs" />
+    <Compile Include="Interop\LinqHelpers.cs" />
     <Compile Include="Interop\MoonSharpUserDataAttribute.cs" />
     <Compile Include="Interop\MoonSharpVisibleAttribute.cs" />
     <Compile Include="Interop\UserDataDelegateDescriptor.cs" />

+ 1 - 1
src/MoonSharp.Interpreter/Properties/AssemblyInfo.cs

@@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
 // You can specify all the values or you can default the Build and Revision Numbers 
 // by using the '*' as shown below:
 // [assembly: AssemblyVersion("1.0.*")]
-[assembly: AssemblyVersion("0.5.0.0")]
+[assembly: AssemblyVersion("0.5.2.0")]
 [assembly: AssemblyFileVersion("1.0.0.0")]

+ 7 - 7
src/PerformanceComparison/Program.cs

@@ -25,7 +25,7 @@ namespace PerformanceComparison
 			function move(n, src, dst, via)
 				if n > 0 then
 					move(n - 1, src, via, dst)
-					print(src, 'to', dst)
+					--print(src, 'to', dst)
 					move(n - 1, via, dst, src)
 				end
 			end
@@ -168,7 +168,7 @@ end
 			sw = Stopwatch.StartNew();
 			for (int i = 0; i < ITERATIONS; i++)
 			{
-				//fn.Call();
+				fn.Call();
 			}
 			sw.Stop();
 
@@ -178,11 +178,11 @@ end
 
 			Console.WriteLine("M# == NL ? {0}", g_MoonSharpStr.ToString() == g_NLuaStr.ToString());
 
-			//Console.WriteLine("=== Moon# ===");
-			//Console.WriteLine(g_MoonSharpStr.ToString());
-			//Console.WriteLine("");
-			//Console.WriteLine("=== NLua  ===");
-			//Console.WriteLine(g_NLuaStr.ToString());
+			Console.WriteLine("=== Moon# ===");
+			Console.WriteLine(g_MoonSharpStr.ToString());
+			Console.WriteLine("");
+			Console.WriteLine("=== NLua  ===");
+			Console.WriteLine(g_NLuaStr.ToString());
 
 			Console.ReadKey();
 		}

+ 15 - 0
src/deploy_release.cmd

@@ -4,6 +4,7 @@ md c:\temp\moonsharp_release
 md c:\temp\moonsharp_release\tests
 md c:\temp\moonsharp_release\repl
 md c:\temp\moonsharp_release\debugger
+md c:\temp\moonsharp_release\help
 md c:\temp\moonsharp_release\library
 
 robocopy /E C:\git\moonsharp\src\MoonSharpTests\bin\Release c:\temp\moonsharp_release\tests
@@ -11,5 +12,19 @@ robocopy /E C:\git\moonsharp\src\MoonSharp\bin\Release c:\temp\moonsharp_release
 robocopy /E C:\git\moonsharp\src\MoonSharp.Debugger\bin\Release c:\temp\moonsharp_release\debugger
 robocopy /E C:\git\moonsharp\src\MoonSharp.Interpreter\bin\Release c:\temp\moonsharp_release\library
 
+move "C:\git\moonsharp\src\MoonSharp.Documentation\Help\MoonSharp.Reference.Documentation.chm" c:\temp\moonsharp_release\help
 
+robocopy /E C:\git\moonsharp\src\MoonSharp.Interpreter\bin\Release c:\temp\moonsharp_release\library
+
+robocopy /E C:\git\moonsharp\src\MoonSharp.Documentation\Help\fti C:\git\moonsharp_org\reference\fti
+robocopy /E C:\git\moonsharp\src\MoonSharp.Documentation\Help\html C:\git\moonsharp_org\reference\html
+robocopy /E C:\git\moonsharp\src\MoonSharp.Documentation\Help\icons C:\git\moonsharp_org\reference\icons
+robocopy /E C:\git\moonsharp\src\MoonSharp.Documentation\Help\scripts C:\git\moonsharp_org\reference\scripts
+robocopy /E C:\git\moonsharp\src\MoonSharp.Documentation\Help\styles C:\git\moonsharp_org\reference\styles
+robocopy /E C:\git\moonsharp\src\MoonSharp.Documentation\Help\toc C:\git\moonsharp_org\reference\toc
+
+copy /Y index.html C:\git\moonsharp_org\reference
+copy /Y search.html C:\git\moonsharp_org\reference
+copy /Y WebKI.xml C:\git\moonsharp_org\reference
+copy /Y WebTOC.xml C:\git\moonsharp_org\reference