ObjectWrapper.cs 7.9 KB

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