ObjectWrapper .cs 4.7 KB

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