ArrayOperations.cs 11 KB

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