ObjectWrapper.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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.GetProperties(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public)
  54. .Where(p => EqualsIgnoreCasing(p.Name, propertyName))
  55. .FirstOrDefault();
  56. if (property != null)
  57. {
  58. var descriptor = new PropertyInfoDescriptor(Engine, property, Target);
  59. Properties.Add(propertyName, descriptor);
  60. return descriptor;
  61. }
  62. // look for a field
  63. var field = type.GetFields(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public)
  64. .Where(f => EqualsIgnoreCasing(f.Name, propertyName))
  65. .FirstOrDefault();
  66. if (field != null)
  67. {
  68. var descriptor = new FieldInfoDescriptor(Engine, field, Target);
  69. Properties.Add(propertyName, descriptor);
  70. return descriptor;
  71. }
  72. // if no properties were found then look for a method
  73. var methods = type.GetMethods(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public)
  74. .Where(m => EqualsIgnoreCasing(m.Name, propertyName))
  75. .ToArray();
  76. if (methods.Any())
  77. {
  78. var descriptor = new PropertyDescriptor(new MethodInfoFunctionInstance(Engine, methods), false, true, false);
  79. Properties.Add(propertyName, descriptor);
  80. return descriptor;
  81. }
  82. // if no methods are found check if target implemented indexing
  83. if (type.GetProperties().Where(p => p.GetIndexParameters().Length != 0).FirstOrDefault() != null)
  84. {
  85. return new IndexDescriptor(Engine, propertyName, Target);
  86. }
  87. var interfaces = type.GetInterfaces();
  88. // try to find a single explicit property implementation
  89. var explicitProperties = (from iface in interfaces
  90. from iprop in iface.GetProperties()
  91. where EqualsIgnoreCasing(iprop.Name, propertyName)
  92. select iprop).ToArray();
  93. if (explicitProperties.Length == 1)
  94. {
  95. var descriptor = new PropertyInfoDescriptor(Engine, explicitProperties[0], Target);
  96. Properties.Add(propertyName, descriptor);
  97. return descriptor;
  98. }
  99. // try to find explicit method implementations
  100. var explicitMethods = (from iface in interfaces
  101. from imethod in iface.GetMethods()
  102. where EqualsIgnoreCasing(imethod.Name, propertyName)
  103. select imethod).ToArray();
  104. if (explicitMethods.Length > 0)
  105. {
  106. var descriptor = new PropertyDescriptor(new MethodInfoFunctionInstance(Engine, explicitMethods), false, true, false);
  107. Properties.Add(propertyName, descriptor);
  108. return descriptor;
  109. }
  110. // try to find explicit indexer implementations
  111. var explicitIndexers =
  112. (from iface in interfaces
  113. from iprop in iface.GetProperties()
  114. where iprop.GetIndexParameters().Length != 0
  115. select iprop).ToArray();
  116. if (explicitIndexers.Length == 1)
  117. {
  118. return new IndexDescriptor(Engine, explicitIndexers[0].DeclaringType, propertyName, Target);
  119. }
  120. return PropertyDescriptor.Undefined;
  121. }
  122. private bool EqualsIgnoreCasing(string s1, string s2)
  123. {
  124. bool equals = false;
  125. if (s1.Length == s2.Length)
  126. {
  127. if (s1.Length > 0 && s2.Length > 0)
  128. {
  129. equals = (s1.ToLower()[0] == s2.ToLower()[0]);
  130. }
  131. if (s1.Length > 1 && s2.Length > 1)
  132. {
  133. equals = equals && (s1.Substring(1) == s2.Substring(1));
  134. }
  135. }
  136. return equals;
  137. }
  138. }
  139. }