ArrayInstance.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. using System.Collections.Generic;
  2. using System.Threading;
  3. using Jint.Native.Object;
  4. using Jint.Runtime;
  5. using Jint.Runtime.Descriptors;
  6. namespace Jint.Native.Array
  7. {
  8. public class ArrayInstance : ObjectInstance
  9. {
  10. // cache key container for array iteration for less allocations
  11. private static readonly ThreadLocal<List<uint>> keyCache = new ThreadLocal<List<uint>>(() => new List<uint>());
  12. private readonly Engine _engine;
  13. private readonly Dictionary<uint, PropertyDescriptor> _array = new Dictionary<uint, PropertyDescriptor>();
  14. private PropertyDescriptor _length;
  15. public ArrayInstance(Engine engine) : base(engine)
  16. {
  17. _engine = engine;
  18. }
  19. public override string Class => "Array";
  20. /// Implementation from ObjectInstance official specs as the one
  21. /// in ObjectInstance is optimized for the general case and wouldn't work
  22. /// for arrays
  23. public override void Put(string propertyName, JsValue value, bool throwOnError)
  24. {
  25. if (!CanPut(propertyName))
  26. {
  27. if (throwOnError)
  28. {
  29. throw new JavaScriptException(Engine.TypeError);
  30. }
  31. return;
  32. }
  33. var ownDesc = GetOwnProperty(propertyName);
  34. if (ownDesc.IsDataDescriptor())
  35. {
  36. var valueDesc = new PropertyDescriptor(value: value, writable: null, enumerable: null, configurable: null);
  37. DefineOwnProperty(propertyName, valueDesc, throwOnError);
  38. return;
  39. }
  40. // property is an accessor or inherited
  41. var desc = GetProperty(propertyName);
  42. if (desc.IsAccessorDescriptor())
  43. {
  44. var setter = desc.Set.TryCast<ICallable>();
  45. setter.Call(JsValue, new[] { value });
  46. }
  47. else
  48. {
  49. var newDesc = new PropertyDescriptor(value, true, true, true);
  50. DefineOwnProperty(propertyName, newDesc, throwOnError);
  51. }
  52. }
  53. public override bool DefineOwnProperty(string propertyName, PropertyDescriptor desc, bool throwOnError)
  54. {
  55. var oldLenDesc = GetOwnProperty("length");
  56. var oldLen = (uint)TypeConverter.ToNumber(oldLenDesc.Value);
  57. uint index;
  58. if (propertyName == "length")
  59. {
  60. if (desc.Value == null)
  61. {
  62. return base.DefineOwnProperty("length", desc, throwOnError);
  63. }
  64. var newLenDesc = new PropertyDescriptor(desc);
  65. uint newLen = TypeConverter.ToUint32(desc.Value);
  66. if (newLen != TypeConverter.ToNumber(desc.Value))
  67. {
  68. throw new JavaScriptException(_engine.RangeError);
  69. }
  70. newLenDesc.Value = newLen;
  71. if (newLen >= oldLen)
  72. {
  73. return base.DefineOwnProperty("length", _length = newLenDesc, throwOnError);
  74. }
  75. if (!oldLenDesc.Writable.Value)
  76. {
  77. if (throwOnError)
  78. {
  79. throw new JavaScriptException(_engine.TypeError);
  80. }
  81. return false;
  82. }
  83. bool newWritable;
  84. if (!newLenDesc.Writable.HasValue || newLenDesc.Writable.Value)
  85. {
  86. newWritable = true;
  87. }
  88. else
  89. {
  90. newWritable = false;
  91. newLenDesc.Writable = true;
  92. }
  93. var succeeded = base.DefineOwnProperty("length", _length = newLenDesc, throwOnError);
  94. if (!succeeded)
  95. {
  96. return false;
  97. }
  98. // in the case of sparse arrays, treat each concrete element instead of
  99. // iterating over all indexes
  100. if (_array.Count < oldLen - newLen)
  101. {
  102. var keys = keyCache.Value;
  103. keys.Clear();
  104. keys.AddRange(_array.Keys);
  105. foreach (var keyIndex in keys)
  106. {
  107. // is it the index of the array
  108. if (keyIndex >= newLen && keyIndex < oldLen)
  109. {
  110. var deleteSucceeded = Delete(TypeConverter.ToString(keyIndex), false);
  111. if (!deleteSucceeded)
  112. {
  113. newLenDesc.Value = JsValue.FromInt(keyIndex + 1);
  114. if (!newWritable)
  115. {
  116. newLenDesc.Writable = false;
  117. }
  118. base.DefineOwnProperty("length", _length = newLenDesc, false);
  119. if (throwOnError)
  120. {
  121. throw new JavaScriptException(_engine.TypeError);
  122. }
  123. return false;
  124. }
  125. }
  126. }
  127. }
  128. else
  129. {
  130. while (newLen < oldLen)
  131. {
  132. // algorithm as per the spec
  133. oldLen--;
  134. var deleteSucceeded = Delete(TypeConverter.ToString(oldLen), false);
  135. if (!deleteSucceeded)
  136. {
  137. newLenDesc.Value = oldLen + 1;
  138. if (!newWritable)
  139. {
  140. newLenDesc.Writable = false;
  141. }
  142. base.DefineOwnProperty("length", _length = newLenDesc, false);
  143. if (throwOnError)
  144. {
  145. throw new JavaScriptException(_engine.TypeError);
  146. }
  147. return false;
  148. }
  149. }
  150. }
  151. if (!newWritable)
  152. {
  153. DefineOwnProperty("length", new PropertyDescriptor(value: null, writable: false, enumerable: null, configurable: null), false);
  154. }
  155. return true;
  156. }
  157. else if (IsArrayIndex(propertyName, out index))
  158. {
  159. if (index >= oldLen && !oldLenDesc.Writable.Value)
  160. {
  161. if (throwOnError)
  162. {
  163. throw new JavaScriptException(_engine.TypeError);
  164. }
  165. return false;
  166. }
  167. var succeeded = base.DefineOwnProperty(propertyName, desc, false);
  168. if (!succeeded)
  169. {
  170. if (throwOnError)
  171. {
  172. throw new JavaScriptException(_engine.TypeError);
  173. }
  174. return false;
  175. }
  176. if (index >= oldLen)
  177. {
  178. oldLenDesc.Value = index + 1;
  179. base.DefineOwnProperty("length", _length = oldLenDesc, false);
  180. }
  181. return true;
  182. }
  183. return base.DefineOwnProperty(propertyName, desc, throwOnError);
  184. }
  185. public uint GetLength()
  186. {
  187. return TypeConverter.ToUint32(_length.Value);
  188. }
  189. public override IEnumerable<KeyValuePair<string, PropertyDescriptor>> GetOwnProperties()
  190. {
  191. foreach(var entry in _array)
  192. {
  193. yield return new KeyValuePair<string, PropertyDescriptor>(TypeConverter.ToString(entry.Key), entry.Value);
  194. }
  195. foreach(var entry in base.GetOwnProperties())
  196. {
  197. yield return entry;
  198. }
  199. }
  200. public override PropertyDescriptor GetOwnProperty(string propertyName)
  201. {
  202. uint index;
  203. if (IsArrayIndex(propertyName, out index))
  204. {
  205. PropertyDescriptor result;
  206. if (_array.TryGetValue(index, out result))
  207. {
  208. return result;
  209. }
  210. else
  211. {
  212. return PropertyDescriptor.Undefined;
  213. }
  214. }
  215. return base.GetOwnProperty(propertyName);
  216. }
  217. protected override void SetOwnProperty(string propertyName, PropertyDescriptor desc)
  218. {
  219. uint index;
  220. if (IsArrayIndex(propertyName, out index))
  221. {
  222. _array[index] = desc;
  223. }
  224. else
  225. {
  226. if(propertyName == "length")
  227. {
  228. _length = desc;
  229. }
  230. base.SetOwnProperty(propertyName, desc);
  231. }
  232. }
  233. public override bool HasOwnProperty(string p)
  234. {
  235. uint index;
  236. if (IsArrayIndex(p, out index))
  237. {
  238. return index < GetLength() && _array.ContainsKey(index);
  239. }
  240. return base.HasOwnProperty(p);
  241. }
  242. public override void RemoveOwnProperty(string p)
  243. {
  244. uint index;
  245. if(IsArrayIndex(p, out index))
  246. {
  247. _array.Remove(index);
  248. }
  249. base.RemoveOwnProperty(p);
  250. }
  251. private static bool IsArrayIndex(string p, out uint index)
  252. {
  253. index = ParseArrayIndex(p);
  254. return index != uint.MaxValue;
  255. // 15.4 - Use an optimized version of the specification
  256. // return TypeConverter.ToString(index) == TypeConverter.ToString(p) && index != uint.MaxValue;
  257. }
  258. internal static uint ParseArrayIndex(string p)
  259. {
  260. int d = p[0] - '0';
  261. if (d < 0 || d > 9)
  262. {
  263. return uint.MaxValue;
  264. }
  265. if(d == 0 && p.Length > 1)
  266. {
  267. // If p is a number that start with '0' and is not '0' then
  268. // its ToString representation can't be the same a p. This is
  269. // not a valid array index. '01' !== ToString(ToUInt32('01'))
  270. // http://www.ecma-international.org/ecma-262/5.1/#sec-15.4
  271. return uint.MaxValue;
  272. }
  273. ulong result = (uint)d;
  274. for (int i = 1; i < p.Length; i++)
  275. {
  276. d = p[i] - '0';
  277. if (d < 0 || d > 9)
  278. {
  279. return uint.MaxValue;
  280. }
  281. result = result * 10 + (uint)d;
  282. if (result >= uint.MaxValue)
  283. {
  284. return uint.MaxValue;
  285. }
  286. }
  287. return (uint)result;
  288. }
  289. }
  290. }