ArrayPrototype.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. using System;
  2. using Jint.Native.Object;
  3. using Jint.Runtime;
  4. using Jint.Runtime.Interop;
  5. namespace Jint.Native.Array
  6. {
  7. /// <summary>
  8. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4
  9. /// </summary>
  10. public sealed class ArrayPrototype : ArrayInstance
  11. {
  12. private ArrayPrototype(Engine engine) : base(engine)
  13. {
  14. }
  15. public static ArrayPrototype CreatePrototypeObject(Engine engine, ArrayConstructor arrayConstructor)
  16. {
  17. var obj = new ArrayPrototype(engine)
  18. {
  19. Extensible = true,
  20. Prototype = engine.Object.PrototypeObject
  21. };
  22. obj.FastAddProperty("length", 0, true, false, false);
  23. obj.FastAddProperty("constructor", arrayConstructor, false, false, false);
  24. return obj;
  25. }
  26. public void Configure()
  27. {
  28. FastAddProperty("toString", new ClrFunctionInstance<object, object>(Engine, ToString, 0), true, false, true);
  29. FastAddProperty("toLocaleString", new ClrFunctionInstance<object, object>(Engine, ToLocaleString), true, false, true);
  30. FastAddProperty("concat", new ClrFunctionInstance<object, object>(Engine, Concat, 1), true, false, true);
  31. FastAddProperty("join", new ClrFunctionInstance<object, object>(Engine, Join, 1), true, false, true);
  32. FastAddProperty("pop", new ClrFunctionInstance<ArrayInstance, object>(Engine, Pop), true, false, true);
  33. FastAddProperty("push", new ClrFunctionInstance<ArrayInstance, object>(Engine, Push, 1), true, false, true);
  34. FastAddProperty("reverse", new ClrFunctionInstance<object, object>(Engine, Reverse), true, false, true);
  35. FastAddProperty("shift", new ClrFunctionInstance<ArrayInstance, object>(Engine, Shift), true, false, true);
  36. FastAddProperty("slice", new ClrFunctionInstance<ArrayInstance, object>(Engine, Slice, 2), true, false, true);
  37. FastAddProperty("sort", new ClrFunctionInstance<ArrayInstance, object>(Engine, Sort), true, false, true);
  38. FastAddProperty("splice", new ClrFunctionInstance<ArrayInstance, object>(Engine, Splice, 2), true, false, true);
  39. FastAddProperty("unshift", new ClrFunctionInstance<ArrayInstance, object>(Engine, Unshift), true, false, true);
  40. FastAddProperty("indexOf", new ClrFunctionInstance<ArrayInstance, object>(Engine, IndexOf), true, false, true);
  41. FastAddProperty("lastIndexOf", new ClrFunctionInstance<ArrayInstance, object>(Engine, LastIndexOf), true, false, true);
  42. FastAddProperty("every", new ClrFunctionInstance<ArrayInstance, object>(Engine, Every), true, false, true);
  43. FastAddProperty("some", new ClrFunctionInstance<ArrayInstance, object>(Engine, Some), true, false, true);
  44. FastAddProperty("forEach", new ClrFunctionInstance<ArrayInstance, object>(Engine, ForEach), true, false, true);
  45. FastAddProperty("map", new ClrFunctionInstance<ArrayInstance, object>(Engine, Map), true, false, true);
  46. FastAddProperty("filter", new ClrFunctionInstance<ArrayInstance, object>(Engine, Filter), true, false, true);
  47. FastAddProperty("reduce", new ClrFunctionInstance<ArrayInstance, object>(Engine, Reduce), true, false, true);
  48. FastAddProperty("reduceRight", new ClrFunctionInstance<ArrayInstance, object>(Engine, ReduceRight), true, false, true);
  49. }
  50. private object LastIndexOf(ArrayInstance arg1, object[] arg2)
  51. {
  52. throw new NotImplementedException();
  53. }
  54. private object Reduce(ArrayInstance arg1, object[] arg2)
  55. {
  56. throw new NotImplementedException();
  57. }
  58. private object Filter(ArrayInstance arg1, object[] arg2)
  59. {
  60. throw new NotImplementedException();
  61. }
  62. private object Map(ArrayInstance arg1, object[] arg2)
  63. {
  64. throw new NotImplementedException();
  65. }
  66. private object ForEach(ArrayInstance arg1, object[] arg2)
  67. {
  68. throw new NotImplementedException();
  69. }
  70. private object Some(ArrayInstance arg1, object[] arg2)
  71. {
  72. throw new NotImplementedException();
  73. }
  74. private object Every(ArrayInstance arg1, object[] arg2)
  75. {
  76. throw new NotImplementedException();
  77. }
  78. private object IndexOf(ArrayInstance arg1, object[] arg2)
  79. {
  80. throw new NotImplementedException();
  81. }
  82. private object Splice(ArrayInstance arg1, object[] arg2)
  83. {
  84. throw new NotImplementedException();
  85. }
  86. private object Unshift(ArrayInstance arg1, object[] arg2)
  87. {
  88. throw new NotImplementedException();
  89. }
  90. private object Sort(ArrayInstance arg1, object[] arg2)
  91. {
  92. throw new NotImplementedException();
  93. }
  94. private object Slice(ArrayInstance arg1, object[] arg2)
  95. {
  96. throw new NotImplementedException();
  97. }
  98. private object Shift(ArrayInstance arg1, object[] arg2)
  99. {
  100. throw new NotImplementedException();
  101. }
  102. private object Reverse(object thisObj, object[] arguments)
  103. {
  104. throw new NotImplementedException();
  105. }
  106. private object Join(object thisObj, object[] arguments)
  107. {
  108. var separator = arguments.Length > 0 ? arguments[0] : Undefined.Instance;
  109. var o = TypeConverter.ToObject(Engine, thisObj);
  110. var lenVal = o.Get("length");
  111. var len = TypeConverter.ToUint32(lenVal);
  112. if (len == 0)
  113. {
  114. return "";
  115. }
  116. if (separator == Undefined.Instance)
  117. {
  118. separator = ",";
  119. }
  120. var sep = TypeConverter.ToString(separator);
  121. var element0 = o.Get("0");
  122. string r = element0 == Undefined.Instance || element0 == Null.Instance
  123. ? ""
  124. : TypeConverter.ToString(element0);
  125. for (var k = 1; k < len; k++)
  126. {
  127. var s = r + sep;
  128. var element = o.Get(k.ToString());
  129. string next = element == Undefined.Instance || element == Null.Instance
  130. ? ""
  131. : TypeConverter.ToString(element);
  132. r = s + next;
  133. }
  134. return r;
  135. }
  136. private object ToLocaleString(object thisObj, object[] arguments)
  137. {
  138. var array = TypeConverter.ToObject(Engine, thisObj);
  139. var arrayLen = array.Get("length");
  140. var len = TypeConverter.ToUint32(arrayLen);
  141. var separator = ",";
  142. if (len == 0)
  143. {
  144. return "";
  145. }
  146. object r;
  147. var firstElement = array.Get("0");
  148. if (firstElement == Null.Instance || firstElement == Undefined.Instance)
  149. {
  150. r = "";
  151. }
  152. else
  153. {
  154. var elementObj = TypeConverter.ToObject(Engine, firstElement);
  155. var func = elementObj.Get("toLocaleString") as ICallable;
  156. if (func == null)
  157. {
  158. throw new JavaScriptException(Engine.TypeError);
  159. }
  160. r = func.Call(elementObj, Arguments.Empty);
  161. }
  162. for (var k = 1; k < len; k++)
  163. {
  164. string s = r + separator;
  165. var nextElement = array.Get(k.ToString());
  166. if (nextElement == Undefined.Instance || nextElement == Null.Instance)
  167. {
  168. r = "";
  169. }
  170. else
  171. {
  172. var elementObj = TypeConverter.ToObject(Engine, firstElement);
  173. var func = elementObj.Get("toLocaleString") as ICallable;
  174. if (func == null)
  175. {
  176. throw new JavaScriptException(Engine.TypeError);
  177. }
  178. r = func.Call(elementObj, Arguments.Empty);
  179. }
  180. r = s + r;
  181. }
  182. return r;
  183. }
  184. private object Concat(object thisObj, object[] arguments)
  185. {
  186. throw new NotImplementedException();
  187. }
  188. private object ToString(object thisObj, object[] arguments)
  189. {
  190. var array = TypeConverter.ToObject(Engine, thisObj);
  191. var func = array.Get("join") as ICallable;
  192. if (func == null)
  193. {
  194. func = Engine.Object.PrototypeObject.Get("toString") as ICallable;
  195. if (func == null)
  196. {
  197. throw new ArgumentException();
  198. }
  199. }
  200. return func.Call(array, Arguments.Empty);
  201. }
  202. private object ReduceRight(ArrayInstance arg1, object[] arg2)
  203. {
  204. throw new NotImplementedException();
  205. }
  206. public object Push(object thisObject, object[] arguments)
  207. {
  208. ObjectInstance o = TypeConverter.ToObject(Engine, thisObject);
  209. object lenVal = o.Get("length");
  210. uint n = TypeConverter.ToUint32(lenVal);
  211. foreach (object e in arguments)
  212. {
  213. o.Put(TypeConverter.ToString(n), e, true);
  214. n++;
  215. }
  216. o.Put("length", n, true);
  217. return n;
  218. }
  219. public object Pop(object thisObject, object[] arguments)
  220. {
  221. ObjectInstance o = TypeConverter.ToObject(Engine, thisObject);
  222. object lenVal = o.Get("length");
  223. uint len = TypeConverter.ToUint32(lenVal);
  224. if (len == 0)
  225. {
  226. o.Put("length", 0, true);
  227. return Undefined.Instance;
  228. }
  229. else
  230. {
  231. string indx = TypeConverter.ToString(len - 1);
  232. object element = o.Get(indx);
  233. o.Delete(indx, true);
  234. o.Put("length", indx, true);
  235. return element;
  236. }
  237. }
  238. }
  239. }