ObjectWrapper.Specialized.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. var defaultValue = default(object);
  60. if (typeof(JsValue).IsAssignableFrom(ItemType))
  61. {
  62. defaultValue = JsValue.Undefined;
  63. }
  64. else if (ItemType.IsValueType)
  65. {
  66. defaultValue = Activator.CreateInstance(ItemType);
  67. }
  68. DoSetAt((int) value, defaultValue);
  69. return true;
  70. }
  71. }
  72. return base.Delete(property);
  73. }
  74. public abstract object? GetAt(int index);
  75. public void SetAt(int index, JsValue value)
  76. {
  77. if (_engine.Options.Interop.AllowWrite)
  78. {
  79. EnsureCapacity(index + 1);
  80. DoSetAt(index, ConvertToItemType(value));
  81. }
  82. }
  83. protected abstract void DoSetAt(int index, object? value);
  84. public abstract void AddDefault();
  85. public abstract void Add(JsValue value);
  86. public abstract void RemoveAt(int index);
  87. public virtual void EnsureCapacity(int capacity)
  88. {
  89. while (Length < capacity)
  90. {
  91. AddDefault();
  92. }
  93. }
  94. protected object? ConvertToItemType(JsValue value)
  95. {
  96. object? converted;
  97. if (ItemType == typeof(JsValue))
  98. {
  99. converted = value;
  100. }
  101. else if (!ReflectionExtensions.TryConvertViaTypeCoercion(ItemType, Engine.Options.Interop.ValueCoercion, value, out converted))
  102. {
  103. // attempt to convert the JsValue to the target type
  104. converted = value.ToObject();
  105. if (converted != null && converted.GetType() != ItemType)
  106. {
  107. converted = Engine.TypeConverter.Convert(converted, ItemType, CultureInfo.InvariantCulture);
  108. }
  109. }
  110. return converted;
  111. }
  112. }
  113. internal class ListWrapper : ArrayLikeWrapper
  114. {
  115. private readonly IList? _list;
  116. internal ListWrapper(Engine engine, IList target, Type type)
  117. : base(engine, target, typeof(object), type)
  118. {
  119. _list = target;
  120. }
  121. public override int Length => _list?.Count ?? 0;
  122. public override object? GetAt(int index)
  123. {
  124. if (_list is not null && index >= 0 && index < _list.Count)
  125. {
  126. return _list[index];
  127. }
  128. return null;
  129. }
  130. protected override void DoSetAt(int index, object? value)
  131. {
  132. if (_list is not null)
  133. {
  134. _list[index] = value;
  135. }
  136. }
  137. public override void AddDefault() => _list?.Add(null);
  138. public override void Add(JsValue value) => _list?.Add(ConvertToItemType(value));
  139. public override void RemoveAt(int index) => _list?.RemoveAt(index);
  140. }
  141. internal class GenericListWrapper<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicFields)] T> : ArrayLikeWrapper
  142. {
  143. private readonly IList<T> _list;
  144. public GenericListWrapper(Engine engine, IList<T> target, Type? type)
  145. : base(engine, target, typeof(T), type)
  146. {
  147. _list = target;
  148. }
  149. public override int Length => _list.Count;
  150. public override object? GetAt(int index)
  151. {
  152. if (index >= 0 && index < _list.Count)
  153. {
  154. return _list[index];
  155. }
  156. return null;
  157. }
  158. protected override void DoSetAt(int index, object? value) => _list[index] = (T) value!;
  159. public override void AddDefault() => _list.Add(default!);
  160. public override void Add(JsValue value)
  161. {
  162. var converted = ConvertToItemType(value);
  163. _list.Add((T) converted!);
  164. }
  165. public override void RemoveAt(int index) => _list.RemoveAt(index);
  166. }
  167. internal sealed class ReadOnlyListWrapper<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicFields)] T> : ArrayLikeWrapper
  168. {
  169. private readonly IReadOnlyList<T> _list;
  170. public ReadOnlyListWrapper(Engine engine, IReadOnlyList<T> target, Type type) : base(engine, target, typeof(T), type)
  171. {
  172. _list = target;
  173. }
  174. public override int Length => _list.Count;
  175. public override object? GetAt(int index)
  176. {
  177. if (index >= 0 && index < _list.Count)
  178. {
  179. return _list[index];
  180. }
  181. return null;
  182. }
  183. public override void AddDefault() => ExceptionHelper.ThrowNotSupportedException();
  184. protected override void DoSetAt(int index, object? value) => ExceptionHelper.ThrowNotSupportedException();
  185. public override void Add(JsValue value) => ExceptionHelper.ThrowNotSupportedException();
  186. public override void RemoveAt(int index) => ExceptionHelper.ThrowNotSupportedException();
  187. public override void EnsureCapacity(int capacity) => ExceptionHelper.ThrowNotSupportedException();
  188. }