ArrayPrototype.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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<object, 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(object thisObj, object[] arg2)
  102. {
  103. var o = TypeConverter.ToObject(Engine, thisObj);
  104. var lenVal = o.Get("length");
  105. var len = TypeConverter.ToUint32(lenVal);
  106. if (len == 0)
  107. {
  108. o.Put("length", 0, true);
  109. return Undefined.Instance;
  110. }
  111. var first = o.Get("0");
  112. for (var k = 1; k < len; k++)
  113. {
  114. var from = TypeConverter.ToString(k);
  115. var to = TypeConverter.ToString(k - 1);
  116. var fromPresent = o.HasProperty(from);
  117. if (fromPresent)
  118. {
  119. var fromVal = o.Get(from);
  120. o.Put(to, fromVal, true);
  121. }
  122. else
  123. {
  124. o.Delete(to, true);
  125. }
  126. }
  127. o.Delete(TypeConverter.ToString(len - 1), true);
  128. o.Put("length", len-1, true);
  129. return first;
  130. }
  131. private object Reverse(object thisObj, object[] arguments)
  132. {
  133. var o = TypeConverter.ToObject(Engine, thisObj);
  134. var lenVal = o.Get("length");
  135. var len = TypeConverter.ToUint32(lenVal);
  136. var middle = (uint)System.Math.Floor(len/2);
  137. uint lower = 0;
  138. while (lower != middle)
  139. {
  140. var upper = len - lower - 1;
  141. var upperP = TypeConverter.ToString(upper);
  142. var lowerP = TypeConverter.ToString(lower);
  143. var lowerValue = o.Get(lowerP);
  144. var upperValue = o.Get(upperP);
  145. var lowerExists = o.HasProperty(lowerP);
  146. var upperExists = o.HasProperty(upperP);
  147. if (lowerExists && upperExists)
  148. {
  149. o.Put(lowerP, upperValue, true);
  150. o.Put(upperP, lowerValue, true);
  151. }
  152. if (!lowerExists && upperExists)
  153. {
  154. o.Put(lowerP, upperValue, true);
  155. o.Delete(upperP, true);
  156. }
  157. if (lowerExists && !upperExists)
  158. {
  159. o.Delete(lowerP, true);
  160. o.Put(upperP, lowerValue, true);
  161. }
  162. lower++;
  163. }
  164. return o;
  165. }
  166. private object Join(object thisObj, object[] arguments)
  167. {
  168. var separator = arguments.Length > 0 ? arguments[0] : Undefined.Instance;
  169. var o = TypeConverter.ToObject(Engine, thisObj);
  170. var lenVal = o.Get("length");
  171. var len = TypeConverter.ToUint32(lenVal);
  172. if (separator == Undefined.Instance)
  173. {
  174. separator = ",";
  175. }
  176. var sep = TypeConverter.ToString(separator);
  177. // as per the spec, this has to be called after ToString(separator)
  178. if (len == 0)
  179. {
  180. return "";
  181. }
  182. var element0 = o.Get("0");
  183. string r = element0 == Undefined.Instance || element0 == Null.Instance
  184. ? ""
  185. : TypeConverter.ToString(element0);
  186. for (var k = 1; k < len; k++)
  187. {
  188. var s = r + sep;
  189. var element = o.Get(k.ToString());
  190. string next = element == Undefined.Instance || element == Null.Instance
  191. ? ""
  192. : TypeConverter.ToString(element);
  193. r = s + next;
  194. }
  195. return r;
  196. }
  197. private object ToLocaleString(object thisObj, object[] arguments)
  198. {
  199. var array = TypeConverter.ToObject(Engine, thisObj);
  200. var arrayLen = array.Get("length");
  201. var len = TypeConverter.ToUint32(arrayLen);
  202. var separator = ",";
  203. if (len == 0)
  204. {
  205. return "";
  206. }
  207. object r;
  208. var firstElement = array.Get("0");
  209. if (firstElement == Null.Instance || firstElement == Undefined.Instance)
  210. {
  211. r = "";
  212. }
  213. else
  214. {
  215. var elementObj = TypeConverter.ToObject(Engine, firstElement);
  216. var func = elementObj.Get("toLocaleString") as ICallable;
  217. if (func == null)
  218. {
  219. throw new JavaScriptException(Engine.TypeError);
  220. }
  221. r = func.Call(elementObj, Arguments.Empty);
  222. }
  223. for (var k = 1; k < len; k++)
  224. {
  225. string s = r + separator;
  226. var nextElement = array.Get(k.ToString());
  227. if (nextElement == Undefined.Instance || nextElement == Null.Instance)
  228. {
  229. r = "";
  230. }
  231. else
  232. {
  233. var elementObj = TypeConverter.ToObject(Engine, nextElement);
  234. var func = elementObj.Get("toLocaleString") as ICallable;
  235. if (func == null)
  236. {
  237. throw new JavaScriptException(Engine.TypeError);
  238. }
  239. r = func.Call(elementObj, Arguments.Empty);
  240. }
  241. r = s + r;
  242. }
  243. return r;
  244. }
  245. private object Concat(object thisObj, object[] arguments)
  246. {
  247. var o = TypeConverter.ToObject(Engine, thisObj);
  248. var a = Engine.Array.Construct(Arguments.Empty);
  249. var n = 0;
  250. var items = new List<object>() {o};
  251. items.AddRange(arguments);
  252. foreach (var e in items)
  253. {
  254. var eArray = e as ArrayInstance;
  255. if (eArray != null)
  256. {
  257. var len = TypeConverter.ToUint32(eArray.Get("length"));
  258. for (var k = 0; k < len; k++)
  259. {
  260. var p = k.ToString();
  261. var exists = eArray.HasProperty(p);
  262. if (exists)
  263. {
  264. var subElement = eArray.Get(p);
  265. a.DefineOwnProperty(TypeConverter.ToString(n), new DataDescriptor(subElement) { Writable = true, Enumerable = true, Configurable = true}, false);
  266. }
  267. n++;
  268. }
  269. }
  270. else
  271. {
  272. a.DefineOwnProperty(TypeConverter.ToString(n), new DataDescriptor(e) { Writable = true, Enumerable = true, Configurable = true }, false);
  273. n++;
  274. }
  275. }
  276. return a;
  277. }
  278. private object ToString(object thisObj, object[] arguments)
  279. {
  280. var array = TypeConverter.ToObject(Engine, thisObj);
  281. var func = array.Get("join") as ICallable;
  282. if (func == null)
  283. {
  284. func = Engine.Object.PrototypeObject.Get("toString") as ICallable;
  285. if (func == null)
  286. {
  287. throw new ArgumentException();
  288. }
  289. }
  290. return func.Call(array, Arguments.Empty);
  291. }
  292. private object ReduceRight(ArrayInstance arg1, object[] arg2)
  293. {
  294. throw new NotImplementedException();
  295. }
  296. public object Push(object thisObject, object[] arguments)
  297. {
  298. ObjectInstance o = TypeConverter.ToObject(Engine, thisObject);
  299. object lenVal = o.Get("length");
  300. // cast to double as we need to prevent an overflow
  301. double n = TypeConverter.ToUint32(lenVal);
  302. foreach (object e in arguments)
  303. {
  304. o.Put(TypeConverter.ToString(n), e, true);
  305. n++;
  306. }
  307. o.Put("length", n, true);
  308. return n;
  309. }
  310. public object Pop(object thisObject, object[] arguments)
  311. {
  312. ObjectInstance o = TypeConverter.ToObject(Engine, thisObject);
  313. object lenVal = o.Get("length");
  314. uint len = TypeConverter.ToUint32(lenVal);
  315. if (len == 0)
  316. {
  317. o.Put("length", 0, true);
  318. return Undefined.Instance;
  319. }
  320. else
  321. {
  322. len = len - 1;
  323. string indx = TypeConverter.ToString(len);
  324. object element = o.Get(indx);
  325. o.Delete(indx, true);
  326. o.Put("length", len, true);
  327. return element;
  328. }
  329. }
  330. }
  331. }