ArrayPrototype.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Dynamic;
  4. using Jint.Native.Object;
  5. using Jint.Runtime;
  6. using Jint.Runtime.Descriptors;
  7. using Jint.Runtime.Interop;
  8. namespace Jint.Native.Array
  9. {
  10. /// <summary>
  11. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4
  12. /// </summary>
  13. public sealed class ArrayPrototype : ArrayInstance
  14. {
  15. private ArrayPrototype(Engine engine) : base(engine)
  16. {
  17. }
  18. public static ArrayPrototype CreatePrototypeObject(Engine engine, ArrayConstructor arrayConstructor)
  19. {
  20. var obj = new ArrayPrototype(engine)
  21. {
  22. Extensible = true,
  23. Prototype = engine.Object.PrototypeObject
  24. };
  25. obj.FastAddProperty("length", 0, true, false, false);
  26. obj.FastAddProperty("constructor", arrayConstructor, false, false, false);
  27. return obj;
  28. }
  29. public void Configure()
  30. {
  31. FastAddProperty("toString", new ClrFunctionInstance<object, object>(Engine, ToString, 0), true, false, true);
  32. FastAddProperty("toLocaleString", new ClrFunctionInstance<object, object>(Engine, ToLocaleString), true, false, true);
  33. FastAddProperty("concat", new ClrFunctionInstance<object, object>(Engine, Concat, 1), true, false, true);
  34. FastAddProperty("join", new ClrFunctionInstance<object, object>(Engine, Join, 1), true, false, true);
  35. FastAddProperty("pop", new ClrFunctionInstance<object, object>(Engine, Pop), true, false, true);
  36. FastAddProperty("push", new ClrFunctionInstance<object, object>(Engine, Push, 1), true, false, true);
  37. FastAddProperty("reverse", new ClrFunctionInstance<object, object>(Engine, Reverse), true, false, true);
  38. FastAddProperty("shift", new ClrFunctionInstance<ArrayInstance, object>(Engine, Shift), true, false, true);
  39. FastAddProperty("slice", new ClrFunctionInstance<ArrayInstance, object>(Engine, Slice, 2), true, false, true);
  40. FastAddProperty("sort", new ClrFunctionInstance<ArrayInstance, object>(Engine, Sort), true, false, true);
  41. FastAddProperty("splice", new ClrFunctionInstance<ArrayInstance, object>(Engine, Splice, 2), true, false, true);
  42. FastAddProperty("unshift", new ClrFunctionInstance<ArrayInstance, object>(Engine, Unshift), true, false, true);
  43. FastAddProperty("indexOf", new ClrFunctionInstance<ArrayInstance, object>(Engine, IndexOf), true, false, true);
  44. FastAddProperty("lastIndexOf", new ClrFunctionInstance<ArrayInstance, object>(Engine, LastIndexOf), true, false, true);
  45. FastAddProperty("every", new ClrFunctionInstance<ArrayInstance, object>(Engine, Every), true, false, true);
  46. FastAddProperty("some", new ClrFunctionInstance<ArrayInstance, object>(Engine, Some), true, false, true);
  47. FastAddProperty("forEach", new ClrFunctionInstance<ArrayInstance, object>(Engine, ForEach), true, false, true);
  48. FastAddProperty("map", new ClrFunctionInstance<ArrayInstance, object>(Engine, Map), true, false, true);
  49. FastAddProperty("filter", new ClrFunctionInstance<ArrayInstance, object>(Engine, Filter), true, false, true);
  50. FastAddProperty("reduce", new ClrFunctionInstance<ArrayInstance, object>(Engine, Reduce), true, false, true);
  51. FastAddProperty("reduceRight", new ClrFunctionInstance<ArrayInstance, object>(Engine, ReduceRight), true, false, true);
  52. }
  53. private object LastIndexOf(ArrayInstance arg1, object[] arg2)
  54. {
  55. throw new NotImplementedException();
  56. }
  57. private object Reduce(ArrayInstance arg1, object[] arg2)
  58. {
  59. throw new NotImplementedException();
  60. }
  61. private object Filter(ArrayInstance arg1, object[] arg2)
  62. {
  63. throw new NotImplementedException();
  64. }
  65. private object Map(ArrayInstance arg1, object[] arg2)
  66. {
  67. throw new NotImplementedException();
  68. }
  69. private object ForEach(ArrayInstance arg1, object[] arg2)
  70. {
  71. throw new NotImplementedException();
  72. }
  73. private object Some(ArrayInstance arg1, object[] arg2)
  74. {
  75. throw new NotImplementedException();
  76. }
  77. private object Every(ArrayInstance arg1, object[] arg2)
  78. {
  79. throw new NotImplementedException();
  80. }
  81. private object IndexOf(ArrayInstance arg1, object[] arg2)
  82. {
  83. throw new NotImplementedException();
  84. }
  85. private object Splice(ArrayInstance arg1, object[] arg2)
  86. {
  87. throw new NotImplementedException();
  88. }
  89. private object Unshift(ArrayInstance arg1, object[] arg2)
  90. {
  91. throw new NotImplementedException();
  92. }
  93. private object Sort(ArrayInstance arg1, object[] arg2)
  94. {
  95. throw new NotImplementedException();
  96. }
  97. private object Slice(ArrayInstance arg1, object[] arg2)
  98. {
  99. throw new NotImplementedException();
  100. }
  101. private object Shift(ArrayInstance arg1, object[] arg2)
  102. {
  103. throw new NotImplementedException();
  104. }
  105. private object Reverse(object thisObj, object[] arguments)
  106. {
  107. var o = TypeConverter.ToObject(Engine, thisObj);
  108. var lenVal = o.Get("length");
  109. var len = TypeConverter.ToUint32(lenVal);
  110. var middle = (uint)System.Math.Floor(len/2);
  111. uint lower = 0;
  112. while (lower != middle)
  113. {
  114. var upper = len - lower - 1;
  115. var upperP = TypeConverter.ToString(upper);
  116. var lowerP = TypeConverter.ToString(lower);
  117. var lowerValue = o.Get(lowerP);
  118. var upperValue = o.Get(upperP);
  119. var lowerExists = o.HasProperty(lowerP);
  120. var upperExists = o.HasProperty(upperP);
  121. if (lowerExists && upperExists)
  122. {
  123. o.Put(lowerP, upperValue, true);
  124. o.Put(upperP, lowerValue, true);
  125. }
  126. if (!lowerExists && upperExists)
  127. {
  128. o.Put(lowerP, upperValue, true);
  129. o.Delete(upperP, true);
  130. }
  131. if (lowerExists && !upperExists)
  132. {
  133. o.Delete(lowerP, true);
  134. o.Put(upperP, lowerValue, true);
  135. }
  136. lower++;
  137. }
  138. return o;
  139. }
  140. private object Join(object thisObj, object[] arguments)
  141. {
  142. var separator = arguments.Length > 0 ? arguments[0] : Undefined.Instance;
  143. var o = TypeConverter.ToObject(Engine, thisObj);
  144. var lenVal = o.Get("length");
  145. var len = TypeConverter.ToUint32(lenVal);
  146. if (separator == Undefined.Instance)
  147. {
  148. separator = ",";
  149. }
  150. var sep = TypeConverter.ToString(separator);
  151. // as per the spec, this has to be called after ToString(separator)
  152. if (len == 0)
  153. {
  154. return "";
  155. }
  156. var element0 = o.Get("0");
  157. string r = element0 == Undefined.Instance || element0 == Null.Instance
  158. ? ""
  159. : TypeConverter.ToString(element0);
  160. for (var k = 1; k < len; k++)
  161. {
  162. var s = r + sep;
  163. var element = o.Get(k.ToString());
  164. string next = element == Undefined.Instance || element == Null.Instance
  165. ? ""
  166. : TypeConverter.ToString(element);
  167. r = s + next;
  168. }
  169. return r;
  170. }
  171. private object ToLocaleString(object thisObj, object[] arguments)
  172. {
  173. var array = TypeConverter.ToObject(Engine, thisObj);
  174. var arrayLen = array.Get("length");
  175. var len = TypeConverter.ToUint32(arrayLen);
  176. var separator = ",";
  177. if (len == 0)
  178. {
  179. return "";
  180. }
  181. object r;
  182. var firstElement = array.Get("0");
  183. if (firstElement == Null.Instance || firstElement == Undefined.Instance)
  184. {
  185. r = "";
  186. }
  187. else
  188. {
  189. var elementObj = TypeConverter.ToObject(Engine, firstElement);
  190. var func = elementObj.Get("toLocaleString") as ICallable;
  191. if (func == null)
  192. {
  193. throw new JavaScriptException(Engine.TypeError);
  194. }
  195. r = func.Call(elementObj, Arguments.Empty);
  196. }
  197. for (var k = 1; k < len; k++)
  198. {
  199. string s = r + separator;
  200. var nextElement = array.Get(k.ToString());
  201. if (nextElement == Undefined.Instance || nextElement == Null.Instance)
  202. {
  203. r = "";
  204. }
  205. else
  206. {
  207. var elementObj = TypeConverter.ToObject(Engine, nextElement);
  208. var func = elementObj.Get("toLocaleString") as ICallable;
  209. if (func == null)
  210. {
  211. throw new JavaScriptException(Engine.TypeError);
  212. }
  213. r = func.Call(elementObj, Arguments.Empty);
  214. }
  215. r = s + r;
  216. }
  217. return r;
  218. }
  219. private object Concat(object thisObj, object[] arguments)
  220. {
  221. var o = TypeConverter.ToObject(Engine, thisObj);
  222. var a = Engine.Array.Construct(Arguments.Empty);
  223. var n = 0;
  224. var items = new List<object>() {o};
  225. items.AddRange(arguments);
  226. foreach (var e in items)
  227. {
  228. var eArray = e as ArrayInstance;
  229. if (eArray != null)
  230. {
  231. var len = TypeConverter.ToUint32(eArray.Get("length"));
  232. for (var k = 0; k < len; k++)
  233. {
  234. var p = k.ToString();
  235. var exists = eArray.HasProperty(p);
  236. if (exists)
  237. {
  238. var subElement = eArray.Get(p);
  239. a.DefineOwnProperty(TypeConverter.ToString(n), new DataDescriptor(subElement) { Writable = true, Enumerable = true, Configurable = true}, false);
  240. }
  241. n++;
  242. }
  243. }
  244. else
  245. {
  246. a.DefineOwnProperty(TypeConverter.ToString(n), new DataDescriptor(e) { Writable = true, Enumerable = true, Configurable = true }, false);
  247. n++;
  248. }
  249. }
  250. return a;
  251. }
  252. private object ToString(object thisObj, object[] arguments)
  253. {
  254. var array = TypeConverter.ToObject(Engine, thisObj);
  255. var func = array.Get("join") as ICallable;
  256. if (func == null)
  257. {
  258. func = Engine.Object.PrototypeObject.Get("toString") as ICallable;
  259. if (func == null)
  260. {
  261. throw new ArgumentException();
  262. }
  263. }
  264. return func.Call(array, Arguments.Empty);
  265. }
  266. private object ReduceRight(ArrayInstance arg1, object[] arg2)
  267. {
  268. throw new NotImplementedException();
  269. }
  270. public object Push(object thisObject, object[] arguments)
  271. {
  272. ObjectInstance o = TypeConverter.ToObject(Engine, thisObject);
  273. object lenVal = o.Get("length");
  274. // cast to double as we need to prevent an overflow
  275. double n = TypeConverter.ToUint32(lenVal);
  276. foreach (object e in arguments)
  277. {
  278. o.Put(TypeConverter.ToString(n), e, true);
  279. n++;
  280. }
  281. o.Put("length", n, true);
  282. return n;
  283. }
  284. public object Pop(object thisObject, object[] arguments)
  285. {
  286. ObjectInstance o = TypeConverter.ToObject(Engine, thisObject);
  287. object lenVal = o.Get("length");
  288. uint len = TypeConverter.ToUint32(lenVal);
  289. if (len == 0)
  290. {
  291. o.Put("length", 0, true);
  292. return Undefined.Instance;
  293. }
  294. else
  295. {
  296. len = len - 1;
  297. string indx = TypeConverter.ToString(len);
  298. object element = o.Get(indx);
  299. o.Delete(indx, true);
  300. o.Put("length", len, true);
  301. return element;
  302. }
  303. }
  304. }
  305. }