StandardUserDataDescriptor.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. using System.Threading;
  7. using MoonSharp.Interpreter.Execution;
  8. namespace MoonSharp.Interpreter.Interop
  9. {
  10. public class StandardUserDataDescriptor : IUserDataDescriptor
  11. {
  12. public string Name { get; private set; }
  13. public Type Type { get; private set; }
  14. public InteropAccessMode AccessMode { get; private set; }
  15. public string FriendlyName { get; private set; }
  16. private Dictionary<string, StandardUserDataMethodDescriptor> m_Methods = new Dictionary<string, StandardUserDataMethodDescriptor>();
  17. private Dictionary<string, StandardUserDataPropertyDescriptor> m_Properties = new Dictionary<string, StandardUserDataPropertyDescriptor>();
  18. protected internal StandardUserDataDescriptor(Type type, InteropAccessMode accessMode, string friendlyName)
  19. {
  20. if (accessMode == InteropAccessMode.Default)
  21. accessMode = UserData.DefaultAccessMode;
  22. Type = type;
  23. Name = type.FullName;
  24. AccessMode = accessMode;
  25. FriendlyName = friendlyName;
  26. if (AccessMode != InteropAccessMode.HideMembers)
  27. {
  28. foreach (ConstructorInfo ci in type.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static))
  29. {
  30. if (CheckVisibility(ci.GetCustomAttributes(true), ci.IsPublic))
  31. {
  32. var md = new StandardUserDataMethodDescriptor(ci, this.AccessMode);
  33. m_Methods.Add("__new", md);
  34. break;
  35. }
  36. }
  37. foreach (MethodInfo mi in type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Static))
  38. {
  39. if (CheckVisibility(mi.GetCustomAttributes(true), mi.IsPublic))
  40. {
  41. if (mi.IsSpecialName)
  42. continue;
  43. var md = new StandardUserDataMethodDescriptor(mi, this.AccessMode);
  44. if (m_Methods.ContainsKey(md.Name))
  45. continue;
  46. //throw new ArgumentException(string.Format("{0}.{1} has overloads", Name, md.Name));
  47. m_Methods.Add(md.Name, md);
  48. }
  49. }
  50. foreach (PropertyInfo pi in type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static))
  51. {
  52. if (CheckVisibility(pi.GetCustomAttributes(true), IsPropertyInfoPublic(pi)))
  53. {
  54. var pd = new StandardUserDataPropertyDescriptor(pi, this.AccessMode);
  55. m_Properties.Add(pd.Name, pd);
  56. }
  57. }
  58. }
  59. }
  60. private bool IsPropertyInfoPublic(PropertyInfo pi)
  61. {
  62. MethodInfo getter = pi.GetGetMethod();
  63. MethodInfo setter = pi.GetSetMethod();
  64. return (getter != null && getter.IsPublic) || (setter != null && setter.IsPublic);
  65. }
  66. private bool CheckVisibility(object[] attributes, bool isPublic)
  67. {
  68. MoonSharpVisibleAttribute va = attributes.OfType<MoonSharpVisibleAttribute>().SingleOrDefault();
  69. if (va != null)
  70. return va.Visible;
  71. else
  72. return isPublic;
  73. }
  74. public DynValue Index(Script script, object obj, DynValue index)
  75. {
  76. if (index.Type != DataType.String)
  77. throw ScriptRuntimeException.BadArgument(1, string.Format("userdata<{0}>.__index", this.Name), "string", index.Type.ToLuaTypeString(), false);
  78. DynValue v = TryIndex(script, obj, index.String);
  79. if (v == null) v = TryIndex(script, obj, UpperFirstLetter(index.String));
  80. if (v == null) v = TryIndex(script, obj, Camelify(index.String));
  81. if (v == null) v = TryIndex(script, obj, UpperFirstLetter(Camelify(index.String)));
  82. return v;
  83. }
  84. protected virtual DynValue TryIndex(Script script, object obj, string indexName)
  85. {
  86. StandardUserDataMethodDescriptor mdesc;
  87. if (m_Methods.TryGetValue(indexName, out mdesc))
  88. return DynValue.NewCallback(mdesc.GetCallback(script, obj));
  89. StandardUserDataPropertyDescriptor pdesc;
  90. if (m_Properties.TryGetValue(indexName, out pdesc))
  91. {
  92. object o = pdesc.GetValue(obj);
  93. return ConversionHelper.ClrObjectToComplexMoonSharpValue(script, o);
  94. }
  95. return null;
  96. }
  97. public bool SetIndex(Script script, object obj, DynValue index, DynValue value)
  98. {
  99. if (index.Type != DataType.String)
  100. throw ScriptRuntimeException.BadArgument(1, string.Format("userdata<{0}>.__setindex", this.Name), "string", index.Type.ToLuaTypeString(), false);
  101. bool v = TrySetIndex(script, obj, index.String, value);
  102. if (!v) v = TrySetIndex(script, obj, UpperFirstLetter(index.String), value);
  103. if (!v) v = TrySetIndex(script, obj, Camelify(index.String), value);
  104. if (!v) v = TrySetIndex(script, obj, UpperFirstLetter(Camelify(index.String)), value);
  105. return v;
  106. }
  107. protected virtual bool TrySetIndex(Script script, object obj, string indexName, DynValue value)
  108. {
  109. StandardUserDataPropertyDescriptor pdesc;
  110. if (m_Properties.TryGetValue(indexName, out pdesc))
  111. {
  112. object o = ConversionHelper.MoonSharpValueToObjectOfType(value, pdesc.PropertyInfo.PropertyType, null);
  113. pdesc.SetValue(obj, o, value.Type);
  114. return true;
  115. }
  116. else
  117. {
  118. return false;
  119. }
  120. }
  121. internal void Optimize()
  122. {
  123. foreach (var m in this.m_Methods.Values)
  124. m.Optimize();
  125. foreach (var m in this.m_Properties.Values)
  126. {
  127. m.OptimizeGetter();
  128. m.OptimizeSetter();
  129. }
  130. }
  131. protected static string Camelify(string name)
  132. {
  133. StringBuilder sb = new StringBuilder(name.Length);
  134. bool lastWasUnderscore = false;
  135. for (int i = 0; i < name.Length; i++)
  136. {
  137. if (name[i] == '_' && i != 0)
  138. {
  139. lastWasUnderscore = true;
  140. }
  141. else
  142. {
  143. if (lastWasUnderscore)
  144. sb.Append(char.ToUpperInvariant(name[i]));
  145. else
  146. sb.Append(name[i]);
  147. lastWasUnderscore = false;
  148. }
  149. }
  150. return sb.ToString();
  151. }
  152. protected static string UpperFirstLetter(string name)
  153. {
  154. if (!string.IsNullOrEmpty(name))
  155. return char.ToUpperInvariant(name[0]) + name.Substring(1);
  156. return name;
  157. }
  158. public string AsString(object obj)
  159. {
  160. return (obj != null) ? obj.ToString() : null;
  161. }
  162. public DynValue MetaIndex(Script script, object obj, string metaname)
  163. {
  164. // TODO: meta access to overloaded operators ?
  165. return null;
  166. }
  167. }
  168. }