ObjectWrapper.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. // if no methods are found check if target implemented indexing
  79. if (type.GetProperties().Where(p => p.GetIndexParameters().Length != 0).FirstOrDefault() != null)
  80. {
  81. return new IndexDescriptor(Engine, propertyName, Target);
  82. }
  83. var interfaces = type.GetInterfaces();
  84. // try to find a single explicit property implementation
  85. var explicitProperties = (from iface in interfaces
  86. from iprop in iface.GetProperties()
  87. where propertyName.Equals(iprop.Name)
  88. select iprop).ToArray();
  89. if (explicitProperties.Length == 1)
  90. {
  91. var descriptor = new PropertyInfoDescriptor(Engine, explicitProperties[0], Target);
  92. Properties.Add(propertyName, descriptor);
  93. return descriptor;
  94. }
  95. // try to find explicit method implementations
  96. var explicitMethods = (from iface in interfaces
  97. from imethod in iface.GetMethods()
  98. where propertyName.Equals(imethod.Name)
  99. select imethod).ToArray();
  100. if (explicitMethods.Length > 0)
  101. {
  102. var descriptor = new PropertyDescriptor(new MethodInfoFunctionInstance(Engine, explicitMethods), false, true, false);
  103. Properties.Add(propertyName, descriptor);
  104. return descriptor;
  105. }
  106. // try to find explicit indexer implementations
  107. var explicitIndexers =
  108. (from iface in interfaces
  109. from iprop in iface.GetProperties()
  110. where iprop.GetIndexParameters().Length != 0
  111. select iprop).ToArray();
  112. if (explicitIndexers.Length == 1)
  113. {
  114. return new IndexDescriptor(Engine, explicitIndexers[0].DeclaringType, propertyName, Target);
  115. }
  116. return PropertyDescriptor.Undefined;
  117. }
  118. }
  119. }