StandardUserDataFieldDescriptor.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Threading;
  8. using MoonSharp.Interpreter.Diagnostics;
  9. using MoonSharp.Interpreter.Interop.Converters;
  10. namespace MoonSharp.Interpreter.Interop
  11. {
  12. /// <summary>
  13. /// Class providing easier marshalling of CLR fields
  14. /// </summary>
  15. public class StandardUserDataFieldDescriptor
  16. {
  17. /// <summary>
  18. /// Gets the FieldInfo got by reflection
  19. /// </summary>
  20. public FieldInfo FieldInfo { get; private set; }
  21. /// <summary>
  22. /// Gets the <see cref="InteropAccessMode" />
  23. /// </summary>
  24. public InteropAccessMode AccessMode { get; private set; }
  25. /// <summary>
  26. /// Gets a value indicating whether the described property is static.
  27. /// </summary>
  28. public bool IsStatic { get; private set; }
  29. /// <summary>
  30. /// Gets the name of the property
  31. /// </summary>
  32. public string Name { get; private set; }
  33. Func<object, object> m_OptimizedGetter = null;
  34. /// <summary>
  35. /// Initializes a new instance of the <see cref="StandardUserDataPropertyDescriptor"/> class.
  36. /// </summary>
  37. /// <param name="fi">The FieldInfo.</param>
  38. /// <param name="accessMode">The <see cref="InteropAccessMode" /> </param>
  39. internal StandardUserDataFieldDescriptor(FieldInfo fi, InteropAccessMode accessMode)
  40. {
  41. if (Script.GlobalOptions.Platform.IsRunningOnAOT())
  42. accessMode = InteropAccessMode.Reflection;
  43. this.FieldInfo = fi;
  44. this.AccessMode = accessMode;
  45. this.Name = fi.Name;
  46. this.IsStatic = this.FieldInfo.IsStatic;
  47. if (AccessMode == InteropAccessMode.Preoptimized)
  48. {
  49. this.OptimizeGetter();
  50. }
  51. }
  52. /// <summary>
  53. /// Gets the value of the property
  54. /// </summary>
  55. /// <param name="script">The script.</param>
  56. /// <param name="obj">The object.</param>
  57. /// <returns></returns>
  58. public DynValue GetValue(Script script, object obj)
  59. {
  60. if (AccessMode == InteropAccessMode.LazyOptimized && m_OptimizedGetter == null)
  61. OptimizeGetter();
  62. object result = null;
  63. if (m_OptimizedGetter != null)
  64. result = m_OptimizedGetter(obj);
  65. else
  66. result = FieldInfo.GetValue(obj);
  67. return ClrToScriptConversions.ObjectToDynValue(script, result);
  68. }
  69. internal void OptimizeGetter()
  70. {
  71. using (PerformanceStatistics.StartGlobalStopwatch(PerformanceCounter.AdaptersCompilation))
  72. {
  73. if (IsStatic)
  74. {
  75. var paramExp = Expression.Parameter(typeof(object), "dummy");
  76. var propAccess = Expression.Field(null, FieldInfo);
  77. var castPropAccess = Expression.Convert(propAccess, typeof(object));
  78. var lambda = Expression.Lambda<Func<object, object>>(castPropAccess, paramExp);
  79. Interlocked.Exchange(ref m_OptimizedGetter, lambda.Compile());
  80. }
  81. else
  82. {
  83. var paramExp = Expression.Parameter(typeof(object), "obj");
  84. var castParamExp = Expression.Convert(paramExp, this.FieldInfo.DeclaringType);
  85. var propAccess = Expression.Field(castParamExp, FieldInfo);
  86. var castPropAccess = Expression.Convert(propAccess, typeof(object));
  87. var lambda = Expression.Lambda<Func<object, object>>(castPropAccess, paramExp);
  88. Interlocked.Exchange(ref m_OptimizedGetter, lambda.Compile());
  89. }
  90. }
  91. }
  92. /// <summary>
  93. /// Sets the value of the property
  94. /// </summary>
  95. /// <param name="script">The script.</param>
  96. /// <param name="obj">The object.</param>
  97. /// <param name="v">The value to set.</param>
  98. public void SetValue(Script script, object obj, DynValue v)
  99. {
  100. object value = ScriptToClrConversions.DynValueToObjectOfType(v, this.FieldInfo.FieldType, null, false);
  101. try
  102. {
  103. if (value is double)
  104. value = NumericConversions.DoubleToType(FieldInfo.FieldType, (double)value);
  105. FieldInfo.SetValue(IsStatic ? null : obj, value);
  106. }
  107. catch (ArgumentException)
  108. {
  109. // non-optimized setters fall here
  110. throw ScriptRuntimeException.UserDataArgumentTypeMismatch(v.Type, FieldInfo.FieldType);
  111. }
  112. catch (InvalidCastException)
  113. {
  114. // optimized setters fall here
  115. throw ScriptRuntimeException.UserDataArgumentTypeMismatch(v.Type, FieldInfo.FieldType);
  116. }
  117. }
  118. /// <summary>
  119. /// Gets the getter of the property as a DynValue containing a callback
  120. /// </summary>
  121. /// <param name="script">The script.</param>
  122. /// <param name="obj">The object.</param>
  123. /// <returns></returns>
  124. public DynValue GetGetterCallbackAsDynValue(Script script, object obj)
  125. {
  126. return DynValue.NewCallback((p1, p2) => GetValue(script, obj));
  127. }
  128. }
  129. }