ArrayOperations.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 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. // there are some evil tests that iterate a lot with unshift..
  126. if (_target.Properties == null)
  127. {
  128. return 0;
  129. }
  130. var min = length;
  131. foreach (var entry in _target.Properties)
  132. {
  133. if (ulong.TryParse(entry.Key.ToString(), out var index))
  134. {
  135. min = System.Math.Min(index, min);
  136. }
  137. }
  138. if (_target.Prototype?.Properties != null)
  139. {
  140. foreach (var entry in _target.Prototype.Properties)
  141. {
  142. if (ulong.TryParse(entry.Key.ToString(), out var index))
  143. {
  144. min = System.Math.Min(index, min);
  145. }
  146. }
  147. }
  148. return min;
  149. }
  150. public override uint GetLength()
  151. {
  152. var integerLength = GetIntegerLength();
  153. return (uint) (integerLength >= 0 ? integerLength : 0);
  154. }
  155. public override ulong GetLongLength()
  156. {
  157. var integerLength = GetIntegerLength();
  158. if (integerLength <= 0)
  159. {
  160. return 0;
  161. }
  162. return (ulong) System.Math.Min(integerLength, MaxArrayLikeLength);
  163. }
  164. public override void SetLength(ulong length)
  165. => _target.Set(CommonProperties.Length, length, true);
  166. public override void EnsureCapacity(ulong capacity)
  167. {
  168. }
  169. public override JsValue Get(ulong index)
  170. => _target.Get(JsString.Create(index), _target);
  171. public override bool TryGetValue(ulong index, out JsValue value)
  172. {
  173. var propertyName = JsString.Create(index);
  174. var property = _target.GetProperty(propertyName);
  175. var kPresent = property != PropertyDescriptor.Undefined;
  176. value = kPresent ? _target.UnwrapJsValue(property) : JsValue.Undefined;
  177. return kPresent;
  178. }
  179. public override void CreateDataPropertyOrThrow(ulong index, JsValue value)
  180. => _target.CreateDataPropertyOrThrow(JsString.Create(index), value);
  181. public override void Set(ulong index, JsValue value, bool updateLength = false, bool throwOnError = true)
  182. => _target.Set(JsString.Create(index), value, throwOnError);
  183. public override void DeletePropertyOrThrow(ulong index)
  184. => _target.DeletePropertyOrThrow(JsString.Create(index));
  185. }
  186. private sealed class ArrayInstanceOperations : ArrayOperations<ArrayInstance>
  187. {
  188. public ArrayInstanceOperations(ArrayInstance target) : base(target)
  189. {
  190. }
  191. public override ulong GetSmallestIndex(ulong length)
  192. => _target.GetSmallestIndex();
  193. public override uint GetLength()
  194. => (uint) ((JsNumber) _target._length._value)._value;
  195. public override ulong GetLongLength()
  196. => (ulong) ((JsNumber) _target._length._value)._value;
  197. public override void SetLength(ulong length)
  198. => _target.Set(CommonProperties.Length, length, true);
  199. public override void EnsureCapacity(ulong capacity)
  200. => _target.EnsureCapacity((uint) capacity);
  201. public override bool TryGetValue(ulong index, out JsValue value)
  202. // array max size is uint
  203. => _target.TryGetValue((uint) index, out value);
  204. public override JsValue Get(ulong index) => _target.Get((uint) index);
  205. public override JsValue[] GetAll(Types elementTypes)
  206. {
  207. var n = _target.GetLength();
  208. if (_target._dense == null || _target._dense.Length < n)
  209. {
  210. return base.GetAll(elementTypes);
  211. }
  212. // optimized
  213. var jsValues = new JsValue[n];
  214. for (uint i = 0; i < (uint) jsValues.Length; i++)
  215. {
  216. var prop = _target._dense[i] ?? PropertyDescriptor.Undefined;
  217. if (prop == PropertyDescriptor.Undefined)
  218. {
  219. prop = _target.Prototype?.GetProperty(i) ?? PropertyDescriptor.Undefined;
  220. }
  221. var jsValue = _target.UnwrapJsValue(prop);
  222. if ((jsValue.Type & elementTypes) == 0)
  223. {
  224. ExceptionHelper.ThrowTypeErrorNoEngine("invalid type");
  225. }
  226. jsValues[i] = jsValue;
  227. }
  228. return jsValues;
  229. }
  230. public override void DeletePropertyOrThrow(ulong index)
  231. => _target.DeletePropertyOrThrow((uint) index);
  232. public override void CreateDataPropertyOrThrow(ulong index, JsValue value)
  233. => _target.SetIndexValue((uint) index, value, updateLength: false);
  234. public override void Set(ulong index, JsValue value, bool updateLength = false, bool throwOnError = true)
  235. => _target.SetIndexValue((uint) index, value, updateLength);
  236. }
  237. private sealed class TypedArrayInstanceOperations : ArrayOperations
  238. {
  239. private readonly TypedArrayInstance _target;
  240. public TypedArrayInstanceOperations(TypedArrayInstance target)
  241. {
  242. _target = target;
  243. }
  244. public override ObjectInstance Target => _target;
  245. public override ulong GetSmallestIndex(ulong length) => 0;
  246. public override uint GetLength()
  247. {
  248. if (!_target.IsConcatSpreadable)
  249. {
  250. return _target.Length;
  251. }
  252. var descValue = _target.Get(CommonProperties.Length, _target);
  253. if (!ReferenceEquals(descValue, null))
  254. {
  255. return (uint) TypeConverter.ToInteger(descValue);
  256. }
  257. return 0;
  258. }
  259. public override ulong GetLongLength() => GetLength();
  260. public override void SetLength(ulong length)
  261. {
  262. }
  263. public override void EnsureCapacity(ulong capacity)
  264. {
  265. }
  266. public override JsValue Get(ulong index) => _target[(int) index];
  267. public override bool TryGetValue(ulong index, out JsValue value)
  268. {
  269. if (index < _target.Length)
  270. {
  271. value = _target[(int) index];
  272. return true;
  273. }
  274. value = JsValue.Undefined;
  275. return false;
  276. }
  277. public override void CreateDataPropertyOrThrow(ulong index, JsValue value)
  278. => _target.CreateDataPropertyOrThrow(index, value);
  279. public override void Set(ulong index, JsValue value, bool updateLength = false, bool throwOnError = true)
  280. => _target[(int) index] = value;
  281. public override void DeletePropertyOrThrow(ulong index)
  282. => _target.DeletePropertyOrThrow(index);
  283. }
  284. }
  285. /// <summary>
  286. /// Adapter to use optimized array operations when possible.
  287. /// Gaps the difference between ArgumentsInstance and ArrayInstance.
  288. /// </summary>
  289. internal abstract class ArrayOperations<T> : ArrayOperations where T : ObjectInstance
  290. {
  291. protected readonly T _target;
  292. protected ArrayOperations(T target)
  293. {
  294. _target = target;
  295. }
  296. public override ObjectInstance Target => _target;
  297. }
  298. }