ArrayOperations.cs 12 KB

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