ObjectWrapper.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Reflection;
  5. using Jint.Native;
  6. using Jint.Native.Object;
  7. using Jint.Runtime.Descriptors;
  8. using Jint.Runtime.Descriptors.Specialized;
  9. namespace Jint.Runtime.Interop
  10. {
  11. /// <summary>
  12. /// Wraps a CLR instance
  13. /// </summary>
  14. public sealed class ObjectWrapper : ObjectInstance, IObjectWrapper
  15. {
  16. public ObjectWrapper(Engine engine, object obj)
  17. : base(engine)
  18. {
  19. Target = obj;
  20. var type = obj.GetType();
  21. if (ObjectIsArrayLikeClrCollection(type))
  22. {
  23. IsArrayLike = true;
  24. // create a forwarder to produce length from Count or Length, whichever is present
  25. var lengthProperty = type.GetProperty("Count") ?? type.GetProperty("Length");
  26. if (lengthProperty is null)
  27. {
  28. ExceptionHelper.ThrowArgumentException("collection is supposed to either have Count or Length property");
  29. return;
  30. }
  31. var functionInstance = new ClrFunctionInstance(engine, "length", (thisObj, arguments) => JsNumber.Create((int) lengthProperty.GetValue(obj)));
  32. var descriptor = new GetSetPropertyDescriptor(functionInstance, Undefined, PropertyFlag.Configurable);
  33. AddProperty(CommonProperties.Length, descriptor);
  34. }
  35. }
  36. internal static bool ObjectIsArrayLikeClrCollection(Type type)
  37. {
  38. if (typeof(ICollection).IsAssignableFrom(type))
  39. {
  40. return true;
  41. }
  42. foreach (var interfaceType in type.GetInterfaces())
  43. {
  44. if (!interfaceType.IsGenericType)
  45. {
  46. continue;
  47. }
  48. if (interfaceType.GetGenericTypeDefinition() == typeof(IReadOnlyCollection<>)
  49. || interfaceType.GetGenericTypeDefinition() == typeof(ICollection<>))
  50. {
  51. return true;
  52. }
  53. }
  54. return false;
  55. }
  56. public object Target { get; }
  57. public override bool IsArrayLike { get; }
  58. public override bool Set(JsValue property, JsValue value, JsValue receiver)
  59. {
  60. if (!CanPut(property))
  61. {
  62. return false;
  63. }
  64. var ownDesc = GetOwnProperty(property);
  65. if (ownDesc == null)
  66. {
  67. return false;
  68. }
  69. ownDesc.Value = value;
  70. return true;
  71. }
  72. public override PropertyDescriptor GetOwnProperty(JsValue property)
  73. {
  74. if (property.IsSymbol())
  75. {
  76. return PropertyDescriptor.Undefined;
  77. }
  78. if (TryGetProperty(property, out var x))
  79. {
  80. return x;
  81. }
  82. var type = Target.GetType();
  83. var key = new Engine.ClrPropertyDescriptorFactoriesKey(type, property.ToString());
  84. if (!_engine.ClrPropertyDescriptorFactories.TryGetValue(key, out var factory))
  85. {
  86. factory = ResolveProperty(type, property.ToString());
  87. _engine.ClrPropertyDescriptorFactories[key] = factory;
  88. }
  89. var descriptor = factory(_engine, Target);
  90. AddProperty(property, descriptor);
  91. return descriptor;
  92. }
  93. private Func<Engine, object, PropertyDescriptor> ResolveProperty(Type type, string propertyName)
  94. {
  95. var isNumber = uint.TryParse(propertyName, out _);
  96. // properties and fields cannot be numbers
  97. if (!isNumber)
  98. {
  99. // look for a property
  100. PropertyInfo property = null;
  101. foreach (var p in type.GetProperties(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public))
  102. {
  103. if (EqualsIgnoreCasing(p.Name, propertyName))
  104. {
  105. property = p;
  106. break;
  107. }
  108. }
  109. if (property != null)
  110. {
  111. return (engine, target) => new PropertyInfoDescriptor(engine, property, target);
  112. }
  113. // look for a field
  114. FieldInfo field = null;
  115. foreach (var f in type.GetFields(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public))
  116. {
  117. if (EqualsIgnoreCasing(f.Name, propertyName))
  118. {
  119. field = f;
  120. break;
  121. }
  122. }
  123. if (field != null)
  124. {
  125. return (engine, target) => new FieldInfoDescriptor(engine, field, target);
  126. }
  127. // if no properties were found then look for a method
  128. List<MethodInfo> methods = null;
  129. foreach (var m in type.GetMethods(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public))
  130. {
  131. if (EqualsIgnoreCasing(m.Name, propertyName))
  132. {
  133. methods ??= new List<MethodInfo>();
  134. methods.Add(m);
  135. }
  136. }
  137. if (methods?.Count > 0)
  138. {
  139. return (engine, target) => new PropertyDescriptor(new MethodInfoFunctionInstance(engine, methods.ToArray()), PropertyFlag.OnlyEnumerable);
  140. }
  141. }
  142. // if no methods are found check if target implemented indexing
  143. if (IndexDescriptor.TryFindIndexer(_engine, type, propertyName, out _, out _, out _))
  144. {
  145. return (engine, target) => new IndexDescriptor(engine, propertyName, target);
  146. }
  147. // try to find a single explicit property implementation
  148. List<PropertyInfo> list = null;
  149. foreach (Type iface in type.GetInterfaces())
  150. {
  151. foreach (var iprop in iface.GetProperties())
  152. {
  153. if (EqualsIgnoreCasing(iprop.Name, propertyName))
  154. {
  155. list ??= new List<PropertyInfo>();
  156. list.Add(iprop);
  157. }
  158. }
  159. }
  160. if (list?.Count == 1)
  161. {
  162. return (engine, target) => new PropertyInfoDescriptor(engine, list[0], target);
  163. }
  164. // try to find explicit method implementations
  165. List<MethodInfo> explicitMethods = null;
  166. foreach (Type iface in type.GetInterfaces())
  167. {
  168. foreach (var imethod in iface.GetMethods())
  169. {
  170. if (EqualsIgnoreCasing(imethod.Name, propertyName))
  171. {
  172. explicitMethods ??= new List<MethodInfo>();
  173. explicitMethods.Add(imethod);
  174. }
  175. }
  176. }
  177. if (explicitMethods?.Count > 0)
  178. {
  179. return (engine, target) => new PropertyDescriptor(new MethodInfoFunctionInstance(engine, explicitMethods.ToArray()), PropertyFlag.OnlyEnumerable);
  180. }
  181. // try to find explicit indexer implementations
  182. foreach (Type interfaceType in type.GetInterfaces())
  183. {
  184. if (IndexDescriptor.TryFindIndexer(_engine, interfaceType, propertyName, out _, out _, out _))
  185. {
  186. return (engine, target) => new IndexDescriptor(engine, interfaceType, propertyName, target);
  187. }
  188. }
  189. return (engine, target) => PropertyDescriptor.Undefined;
  190. }
  191. private static bool EqualsIgnoreCasing(string s1, string s2)
  192. {
  193. bool equals = false;
  194. if (s1.Length == s2.Length)
  195. {
  196. if (s1.Length > 0)
  197. {
  198. equals = char.ToLowerInvariant(s1[0]) == char.ToLowerInvariant(s2[0]);
  199. }
  200. if (equals && s1.Length > 1)
  201. {
  202. equals = s1.Substring(1) == s2.Substring(1);
  203. }
  204. }
  205. return equals;
  206. }
  207. }
  208. }