ObjectWrapper.Specialized.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. using System.Collections;
  2. using System.Diagnostics.CodeAnalysis;
  3. using System.Globalization;
  4. using Jint.Extensions;
  5. using Jint.Native;
  6. namespace Jint.Runtime.Interop;
  7. internal abstract class ArrayLikeWrapper : ObjectWrapper
  8. {
  9. protected ArrayLikeWrapper(
  10. Engine engine,
  11. object obj,
  12. Type itemType,
  13. Type? type = null) : base(engine, obj, type)
  14. {
  15. ItemType = itemType;
  16. if (engine.Options.Interop.AttachArrayPrototype)
  17. {
  18. Prototype = engine.Intrinsics.Array.PrototypeObject;
  19. }
  20. }
  21. [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicFields)]
  22. private Type ItemType { get; }
  23. public abstract int Length { get; }
  24. public override JsValue Get(JsValue property, JsValue receiver)
  25. {
  26. if (property.IsInteger())
  27. {
  28. return FromObject(_engine, GetAt(property.AsInteger()));
  29. }
  30. return base.Get(property, receiver);
  31. }
  32. public override bool HasProperty(JsValue property)
  33. {
  34. if (property.IsNumber())
  35. {
  36. var value = ((JsNumber) property)._value;
  37. if (TypeConverter.IsIntegralNumber(value))
  38. {
  39. var index = (int) value;
  40. if (Target is ICollection collection && index < collection.Count)
  41. {
  42. return true;
  43. }
  44. }
  45. }
  46. return base.HasProperty(property);
  47. }
  48. public override bool Delete(JsValue property)
  49. {
  50. if (!_engine.Options.Interop.AllowWrite)
  51. {
  52. return false;
  53. }
  54. if (property.IsNumber())
  55. {
  56. var value = ((JsNumber) property)._value;
  57. if (TypeConverter.IsIntegralNumber(value))
  58. {
  59. DoSetAt((int) value, default);
  60. return true;
  61. }
  62. }
  63. return base.Delete(property);
  64. }
  65. public abstract object? GetAt(int index);
  66. public void SetAt(int index, JsValue value)
  67. {
  68. if (_engine.Options.Interop.AllowWrite)
  69. {
  70. EnsureCapacity(index + 1);
  71. DoSetAt(index, ConvertToItemType(value));
  72. }
  73. }
  74. protected abstract void DoSetAt(int index, object? value);
  75. public abstract void AddDefault();
  76. public abstract void Add(JsValue value);
  77. public abstract void RemoveAt(int index);
  78. public virtual void EnsureCapacity(int capacity)
  79. {
  80. while (Length < capacity)
  81. {
  82. AddDefault();
  83. }
  84. }
  85. protected object? ConvertToItemType(JsValue value)
  86. {
  87. object? converted;
  88. if (ItemType == typeof(JsValue))
  89. {
  90. converted = value;
  91. }
  92. else if (!ReflectionExtensions.TryConvertViaTypeCoercion(ItemType, Engine.Options.Interop.ValueCoercion, value, out converted))
  93. {
  94. // attempt to convert the JsValue to the target type
  95. converted = value.ToObject();
  96. if (converted != null && converted.GetType() != ItemType)
  97. {
  98. converted = Engine.TypeConverter.Convert(converted, ItemType, CultureInfo.InvariantCulture);
  99. }
  100. }
  101. return converted;
  102. }
  103. }
  104. internal class ListWrapper : ArrayLikeWrapper
  105. {
  106. private readonly IList? _list;
  107. internal ListWrapper(Engine engine, IList target, Type type)
  108. : base(engine, target, typeof(object), type)
  109. {
  110. _list = target;
  111. }
  112. public override int Length => _list?.Count ?? 0;
  113. public override object? GetAt(int index)
  114. {
  115. if (_list is not null && index >= 0 && index < _list.Count)
  116. {
  117. return _list[index];
  118. }
  119. return null;
  120. }
  121. protected override void DoSetAt(int index, object? value)
  122. {
  123. if (_list is not null)
  124. {
  125. _list[index] = value;
  126. }
  127. }
  128. public override void AddDefault() => _list?.Add(null);
  129. public override void Add(JsValue value) => _list?.Add(ConvertToItemType(value));
  130. public override void RemoveAt(int index) => _list?.RemoveAt(index);
  131. }
  132. internal class GenericListWrapper<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicFields)] T> : ArrayLikeWrapper
  133. {
  134. private readonly IList<T> _list;
  135. public GenericListWrapper(Engine engine, IList<T> target, Type? type)
  136. : base(engine, target, typeof(T), type)
  137. {
  138. _list = target;
  139. }
  140. public override int Length => _list.Count;
  141. public override object? GetAt(int index)
  142. {
  143. if (index >= 0 && index < _list.Count)
  144. {
  145. return _list[index];
  146. }
  147. return null;
  148. }
  149. protected override void DoSetAt(int index, object? value) => _list[index] = (T) value!;
  150. public override void AddDefault() => _list.Add(default!);
  151. public override void Add(JsValue value)
  152. {
  153. var converted = ConvertToItemType(value);
  154. _list.Add((T) converted!);
  155. }
  156. public override void RemoveAt(int index) => _list.RemoveAt(index);
  157. }
  158. internal sealed class ReadOnlyListWrapper<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicFields)] T> : ArrayLikeWrapper
  159. {
  160. private readonly IReadOnlyList<T> _list;
  161. public ReadOnlyListWrapper(Engine engine, IReadOnlyList<T> target, Type type) : base(engine, target, typeof(T), type)
  162. {
  163. _list = target;
  164. }
  165. public override int Length => _list.Count;
  166. public override object? GetAt(int index)
  167. {
  168. if (index >= 0 && index < _list.Count)
  169. {
  170. return _list[index];
  171. }
  172. return null;
  173. }
  174. public override void AddDefault() => ExceptionHelper.ThrowNotSupportedException();
  175. protected override void DoSetAt(int index, object? value) => ExceptionHelper.ThrowNotSupportedException();
  176. public override void Add(JsValue value) => ExceptionHelper.ThrowNotSupportedException();
  177. public override void RemoveAt(int index) => ExceptionHelper.ThrowNotSupportedException();
  178. public override void EnsureCapacity(int capacity) => ExceptionHelper.ThrowNotSupportedException();
  179. }