IndexDescriptor.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.Reflection;
  6. using Jint.Native;
  7. namespace Jint.Runtime.Descriptors.Specialized
  8. {
  9. internal sealed class IndexDescriptor : PropertyDescriptor
  10. {
  11. private readonly Engine _engine;
  12. private readonly object _key;
  13. private readonly object _target;
  14. private readonly PropertyInfo _indexer;
  15. private readonly MethodInfo _containsKey;
  16. private static readonly PropertyInfo _iListIndexer = typeof(IList).GetProperty("Item");
  17. internal IndexDescriptor(Engine engine, object target, PropertyInfo indexer, MethodInfo containsKey, object key)
  18. : base(PropertyFlag.Enumerable | PropertyFlag.CustomJsValue)
  19. {
  20. _engine = engine;
  21. _target = target;
  22. _indexer = indexer;
  23. _containsKey = containsKey;
  24. _key = key;
  25. Writable = engine.Options._IsClrWriteAllowed;
  26. }
  27. internal static bool TryFindIndexer(
  28. Engine engine,
  29. Type targetType,
  30. string propertyName,
  31. out Func<object, IndexDescriptor> factory)
  32. {
  33. var paramTypeArray = new Type[1];
  34. Func<object, IndexDescriptor> ComposeIndexerFactory(PropertyInfo candidate, Type paramType)
  35. {
  36. if (engine.ClrTypeConverter.TryConvert(propertyName, paramType, CultureInfo.InvariantCulture, out var key))
  37. {
  38. // the key can be converted for this indexer
  39. var indexerProperty = candidate;
  40. // get contains key method to avoid index exception being thrown in dictionaries
  41. paramTypeArray[0] = paramType;
  42. var containsKeyMethod = targetType.GetMethod(nameof(IDictionary<string,string>.ContainsKey), paramTypeArray);
  43. if (containsKeyMethod is null)
  44. {
  45. paramTypeArray[0] = typeof(object);
  46. containsKeyMethod = targetType.GetMethod(nameof(IDictionary.Contains), paramTypeArray);
  47. }
  48. return (target) => new IndexDescriptor(engine, target, indexerProperty, containsKeyMethod, key);
  49. }
  50. // the key type doesn't work for this indexer
  51. return null;
  52. }
  53. // default indexer wins
  54. if (typeof(IList).IsAssignableFrom(targetType))
  55. {
  56. factory = ComposeIndexerFactory(_iListIndexer, typeof(int));
  57. if (factory != null)
  58. {
  59. return true;
  60. }
  61. }
  62. // try to find first indexer having either public getter or setter with matching argument type
  63. foreach (var candidate in targetType.GetProperties())
  64. {
  65. var indexParameters = candidate.GetIndexParameters();
  66. if (indexParameters.Length != 1)
  67. {
  68. continue;
  69. }
  70. if (candidate.GetGetMethod() != null || candidate.GetSetMethod() != null)
  71. {
  72. var paramType = indexParameters[0].ParameterType;
  73. factory = ComposeIndexerFactory(candidate, paramType);
  74. if (factory != null)
  75. {
  76. return true;
  77. }
  78. }
  79. }
  80. factory = default;
  81. return false;
  82. }
  83. protected internal override JsValue CustomValue
  84. {
  85. get
  86. {
  87. var getter = _indexer.GetGetMethod();
  88. if (getter == null)
  89. {
  90. ExceptionHelper.ThrowInvalidOperationException("Indexer has no public getter.");
  91. }
  92. object[] parameters = { _key };
  93. if (_containsKey != null)
  94. {
  95. if ((_containsKey.Invoke(_target, parameters) as bool?) != true)
  96. {
  97. return JsValue.Undefined;
  98. }
  99. }
  100. try
  101. {
  102. return JsValue.FromObject(_engine, getter.Invoke(_target, parameters));
  103. }
  104. catch (TargetInvocationException tie)
  105. {
  106. switch (tie.InnerException)
  107. {
  108. case null:
  109. throw;
  110. case ArgumentOutOfRangeException _:
  111. case IndexOutOfRangeException _:
  112. case InvalidOperationException _:
  113. return JsValue.Undefined;
  114. default:
  115. throw tie.InnerException;
  116. }
  117. }
  118. }
  119. set
  120. {
  121. var setter = _indexer.GetSetMethod();
  122. if (setter == null)
  123. {
  124. ExceptionHelper.ThrowInvalidOperationException("Indexer has no public setter.");
  125. }
  126. var obj = value?.ToObject();
  127. // attempt to convert to expected type
  128. if (obj != null && obj.GetType() != _indexer.PropertyType)
  129. {
  130. obj = _engine.ClrTypeConverter.Convert(obj, _indexer.PropertyType, CultureInfo.InvariantCulture);
  131. }
  132. object[] parameters = { _key, obj };
  133. try
  134. {
  135. setter!.Invoke(_target, parameters);
  136. }
  137. catch (TargetInvocationException tie)
  138. {
  139. if (tie.InnerException != null)
  140. {
  141. throw tie.InnerException;
  142. }
  143. throw;
  144. }
  145. }
  146. }
  147. }
  148. }