ArrayPrototype.cs 12 KB

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