ObjectWrapper.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using Jint.Native;
  5. using Jint.Native.Object;
  6. using Jint.Runtime.Descriptors;
  7. using Jint.Runtime.Descriptors.Specialized;
  8. namespace Jint.Runtime.Interop
  9. {
  10. /// <summary>
  11. /// Wraps a CLR instance
  12. /// </summary>
  13. public sealed class ObjectWrapper : ObjectInstance, IObjectWrapper
  14. {
  15. public ObjectWrapper(Engine engine, object obj)
  16. : base(engine)
  17. {
  18. Target = obj;
  19. }
  20. public object Target { get; }
  21. public override void Put(string propertyName, JsValue value, bool throwOnError)
  22. {
  23. if (!CanPut(propertyName))
  24. {
  25. if (throwOnError)
  26. {
  27. ExceptionHelper.ThrowTypeError(Engine);
  28. }
  29. return;
  30. }
  31. var ownDesc = GetOwnProperty(propertyName);
  32. if (ownDesc == null)
  33. {
  34. if (throwOnError)
  35. {
  36. ExceptionHelper.ThrowTypeError(_engine, "Unknown member: " + propertyName);
  37. }
  38. }
  39. else
  40. {
  41. ownDesc.Value = value;
  42. }
  43. }
  44. public override PropertyDescriptor GetOwnProperty(string propertyName)
  45. {
  46. if (TryGetProperty(propertyName, out var x))
  47. {
  48. return x;
  49. }
  50. var type = Target.GetType();
  51. var key = (type, propertyName);
  52. if (!_engine.ClrPropertyDescriptorFactories.TryGetValue(key, out var factory))
  53. {
  54. factory = ResolveProperty(type, propertyName);
  55. _engine.ClrPropertyDescriptorFactories[key] = factory;
  56. }
  57. var descriptor = factory(_engine, Target);
  58. AddProperty(propertyName, descriptor);
  59. return descriptor;
  60. }
  61. private static Func<Engine, object, PropertyDescriptor> ResolveProperty(Type type, string propertyName)
  62. {
  63. // look for a property
  64. PropertyInfo property = null;
  65. foreach (var p in type.GetProperties(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public))
  66. {
  67. if (EqualsIgnoreCasing(p.Name, propertyName))
  68. {
  69. property = p;
  70. break;
  71. }
  72. }
  73. if (property != null)
  74. {
  75. return (engine, target) => new PropertyInfoDescriptor(engine, property, target);
  76. }
  77. // look for a field
  78. FieldInfo field = null;
  79. foreach (var f in type.GetFields(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public))
  80. {
  81. if (EqualsIgnoreCasing(f.Name, propertyName))
  82. {
  83. field = f;
  84. break;
  85. }
  86. }
  87. if (field != null)
  88. {
  89. return (engine, target) => new FieldInfoDescriptor(engine, field, target);
  90. }
  91. // if no properties were found then look for a method
  92. List<MethodInfo> methods = null;
  93. foreach (var m in type.GetMethods(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public))
  94. {
  95. if (EqualsIgnoreCasing(m.Name, propertyName))
  96. {
  97. methods = methods ?? new List<MethodInfo>();
  98. methods.Add(m);
  99. }
  100. }
  101. if (methods?.Count > 0)
  102. {
  103. return (engine, target) => new PropertyDescriptor(new MethodInfoFunctionInstance(engine, methods.ToArray()), PropertyFlag.OnlyEnumerable);
  104. }
  105. // if no methods are found check if target implemented indexing
  106. PropertyInfo first = null;
  107. foreach (var p in type.GetProperties())
  108. {
  109. if (p.GetIndexParameters().Length != 0)
  110. {
  111. first = p;
  112. break;
  113. }
  114. }
  115. if (first != null)
  116. {
  117. return (engine, target) => new IndexDescriptor(engine, propertyName, target);
  118. }
  119. // try to find a single explicit property implementation
  120. List<PropertyInfo> list = null;
  121. foreach (Type iface in type.GetInterfaces())
  122. {
  123. foreach (var iprop in iface.GetProperties())
  124. {
  125. if (EqualsIgnoreCasing(iprop.Name, propertyName))
  126. {
  127. list = list ?? new List<PropertyInfo>();
  128. list.Add(iprop);
  129. }
  130. }
  131. }
  132. if (list?.Count == 1)
  133. {
  134. return (engine, target) => new PropertyInfoDescriptor(engine, list[0], target);
  135. }
  136. // try to find explicit method implementations
  137. List<MethodInfo> explicitMethods = null;
  138. foreach (Type iface in type.GetInterfaces())
  139. {
  140. foreach (var imethod in iface.GetMethods())
  141. {
  142. if (EqualsIgnoreCasing(imethod.Name, propertyName))
  143. {
  144. explicitMethods = explicitMethods ?? new List<MethodInfo>();
  145. explicitMethods.Add(imethod);
  146. }
  147. }
  148. }
  149. if (explicitMethods?.Count > 0)
  150. {
  151. return (engine, target) => new PropertyDescriptor(new MethodInfoFunctionInstance(engine, explicitMethods.ToArray()), PropertyFlag.OnlyEnumerable);
  152. }
  153. // try to find explicit indexer implementations
  154. List<PropertyInfo> explicitIndexers = null;
  155. foreach (Type iface in type.GetInterfaces())
  156. {
  157. foreach (var iprop in iface.GetProperties())
  158. {
  159. if (iprop.GetIndexParameters().Length != 0)
  160. {
  161. explicitIndexers = explicitIndexers ?? new List<PropertyInfo>();
  162. explicitIndexers.Add(iprop);
  163. }
  164. }
  165. }
  166. if (explicitIndexers?.Count == 1)
  167. {
  168. return (engine, target) => new IndexDescriptor(engine, explicitIndexers[0].DeclaringType, propertyName, target);
  169. }
  170. return (engine, target) => PropertyDescriptor.Undefined;
  171. }
  172. private static bool EqualsIgnoreCasing(string s1, string s2)
  173. {
  174. bool equals = false;
  175. if (s1.Length == s2.Length)
  176. {
  177. if (s1.Length > 0)
  178. {
  179. equals = char.ToLowerInvariant(s1[0]) == char.ToLowerInvariant(s2[0]);
  180. }
  181. if (equals && s1.Length > 1)
  182. {
  183. equals = s1.Substring(1) == s2.Substring(1);
  184. }
  185. }
  186. return equals;
  187. }
  188. }
  189. }