ObjectWrapper.Specialized.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 sealed 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 sealed 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 sealed 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 sealed override bool Set(JsValue property, JsValue value, JsValue receiver)
  76. {
  77. if (ReferenceEquals(receiver, this) && CommonProperties.Length.Equals(property))
  78. {
  79. if (!CanWrite)
  80. {
  81. return false;
  82. }
  83. if (value.IsInteger())
  84. {
  85. var length = value.AsInteger();
  86. if (length < 0)
  87. {
  88. Throw.RangeError(_engine.Realm, "Invalid array length");
  89. }
  90. if (length == Length)
  91. {
  92. return true;
  93. }
  94. if (length > Length)
  95. {
  96. EnsureCapacity(length);
  97. }
  98. else
  99. {
  100. // decrease the length, remove items
  101. for (var i = Length - 1; i >= length; i--)
  102. {
  103. RemoveAt(i);
  104. }
  105. }
  106. return true;
  107. }
  108. Throw.TypeError(_engine.Realm, "Invalid array length");
  109. }
  110. return base.Set(property, value, receiver);
  111. }
  112. protected virtual bool CanWrite => _engine.Options.Interop.AllowWrite;
  113. public void SetAt(int index, JsValue value)
  114. {
  115. if (_engine.Options.Interop.AllowWrite)
  116. {
  117. EnsureCapacity(index + 1);
  118. DoSetAt(index, ConvertToItemType(value));
  119. }
  120. }
  121. protected abstract void DoSetAt(int index, object? value);
  122. public abstract void AddDefault();
  123. public abstract void Add(JsValue value);
  124. public abstract void RemoveAt(int index);
  125. public virtual void EnsureCapacity(int capacity)
  126. {
  127. while (Length < capacity)
  128. {
  129. AddDefault();
  130. }
  131. }
  132. protected object? ConvertToItemType(JsValue value)
  133. {
  134. object? converted;
  135. if (ItemType == typeof(JsValue))
  136. {
  137. converted = value;
  138. }
  139. else if (!ReflectionExtensions.TryConvertViaTypeCoercion(ItemType, Engine.Options.Interop.ValueCoercion, value, out converted))
  140. {
  141. // attempt to convert the JsValue to the target type
  142. converted = value.ToObject();
  143. if (converted != null && converted.GetType() != ItemType)
  144. {
  145. converted = Engine.TypeConverter.Convert(converted, ItemType, CultureInfo.InvariantCulture);
  146. }
  147. }
  148. return converted;
  149. }
  150. }
  151. internal sealed class ListWrapper : ArrayLikeWrapper
  152. {
  153. private readonly IList? _list;
  154. internal ListWrapper(Engine engine, IList target, Type type)
  155. : base(engine, target, typeof(object), type)
  156. {
  157. _list = target;
  158. }
  159. public override int Length => _list?.Count ?? 0;
  160. public override object? GetAt(int index)
  161. {
  162. if (_list is not null && index >= 0 && index < _list.Count)
  163. {
  164. return _list[index];
  165. }
  166. return null;
  167. }
  168. protected override void DoSetAt(int index, object? value)
  169. {
  170. if (_list is not null)
  171. {
  172. _list[index] = value;
  173. }
  174. }
  175. public override void AddDefault() => _list?.Add(null);
  176. public override void Add(JsValue value) => _list?.Add(ConvertToItemType(value));
  177. public override void RemoveAt(int index) => _list?.RemoveAt(index);
  178. }
  179. internal class GenericListWrapper<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicFields)] T> : ArrayLikeWrapper
  180. {
  181. private readonly IList<T> _list;
  182. public GenericListWrapper(Engine engine, IList<T> target, Type? type)
  183. : base(engine, target, typeof(T), type)
  184. {
  185. _list = target;
  186. }
  187. public override int Length => _list.Count;
  188. public override object? GetAt(int index)
  189. {
  190. if (index >= 0 && index < _list.Count)
  191. {
  192. return _list[index];
  193. }
  194. return null;
  195. }
  196. protected override void DoSetAt(int index, object? value) => _list[index] = (T) value!;
  197. public override void AddDefault() => _list.Add(default!);
  198. public override void Add(JsValue value)
  199. {
  200. var converted = ConvertToItemType(value);
  201. _list.Add((T) converted!);
  202. }
  203. public override void RemoveAt(int index) => _list.RemoveAt(index);
  204. }
  205. internal sealed class ReadOnlyListWrapper<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicFields)] T> : ArrayLikeWrapper
  206. {
  207. private readonly IReadOnlyList<T> _list;
  208. public ReadOnlyListWrapper(Engine engine, IReadOnlyList<T> target, Type type) : base(engine, target, typeof(T), type)
  209. {
  210. _list = target;
  211. }
  212. public override int Length => _list.Count;
  213. public override object? GetAt(int index)
  214. {
  215. if (index >= 0 && index < _list.Count)
  216. {
  217. return _list[index];
  218. }
  219. return null;
  220. }
  221. protected override bool CanWrite => false;
  222. public override void AddDefault() => Throw.NotSupportedException();
  223. protected override void DoSetAt(int index, object? value) => Throw.NotSupportedException();
  224. public override void Add(JsValue value) => Throw.NotSupportedException();
  225. public override void RemoveAt(int index) => Throw.NotSupportedException();
  226. public override void EnsureCapacity(int capacity) => Throw.NotSupportedException();
  227. }