ArrayOperations.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using Jint.Native.Number;
  4. using Jint.Native.Object;
  5. using Jint.Native.TypedArray;
  6. using Jint.Runtime;
  7. using Jint.Runtime.Descriptors;
  8. namespace Jint.Native.Array
  9. {
  10. internal abstract class ArrayOperations : IEnumerable<JsValue>
  11. {
  12. protected internal const ulong MaxArrayLength = 4294967295;
  13. protected internal const ulong MaxArrayLikeLength = NumberConstructor.MaxSafeInteger;
  14. public static ArrayOperations For(ObjectInstance instance)
  15. {
  16. if (instance is ArrayInstance { CanUseFastAccess: true } arrayInstance)
  17. {
  18. return new ArrayInstanceOperations(arrayInstance);
  19. }
  20. if (instance is TypedArrayInstance typedArrayInstance)
  21. {
  22. return new TypedArrayInstanceOperations(typedArrayInstance);
  23. }
  24. return new ObjectInstanceOperations(instance);
  25. }
  26. public static ArrayOperations For(Realm realm, JsValue thisObj)
  27. {
  28. var instance = TypeConverter.ToObject(realm, thisObj);
  29. return For(instance);
  30. }
  31. public abstract ObjectInstance Target { get; }
  32. public abstract ulong GetSmallestIndex(ulong length);
  33. public abstract uint GetLength();
  34. public abstract ulong GetLongLength();
  35. public abstract void SetLength(ulong length);
  36. public abstract void EnsureCapacity(ulong capacity);
  37. public abstract JsValue Get(ulong index);
  38. public virtual JsValue[] GetAll(Types elementTypes)
  39. {
  40. var n = (int) GetLength();
  41. var jsValues = new JsValue[n];
  42. for (uint i = 0; i < (uint) jsValues.Length; i++)
  43. {
  44. var jsValue = Get(i);
  45. if ((jsValue.Type & elementTypes) == 0)
  46. {
  47. ExceptionHelper.ThrowTypeErrorNoEngine("invalid type");
  48. }
  49. jsValues[i] = jsValue;
  50. }
  51. return jsValues;
  52. }
  53. public abstract bool TryGetValue(ulong index, out JsValue value);
  54. public bool HasProperty(ulong index) => Target.HasProperty(index);
  55. public abstract void CreateDataPropertyOrThrow(ulong index, JsValue value);
  56. public abstract void Set(ulong index, JsValue value, bool updateLength = false, bool throwOnError = true);
  57. public abstract void DeletePropertyOrThrow(ulong index);
  58. internal ArrayLikeIterator GetEnumerator() => new ArrayLikeIterator(this);
  59. IEnumerator<JsValue> IEnumerable<JsValue>.GetEnumerator() => new ArrayLikeIterator(this);
  60. IEnumerator IEnumerable.GetEnumerator() => new ArrayLikeIterator(this);
  61. internal sealed class ArrayLikeIterator : IEnumerator<JsValue>
  62. {
  63. private readonly ArrayOperations _obj;
  64. private ulong _current;
  65. private bool _initialized;
  66. private readonly uint _length;
  67. public ArrayLikeIterator(ArrayOperations obj)
  68. {
  69. _obj = obj;
  70. _length = obj.GetLength();
  71. Reset();
  72. }
  73. public JsValue Current
  74. {
  75. get
  76. {
  77. if (!_initialized)
  78. {
  79. return null;
  80. }
  81. else
  82. {
  83. return _obj.TryGetValue(_current, out var temp) ? temp : null;
  84. }
  85. }
  86. }
  87. object IEnumerator.Current => Current;
  88. public void Dispose()
  89. {
  90. }
  91. public bool MoveNext()
  92. {
  93. if (!_initialized)
  94. {
  95. _initialized = true;
  96. }
  97. else
  98. {
  99. _current++;
  100. }
  101. return _current < _length;
  102. }
  103. public void Reset()
  104. {
  105. _initialized = false;
  106. _current = 0;
  107. }
  108. }
  109. private sealed class ObjectInstanceOperations : ArrayOperations<ObjectInstance>
  110. {
  111. public ObjectInstanceOperations(ObjectInstance target) : base(target)
  112. {
  113. }
  114. private double GetIntegerLength()
  115. {
  116. var descValue = _target.Get(CommonProperties.Length, _target);
  117. if (!ReferenceEquals(descValue, null))
  118. {
  119. return TypeConverter.ToInteger(descValue);
  120. }
  121. return 0;
  122. }
  123. public override ulong GetSmallestIndex(ulong length)
  124. {
  125. return _target.GetSmallestIndex(length);
  126. }
  127. public override uint GetLength()
  128. {
  129. var integerLength = GetIntegerLength();
  130. return (uint) (integerLength >= 0 ? integerLength : 0);
  131. }
  132. public override ulong GetLongLength()
  133. {
  134. var integerLength = GetIntegerLength();
  135. if (integerLength <= 0)
  136. {
  137. return 0;
  138. }
  139. return (ulong) System.Math.Min(integerLength, MaxArrayLikeLength);
  140. }
  141. public override void SetLength(ulong length)
  142. => _target.Set(CommonProperties.Length, length, true);
  143. public override void EnsureCapacity(ulong capacity)
  144. {
  145. }
  146. public override JsValue Get(ulong index)
  147. => _target.Get(JsString.Create(index), _target);
  148. public override bool TryGetValue(ulong index, out JsValue value)
  149. {
  150. var propertyName = JsString.Create(index);
  151. var property = _target.GetProperty(propertyName);
  152. var kPresent = property != PropertyDescriptor.Undefined;
  153. value = kPresent ? _target.UnwrapJsValue(property) : JsValue.Undefined;
  154. return kPresent;
  155. }
  156. public override void CreateDataPropertyOrThrow(ulong index, JsValue value)
  157. => _target.CreateDataPropertyOrThrow(JsString.Create(index), value);
  158. public override void Set(ulong index, JsValue value, bool updateLength = false, bool throwOnError = true)
  159. => _target.Set(JsString.Create(index), value, throwOnError);
  160. public override void DeletePropertyOrThrow(ulong index)
  161. => _target.DeletePropertyOrThrow(JsString.Create(index));
  162. }
  163. private sealed class ArrayInstanceOperations : ArrayOperations<ArrayInstance>
  164. {
  165. public ArrayInstanceOperations(ArrayInstance target) : base(target)
  166. {
  167. }
  168. public override ulong GetSmallestIndex(ulong length)
  169. => _target.GetSmallestIndex();
  170. public override uint GetLength()
  171. => (uint) ((JsNumber) _target._length._value)._value;
  172. public override ulong GetLongLength()
  173. => (ulong) ((JsNumber) _target._length._value)._value;
  174. public override void SetLength(ulong length)
  175. => _target.Set(CommonProperties.Length, length, true);
  176. public override void EnsureCapacity(ulong capacity)
  177. => _target.EnsureCapacity((uint) capacity);
  178. public override bool TryGetValue(ulong index, out JsValue value)
  179. // array max size is uint
  180. => _target.TryGetValue((uint) index, out value);
  181. public override JsValue Get(ulong index) => _target.Get((uint) index);
  182. public override JsValue[] GetAll(Types elementTypes)
  183. {
  184. var n = _target.GetLength();
  185. if (_target._dense == null || _target._dense.Length < n)
  186. {
  187. return base.GetAll(elementTypes);
  188. }
  189. // optimized
  190. var jsValues = new JsValue[n];
  191. for (uint i = 0; i < (uint) jsValues.Length; i++)
  192. {
  193. var prop = _target._dense[i] ?? PropertyDescriptor.Undefined;
  194. if (prop == PropertyDescriptor.Undefined)
  195. {
  196. prop = _target.Prototype?.GetProperty(i) ?? PropertyDescriptor.Undefined;
  197. }
  198. var jsValue = _target.UnwrapJsValue(prop);
  199. if ((jsValue.Type & elementTypes) == 0)
  200. {
  201. ExceptionHelper.ThrowTypeErrorNoEngine("invalid type");
  202. }
  203. jsValues[i] = jsValue;
  204. }
  205. return jsValues;
  206. }
  207. public override void DeletePropertyOrThrow(ulong index)
  208. => _target.DeletePropertyOrThrow((uint) index);
  209. public override void CreateDataPropertyOrThrow(ulong index, JsValue value)
  210. => _target.SetIndexValue((uint) index, value, updateLength: false);
  211. public override void Set(ulong index, JsValue value, bool updateLength = false, bool throwOnError = true)
  212. => _target.SetIndexValue((uint) index, value, updateLength);
  213. }
  214. private sealed class TypedArrayInstanceOperations : ArrayOperations
  215. {
  216. private readonly TypedArrayInstance _target;
  217. public TypedArrayInstanceOperations(TypedArrayInstance target)
  218. {
  219. _target = target;
  220. }
  221. public override ObjectInstance Target => _target;
  222. public override ulong GetSmallestIndex(ulong length) => 0;
  223. public override uint GetLength()
  224. {
  225. if (!_target.IsConcatSpreadable)
  226. {
  227. return _target.Length;
  228. }
  229. var descValue = _target.Get(CommonProperties.Length, _target);
  230. if (!ReferenceEquals(descValue, null))
  231. {
  232. return (uint) TypeConverter.ToInteger(descValue);
  233. }
  234. return 0;
  235. }
  236. public override ulong GetLongLength() => GetLength();
  237. public override void SetLength(ulong length)
  238. {
  239. }
  240. public override void EnsureCapacity(ulong capacity)
  241. {
  242. }
  243. public override JsValue Get(ulong index) => _target[(int) index];
  244. public override bool TryGetValue(ulong index, out JsValue value)
  245. {
  246. if (index < _target.Length)
  247. {
  248. value = _target[(int) index];
  249. return true;
  250. }
  251. value = JsValue.Undefined;
  252. return false;
  253. }
  254. public override void CreateDataPropertyOrThrow(ulong index, JsValue value)
  255. => _target.CreateDataPropertyOrThrow(index, value);
  256. public override void Set(ulong index, JsValue value, bool updateLength = false, bool throwOnError = true)
  257. => _target[(int) index] = value;
  258. public override void DeletePropertyOrThrow(ulong index)
  259. => _target.DeletePropertyOrThrow(index);
  260. }
  261. }
  262. /// <summary>
  263. /// Adapter to use optimized array operations when possible.
  264. /// Gaps the difference between ArgumentsInstance and ArrayInstance.
  265. /// </summary>
  266. internal abstract class ArrayOperations<T> : ArrayOperations where T : ObjectInstance
  267. {
  268. protected readonly T _target;
  269. protected ArrayOperations(T target)
  270. {
  271. _target = target;
  272. }
  273. public override ObjectInstance Target => _target;
  274. }
  275. }