ObjectWrapper.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. if (obj is ICollection collection)
  21. {
  22. IsArrayLike = true;
  23. // create a forwarder to produce length from Count
  24. var functionInstance = new ClrFunctionInstance(engine, "length", (thisObj, arguments) => collection.Count);
  25. var descriptor = new GetSetPropertyDescriptor(functionInstance, Undefined, PropertyFlag.Configurable);
  26. AddProperty("length", descriptor);
  27. }
  28. }
  29. public object Target { get; }
  30. internal override bool IsArrayLike { get; }
  31. public override bool Set(in Key propertyName, JsValue value, JsValue receiver)
  32. {
  33. if (!CanPut(propertyName))
  34. {
  35. return false;
  36. }
  37. var ownDesc = GetOwnProperty(propertyName);
  38. if (ownDesc == null)
  39. {
  40. return false;
  41. }
  42. ownDesc.Value = value;
  43. return true;
  44. }
  45. public override PropertyDescriptor GetOwnProperty(in Key propertyName)
  46. {
  47. if (TryGetProperty(propertyName, out var x))
  48. {
  49. return x;
  50. }
  51. var type = Target.GetType();
  52. var key = new Engine.ClrPropertyDescriptorFactoriesKey(type, propertyName);
  53. if (!_engine.ClrPropertyDescriptorFactories.TryGetValue(key, out var factory))
  54. {
  55. factory = ResolveProperty(type, propertyName);
  56. _engine.ClrPropertyDescriptorFactories[key] = factory;
  57. }
  58. var descriptor = factory(_engine, Target);
  59. AddProperty(propertyName, descriptor);
  60. return descriptor;
  61. }
  62. private static Func<Engine, object, PropertyDescriptor> ResolveProperty(Type type, string propertyName)
  63. {
  64. var isNumber = uint.TryParse(propertyName, out _);
  65. // properties and fields cannot be numbers
  66. if (!isNumber)
  67. {
  68. // look for a property
  69. PropertyInfo property = null;
  70. foreach (var p in type.GetProperties(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public))
  71. {
  72. if (EqualsIgnoreCasing(p.Name, propertyName))
  73. {
  74. property = p;
  75. break;
  76. }
  77. }
  78. if (property != null)
  79. {
  80. return (engine, target) => new PropertyInfoDescriptor(engine, property, target);
  81. }
  82. // look for a field
  83. FieldInfo field = null;
  84. foreach (var f in type.GetFields(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public))
  85. {
  86. if (EqualsIgnoreCasing(f.Name, propertyName))
  87. {
  88. field = f;
  89. break;
  90. }
  91. }
  92. if (field != null)
  93. {
  94. return (engine, target) => new FieldInfoDescriptor(engine, field, target);
  95. }
  96. // if no properties were found then look for a method
  97. List<MethodInfo> methods = null;
  98. foreach (var m in type.GetMethods(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public))
  99. {
  100. if (EqualsIgnoreCasing(m.Name, propertyName))
  101. {
  102. methods = methods ?? new List<MethodInfo>();
  103. methods.Add(m);
  104. }
  105. }
  106. if (methods?.Count > 0)
  107. {
  108. return (engine, target) => new PropertyDescriptor(new MethodInfoFunctionInstance(engine, methods.ToArray()), PropertyFlag.OnlyEnumerable);
  109. }
  110. }
  111. // if no methods are found check if target implemented indexing
  112. PropertyInfo first = null;
  113. foreach (var p in type.GetProperties())
  114. {
  115. if (p.GetIndexParameters().Length != 0)
  116. {
  117. first = p;
  118. break;
  119. }
  120. }
  121. if (first != null)
  122. {
  123. return (engine, target) => new IndexDescriptor(engine, propertyName, target);
  124. }
  125. // try to find a single explicit property implementation
  126. List<PropertyInfo> list = null;
  127. foreach (Type iface in type.GetInterfaces())
  128. {
  129. foreach (var iprop in iface.GetProperties())
  130. {
  131. if (EqualsIgnoreCasing(iprop.Name, propertyName))
  132. {
  133. list = list ?? new List<PropertyInfo>();
  134. list.Add(iprop);
  135. }
  136. }
  137. }
  138. if (list?.Count == 1)
  139. {
  140. return (engine, target) => new PropertyInfoDescriptor(engine, list[0], target);
  141. }
  142. // try to find explicit method implementations
  143. List<MethodInfo> explicitMethods = null;
  144. foreach (Type iface in type.GetInterfaces())
  145. {
  146. foreach (var imethod in iface.GetMethods())
  147. {
  148. if (EqualsIgnoreCasing(imethod.Name, propertyName))
  149. {
  150. explicitMethods = explicitMethods ?? new List<MethodInfo>();
  151. explicitMethods.Add(imethod);
  152. }
  153. }
  154. }
  155. if (explicitMethods?.Count > 0)
  156. {
  157. return (engine, target) => new PropertyDescriptor(new MethodInfoFunctionInstance(engine, explicitMethods.ToArray()), PropertyFlag.OnlyEnumerable);
  158. }
  159. // try to find explicit indexer implementations
  160. List<PropertyInfo> explicitIndexers = null;
  161. foreach (Type iface in type.GetInterfaces())
  162. {
  163. foreach (var iprop in iface.GetProperties())
  164. {
  165. if (iprop.GetIndexParameters().Length != 0)
  166. {
  167. explicitIndexers = explicitIndexers ?? new List<PropertyInfo>();
  168. explicitIndexers.Add(iprop);
  169. }
  170. }
  171. }
  172. if (explicitIndexers?.Count == 1)
  173. {
  174. return (engine, target) => new IndexDescriptor(engine, explicitIndexers[0].DeclaringType, propertyName, target);
  175. }
  176. return (engine, target) => PropertyDescriptor.Undefined;
  177. }
  178. private static bool EqualsIgnoreCasing(string s1, string s2)
  179. {
  180. bool equals = false;
  181. if (s1.Length == s2.Length)
  182. {
  183. if (s1.Length > 0)
  184. {
  185. equals = char.ToLowerInvariant(s1[0]) == char.ToLowerInvariant(s2[0]);
  186. }
  187. if (equals && s1.Length > 1)
  188. {
  189. equals = s1.Substring(1) == s2.Substring(1);
  190. }
  191. }
  192. return equals;
  193. }
  194. }
  195. }