ObjectWrapper.cs 5.6 KB

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