ObjectWrapper .cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. using System.Collections.Generic;
  10. namespace Jint.Runtime.Interop
  11. {
  12. /// <summary>
  13. /// Wraps a CLR instance
  14. /// </summary>
  15. public sealed class ObjectWrapper : ObjectInstance, IObjectWrapper
  16. {
  17. public Object Target { get; set; }
  18. /// <summary>
  19. /// A private instance to cache methods
  20. /// </summary>
  21. private Dictionary<string, Delegate> DelegateCache = new Dictionary<string, Delegate>();
  22. public ObjectWrapper(Engine engine, Object obj)
  23. : base(engine)
  24. {
  25. Target = obj;
  26. }
  27. public override void Put(string propertyName, JsValue value, bool throwOnError)
  28. {
  29. if (!CanPut(propertyName))
  30. {
  31. if (throwOnError)
  32. {
  33. throw new JavaScriptException(Engine.TypeError);
  34. }
  35. return;
  36. }
  37. var ownDesc = GetOwnProperty(propertyName);
  38. if (ownDesc == null)
  39. {
  40. if (throwOnError)
  41. {
  42. throw new JavaScriptException(Engine.TypeError, "Unknown member: " + propertyName);
  43. }
  44. else
  45. {
  46. return;
  47. }
  48. }
  49. ownDesc.Value = value;
  50. }
  51. public override PropertyDescriptor GetOwnProperty(string propertyName)
  52. {
  53. PropertyDescriptor x;
  54. if (Properties.TryGetValue(propertyName, out x))
  55. return x;
  56. var type = Target.GetType();
  57. // look for a property
  58. var property = type.GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public);
  59. if (property != null)
  60. {
  61. var descriptor = new PropertyInfoDescriptor(Engine, property, Target);
  62. Properties.Add(propertyName, descriptor);
  63. return descriptor;
  64. }
  65. // look for a field
  66. var field = type.GetField(propertyName, BindingFlags.Instance | BindingFlags.Public);
  67. if (field != null)
  68. {
  69. var descriptor = new FieldInfoDescriptor(Engine, field, Target);
  70. Properties.Add(propertyName, descriptor);
  71. return descriptor;
  72. }
  73. // if no properties were found then look for a method
  74. var methods = type.GetMethods(BindingFlags.Instance | BindingFlags.Public)
  75. .Where(m => m.Name == propertyName)
  76. .ToArray();
  77. if (methods.Any())
  78. {
  79. var descriptor = new PropertyDescriptor(new MethodInfoFunctionInstance(Engine, methods), false, true, false);
  80. Properties.Add(propertyName, descriptor);
  81. return descriptor;
  82. }
  83. // if no methods are found check if target implemented indexing
  84. if (type.GetProperties().Where(p => p.GetIndexParameters().Length != 0).FirstOrDefault() != null)
  85. {
  86. return new IndexDescriptor(Engine, propertyName, Target);
  87. }
  88. var interfaces = type.GetInterfaces();
  89. // try to find a single explicit property implementation
  90. var explicitProperties = (from iface in interfaces
  91. from iprop in iface.GetProperties()
  92. where propertyName.Equals(iprop.Name)
  93. select iprop).ToArray();
  94. if (explicitProperties.Length == 1)
  95. {
  96. var descriptor = new PropertyInfoDescriptor(Engine, explicitProperties[0], Target);
  97. Properties.Add(propertyName, descriptor);
  98. return descriptor;
  99. }
  100. // try to find explicit method implementations
  101. var explicitMethods = (from iface in interfaces
  102. from imethod in iface.GetMethods()
  103. where propertyName.Equals(imethod.Name)
  104. select imethod).ToArray();
  105. if (explicitMethods.Length > 0)
  106. {
  107. var descriptor = new PropertyDescriptor(new MethodInfoFunctionInstance(Engine, explicitMethods), false, true, false);
  108. Properties.Add(propertyName, descriptor);
  109. return descriptor;
  110. }
  111. // try to find explicit indexer implementations
  112. var explicitIndexers =
  113. (from iface in interfaces
  114. from iprop in iface.GetProperties()
  115. where iprop.GetIndexParameters().Length != 0
  116. select iprop).ToArray();
  117. if (explicitIndexers.Length == 1)
  118. {
  119. return new IndexDescriptor(Engine, explicitIndexers[0].DeclaringType, propertyName, Target);
  120. }
  121. return PropertyDescriptor.Undefined;
  122. }
  123. }
  124. }