StandardUserDataDescriptor.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 (MethodInfo mi in type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Static))
  29. {
  30. if (CheckVisibility(mi.GetCustomAttributes(true), mi.IsPublic))
  31. {
  32. if (mi.IsSpecialName)
  33. continue;
  34. var md = new StandardUserDataMethodDescriptor(mi, this.AccessMode);
  35. if (m_Methods.ContainsKey(md.Name))
  36. continue;
  37. //throw new ArgumentException(string.Format("{0}.{1} has overloads", Name, md.Name));
  38. m_Methods.Add(md.Name, md);
  39. }
  40. }
  41. foreach (PropertyInfo pi in type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static))
  42. {
  43. if (CheckVisibility(pi.GetCustomAttributes(true), IsPropertyInfoPublic(pi)))
  44. {
  45. var pd = new StandardUserDataPropertyDescriptor(pi, this.AccessMode);
  46. m_Properties.Add(pd.Name, pd);
  47. }
  48. }
  49. }
  50. }
  51. private bool IsPropertyInfoPublic(PropertyInfo pi)
  52. {
  53. MethodInfo getter = pi.GetGetMethod();
  54. MethodInfo setter = pi.GetSetMethod();
  55. return (getter != null && getter.IsPublic) || (setter != null && setter.IsPublic);
  56. }
  57. private bool CheckVisibility(object[] attributes, bool isPublic)
  58. {
  59. MoonSharpVisibleAttribute va = attributes.OfType<MoonSharpVisibleAttribute>().SingleOrDefault();
  60. if (va != null)
  61. return va.Visible;
  62. else
  63. return isPublic;
  64. }
  65. public DynValue Index(Script script, object obj, DynValue index)
  66. {
  67. if (index.Type != DataType.String)
  68. throw ScriptRuntimeException.BadArgument(1, string.Format("userdata<{0}>.__index", this.Name), "string", index.Type.ToLuaTypeString(), false);
  69. DynValue v = TryIndex(script, obj, index.String);
  70. if (v == null) v = TryIndex(script, obj, UpperFirstLetter(index.String));
  71. if (v == null) v = TryIndex(script, obj, Camelify(index.String));
  72. if (v == null) v = TryIndex(script, obj, UpperFirstLetter(Camelify(index.String)));
  73. return v;
  74. }
  75. protected virtual DynValue TryIndex(Script script, object obj, string indexName)
  76. {
  77. StandardUserDataMethodDescriptor mdesc;
  78. if (m_Methods.TryGetValue(indexName, out mdesc))
  79. return DynValue.NewCallback(mdesc.GetCallback(script, obj));
  80. StandardUserDataPropertyDescriptor pdesc;
  81. if (m_Properties.TryGetValue(indexName, out pdesc))
  82. {
  83. object o = pdesc.GetValue(obj);
  84. return ConversionHelper.ClrObjectToComplexMoonSharpValue(script, o);
  85. }
  86. return null;
  87. }
  88. public bool SetIndex(Script script, object obj, DynValue index, DynValue value)
  89. {
  90. if (index.Type != DataType.String)
  91. throw ScriptRuntimeException.BadArgument(1, string.Format("userdata<{0}>.__setindex", this.Name), "string", index.Type.ToLuaTypeString(), false);
  92. bool v = TrySetIndex(script, obj, index.String, value);
  93. if (!v) v = TrySetIndex(script, obj, UpperFirstLetter(index.String), value);
  94. if (!v) v = TrySetIndex(script, obj, Camelify(index.String), value);
  95. if (!v) v = TrySetIndex(script, obj, UpperFirstLetter(Camelify(index.String)), value);
  96. return v;
  97. }
  98. protected virtual bool TrySetIndex(Script script, object obj, string indexName, DynValue value)
  99. {
  100. StandardUserDataPropertyDescriptor pdesc;
  101. if (m_Properties.TryGetValue(indexName, out pdesc))
  102. {
  103. object o = ConversionHelper.MoonSharpValueToObjectOfType(value, pdesc.PropertyInfo.PropertyType, null);
  104. pdesc.SetValue(obj, o, value.Type);
  105. return true;
  106. }
  107. else
  108. {
  109. return false;
  110. }
  111. }
  112. internal void Optimize()
  113. {
  114. foreach (var m in this.m_Methods.Values)
  115. m.Optimize();
  116. foreach (var m in this.m_Properties.Values)
  117. {
  118. m.OptimizeGetter();
  119. m.OptimizeSetter();
  120. }
  121. }
  122. protected static string Camelify(string name)
  123. {
  124. StringBuilder sb = new StringBuilder(name.Length);
  125. bool lastWasUnderscore = false;
  126. for (int i = 0; i < name.Length; i++)
  127. {
  128. if (name[i] == '_' && i != 0)
  129. {
  130. lastWasUnderscore = true;
  131. }
  132. else
  133. {
  134. if (lastWasUnderscore)
  135. sb.Append(char.ToUpperInvariant(name[i]));
  136. else
  137. sb.Append(name[i]);
  138. lastWasUnderscore = false;
  139. }
  140. }
  141. return sb.ToString();
  142. }
  143. protected static string UpperFirstLetter(string name)
  144. {
  145. if (!string.IsNullOrEmpty(name))
  146. return char.ToUpperInvariant(name[0]) + name.Substring(1);
  147. return name;
  148. }
  149. public string AsString(object obj)
  150. {
  151. return (obj != null) ? obj.ToString() : null;
  152. }
  153. public DynValue MetaIndex(Script script, object obj, string metaname)
  154. {
  155. // TODO: meta access to overloaded operators ?
  156. return null;
  157. }
  158. }
  159. }