ArrayPrototype.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. throw new NotImplementedException();
  108. }
  109. private object Join(object thisObj, object[] arguments)
  110. {
  111. var separator = arguments.Length > 0 ? arguments[0] : Undefined.Instance;
  112. var o = TypeConverter.ToObject(Engine, thisObj);
  113. var lenVal = o.Get("length");
  114. var len = TypeConverter.ToUint32(lenVal);
  115. if (separator == Undefined.Instance)
  116. {
  117. separator = ",";
  118. }
  119. var sep = TypeConverter.ToString(separator);
  120. // as per the spec, this has to be called after ToString(separator)
  121. if (len == 0)
  122. {
  123. return "";
  124. }
  125. var element0 = o.Get("0");
  126. string r = element0 == Undefined.Instance || element0 == Null.Instance
  127. ? ""
  128. : TypeConverter.ToString(element0);
  129. for (var k = 1; k < len; k++)
  130. {
  131. var s = r + sep;
  132. var element = o.Get(k.ToString());
  133. string next = element == Undefined.Instance || element == Null.Instance
  134. ? ""
  135. : TypeConverter.ToString(element);
  136. r = s + next;
  137. }
  138. return r;
  139. }
  140. private object ToLocaleString(object thisObj, object[] arguments)
  141. {
  142. var array = TypeConverter.ToObject(Engine, thisObj);
  143. var arrayLen = array.Get("length");
  144. var len = TypeConverter.ToUint32(arrayLen);
  145. var separator = ",";
  146. if (len == 0)
  147. {
  148. return "";
  149. }
  150. object r;
  151. var firstElement = array.Get("0");
  152. if (firstElement == Null.Instance || firstElement == Undefined.Instance)
  153. {
  154. r = "";
  155. }
  156. else
  157. {
  158. var elementObj = TypeConverter.ToObject(Engine, firstElement);
  159. var func = elementObj.Get("toLocaleString") as ICallable;
  160. if (func == null)
  161. {
  162. throw new JavaScriptException(Engine.TypeError);
  163. }
  164. r = func.Call(elementObj, Arguments.Empty);
  165. }
  166. for (var k = 1; k < len; k++)
  167. {
  168. string s = r + separator;
  169. var nextElement = array.Get(k.ToString());
  170. if (nextElement == Undefined.Instance || nextElement == Null.Instance)
  171. {
  172. r = "";
  173. }
  174. else
  175. {
  176. var elementObj = TypeConverter.ToObject(Engine, nextElement);
  177. var func = elementObj.Get("toLocaleString") as ICallable;
  178. if (func == null)
  179. {
  180. throw new JavaScriptException(Engine.TypeError);
  181. }
  182. r = func.Call(elementObj, Arguments.Empty);
  183. }
  184. r = s + r;
  185. }
  186. return r;
  187. }
  188. private object Concat(object thisObj, object[] arguments)
  189. {
  190. var o = TypeConverter.ToObject(Engine, thisObj);
  191. var a = Engine.Array.Construct(Arguments.Empty);
  192. var n = 0;
  193. var items = new List<object>() {o};
  194. items.AddRange(arguments);
  195. foreach (var e in items)
  196. {
  197. var eArray = e as ArrayInstance;
  198. if (eArray != null)
  199. {
  200. var len = TypeConverter.ToUint32(eArray.Get("length"));
  201. for (var k = 0; k < len; k++)
  202. {
  203. var p = k.ToString();
  204. var exists = eArray.HasProperty(p);
  205. if (exists)
  206. {
  207. var subElement = eArray.Get(p);
  208. a.DefineOwnProperty(TypeConverter.ToString(n), new DataDescriptor(subElement) { Writable = true, Enumerable = true, Configurable = true}, false);
  209. }
  210. n++;
  211. }
  212. }
  213. else
  214. {
  215. a.DefineOwnProperty(TypeConverter.ToString(n), new DataDescriptor(e) { Writable = true, Enumerable = true, Configurable = true }, false);
  216. n++;
  217. }
  218. }
  219. return a;
  220. }
  221. private object ToString(object thisObj, object[] arguments)
  222. {
  223. var array = TypeConverter.ToObject(Engine, thisObj);
  224. var func = array.Get("join") as ICallable;
  225. if (func == null)
  226. {
  227. func = Engine.Object.PrototypeObject.Get("toString") as ICallable;
  228. if (func == null)
  229. {
  230. throw new ArgumentException();
  231. }
  232. }
  233. return func.Call(array, Arguments.Empty);
  234. }
  235. private object ReduceRight(ArrayInstance arg1, object[] arg2)
  236. {
  237. throw new NotImplementedException();
  238. }
  239. public object Push(object thisObject, object[] arguments)
  240. {
  241. ObjectInstance o = TypeConverter.ToObject(Engine, thisObject);
  242. object lenVal = o.Get("length");
  243. // cast to double as we need to prevent an overflow
  244. double n = TypeConverter.ToUint32(lenVal);
  245. foreach (object e in arguments)
  246. {
  247. o.Put(TypeConverter.ToString(n), e, true);
  248. n++;
  249. }
  250. o.Put("length", n, true);
  251. return n;
  252. }
  253. public object Pop(object thisObject, object[] arguments)
  254. {
  255. ObjectInstance o = TypeConverter.ToObject(Engine, thisObject);
  256. object lenVal = o.Get("length");
  257. uint len = TypeConverter.ToUint32(lenVal);
  258. if (len == 0)
  259. {
  260. o.Put("length", 0, true);
  261. return Undefined.Instance;
  262. }
  263. else
  264. {
  265. len = len - 1;
  266. string indx = TypeConverter.ToString(len);
  267. object element = o.Get(indx);
  268. o.Delete(indx, true);
  269. o.Put("length", len, true);
  270. return element;
  271. }
  272. }
  273. }
  274. }