ObjectWrapper.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using System;
  2. using System.Linq;
  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. using System.Collections;
  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 Object Target { get; set; }
  17. public ObjectWrapper(Engine engine, Object obj)
  18. : base(engine)
  19. {
  20. Target = obj;
  21. }
  22. public override void Put(string propertyName, JsValue value, bool throwOnError)
  23. {
  24. if (!CanPut(propertyName))
  25. {
  26. if (throwOnError)
  27. {
  28. throw new JavaScriptException(Engine.TypeError);
  29. }
  30. return;
  31. }
  32. var ownDesc = GetOwnProperty(propertyName);
  33. if (ownDesc == null)
  34. {
  35. if (throwOnError)
  36. {
  37. throw new JavaScriptException(Engine.TypeError, "Unknown member: " + propertyName);
  38. }
  39. else
  40. {
  41. return;
  42. }
  43. }
  44. ownDesc.Value = value;
  45. }
  46. public override PropertyDescriptor GetOwnProperty(string propertyName)
  47. {
  48. PropertyDescriptor x;
  49. if (Properties.TryGetValue(propertyName, out x))
  50. return x;
  51. var type = Target.GetType();
  52. // look for a property
  53. var property = type.GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public);
  54. if (property != null)
  55. {
  56. var descriptor = new PropertyInfoDescriptor(Engine, property, Target);
  57. Properties.Add(propertyName, descriptor);
  58. return descriptor;
  59. }
  60. // look for a field
  61. var field = type.GetField(propertyName, BindingFlags.Instance | BindingFlags.Public);
  62. if (field != null)
  63. {
  64. var descriptor = new FieldInfoDescriptor(Engine, field, Target);
  65. Properties.Add(propertyName, descriptor);
  66. return descriptor;
  67. }
  68. // if no properties were found then look for a method
  69. var methods = type.GetMethods(BindingFlags.Instance | BindingFlags.Public)
  70. .Where(m => m.Name == propertyName)
  71. .ToArray();
  72. if (methods.Any())
  73. {
  74. var descriptor = new PropertyDescriptor(new MethodInfoFunctionInstance(Engine, methods), false, true, false);
  75. Properties.Add(propertyName, descriptor);
  76. return descriptor;
  77. }
  78. var pascalCasedPropertyName = char.ToUpperInvariant(propertyName[0]).ToString();
  79. if (propertyName.Length > 1)
  80. {
  81. pascalCasedPropertyName += propertyName.Substring(1);
  82. }
  83. // look for methods using pascal cased name.
  84. var pascalCasedMethods = type.GetMethods(BindingFlags.Instance | BindingFlags.Public)
  85. .Where(m => m.Name == pascalCasedPropertyName)
  86. .ToArray();
  87. if (pascalCasedMethods.Any())
  88. {
  89. var descriptor = new PropertyDescriptor(new MethodInfoFunctionInstance(Engine, pascalCasedMethods), false, true, false);
  90. Properties.Add(propertyName, descriptor);
  91. return descriptor;
  92. }
  93. // if no methods are found check if target implemented indexing
  94. if (type.GetProperties().Where(p => p.GetIndexParameters().Length != 0).FirstOrDefault() != null)
  95. {
  96. return new IndexDescriptor(Engine, propertyName, Target);
  97. }
  98. var interfaces = type.GetInterfaces();
  99. // try to find a single explicit property implementation
  100. var explicitProperties = (from iface in interfaces
  101. from iprop in iface.GetProperties()
  102. where propertyName.Equals(iprop.Name)
  103. select iprop).ToArray();
  104. if (explicitProperties.Length == 1)
  105. {
  106. var descriptor = new PropertyInfoDescriptor(Engine, explicitProperties[0], Target);
  107. Properties.Add(propertyName, descriptor);
  108. return descriptor;
  109. }
  110. // try to find explicit method implementations
  111. var explicitMethods = (from iface in interfaces
  112. from imethod in iface.GetMethods()
  113. where propertyName.Equals(imethod.Name)
  114. select imethod).ToArray();
  115. if (explicitMethods.Length > 0)
  116. {
  117. var descriptor = new PropertyDescriptor(new MethodInfoFunctionInstance(Engine, explicitMethods), false, true, false);
  118. Properties.Add(propertyName, descriptor);
  119. return descriptor;
  120. }
  121. // try to find explicit method implementations using the pascal cased property name
  122. var explicitPascalCasedMethods = (from iface in interfaces
  123. from imethod in iface.GetMethods()
  124. where pascalCasedPropertyName.Equals(imethod.Name)
  125. select imethod).ToArray();
  126. if (explicitPascalCasedMethods.Length > 0)
  127. {
  128. var descriptor = new PropertyDescriptor(new MethodInfoFunctionInstance(Engine, explicitPascalCasedMethods), false, true, false);
  129. Properties.Add(propertyName, descriptor);
  130. return descriptor;
  131. }
  132. // try to find explicit indexer implementations
  133. var explicitIndexers =
  134. (from iface in interfaces
  135. from iprop in iface.GetProperties()
  136. where iprop.GetIndexParameters().Length != 0
  137. select iprop).ToArray();
  138. if (explicitIndexers.Length == 1)
  139. {
  140. return new IndexDescriptor(Engine, explicitIndexers[0].DeclaringType, propertyName, Target);
  141. }
  142. return PropertyDescriptor.Undefined;
  143. }
  144. }
  145. }