ObjectWrapper.cs 5.6 KB

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