Browse Source

Fixed custom descriptors

Xanathar 11 years ago
parent
commit
b40dc7b1ca

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

@@ -84,6 +84,7 @@ namespace MoonSharp.Interpreter.Tests.EndToEnd
 			string Test2();
 			string Test2();
 		}
 		}
 
 
+
 		public class SomeOtherClass
 		public class SomeOtherClass
 		{
 		{
 			public string Test1()
 			public string Test1()
@@ -97,6 +98,47 @@ namespace MoonSharp.Interpreter.Tests.EndToEnd
 			}
 			}
 		}
 		}
 
 
+
+		public class SomeOtherClassCustomDescriptor
+		{
+		}
+
+		public class CustomDescriptor : IUserDataDescriptor
+		{
+			public string Name
+			{
+				get { return "ciao"; }
+			}
+
+			public Type Type
+			{
+				get { return typeof(SomeOtherClassCustomDescriptor); }
+			}
+
+			public DynValue Index(Script script, object obj, DynValue index)
+			{
+				return DynValue.NewNumber(index.Number * 4);
+			}
+
+			public bool SetIndex(Script script, object obj, DynValue index, DynValue value)
+			{
+				throw new NotImplementedException();
+			}
+
+			public string AsString(object obj)
+			{
+				return null;
+			}
+
+			public DynValue MetaIndex(Script script, object obj, string metaname)
+			{
+				throw new NotImplementedException();
+			}
+		}
+
+
+
+
 		public class SelfDescribingClass : IUserDataType
 		public class SelfDescribingClass : IUserDataType
 		{
 		{
 			public DynValue Index(Script script, DynValue index)
 			public DynValue Index(Script script, DynValue index)
@@ -481,9 +523,34 @@ namespace MoonSharp.Interpreter.Tests.EndToEnd
 
 
 			Assert.AreEqual(DataType.Number, res.Type);
 			Assert.AreEqual(DataType.Number, res.Type);
 			Assert.AreEqual(18, res.Number);
 			Assert.AreEqual(18, res.Number);
-
 		}
 		}
 
 
+		[Test]
+		public void Interop_TestCustomDescribedType()
+		{
+			UserData.UnregisterType<SomeOtherClassCustomDescriptor>();
+
+			string script = @"    
+				a = myobj[1];
+				b = myobj[2];
+				c = myobj[3];
+				
+				return a + b + c;
+			";
+
+			Script S = new Script();
+
+			SomeOtherClassCustomDescriptor obj = new SomeOtherClassCustomDescriptor();
+
+			UserData.RegisterType<SomeOtherClassCustomDescriptor>(new CustomDescriptor());
+
+			S.Globals.Set("myobj", UserData.Create(obj));
+
+			DynValue res = S.DoString(script);
+
+			Assert.AreEqual(DataType.Number, res.Type);
+			Assert.AreEqual(24, res.Number);
+		}
 
 
 	}
 	}
 }
 }

+ 13 - 3
src/MoonSharp.Interpreter/DataTypes/UserData.cs

@@ -41,6 +41,16 @@ namespace MoonSharp.Interpreter
 			RegisterType_Impl(type, accessMode, friendlyName, null);
 			RegisterType_Impl(type, accessMode, friendlyName, null);
 		}
 		}
 
 
+		public static void RegisterType<T>(IUserDataDescriptor customDescriptor)
+		{
+			RegisterType_Impl(typeof(T), InteropAccessMode.Default, null, customDescriptor);
+		}
+
+		public static void RegisterType(Type type, IUserDataDescriptor customDescriptor)
+		{
+			RegisterType_Impl(type, InteropAccessMode.Default, null, customDescriptor);
+		}
+
 		public static void RegisterAssembly(Assembly asm = null)
 		public static void RegisterAssembly(Assembly asm = null)
 		{
 		{
 			asm = asm ?? Assembly.GetCallingAssembly();
 			asm = asm ?? Assembly.GetCallingAssembly();
@@ -140,13 +150,13 @@ namespace MoonSharp.Interpreter
 						if (type.GetInterfaces().Any(ii => ii == typeof(IUserDataType)))
 						if (type.GetInterfaces().Any(ii => ii == typeof(IUserDataType)))
 						{
 						{
 							AutoDescribingUserDataDescriptor audd = new AutoDescribingUserDataDescriptor(type, friendlyName);
 							AutoDescribingUserDataDescriptor audd = new AutoDescribingUserDataDescriptor(type, friendlyName);
-							s_Registry.Add(audd.Type, audd);
+							s_Registry.Add(type, audd);
 							return audd;
 							return audd;
 						}
 						}
 						else
 						else
 						{
 						{
 							StandardUserDataDescriptor udd = new StandardUserDataDescriptor(type, accessMode, friendlyName);
 							StandardUserDataDescriptor udd = new StandardUserDataDescriptor(type, accessMode, friendlyName);
-							s_Registry.Add(udd.Type, udd);
+							s_Registry.Add(type, udd);
 
 
 							if (accessMode == InteropAccessMode.BackgroundOptimized)
 							if (accessMode == InteropAccessMode.BackgroundOptimized)
 							{
 							{
@@ -158,7 +168,7 @@ namespace MoonSharp.Interpreter
 					}
 					}
 					else
 					else
 					{
 					{
-						s_Registry.Add(descriptor.Type, descriptor);
+						s_Registry.Add(type, descriptor);
 						return descriptor;
 						return descriptor;
 					}
 					}
 				}
 				}