ArrayPrototype.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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, false, 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), true, false, true);
  29. FastAddProperty("toLocaleString", new ClrFunctionInstance<object, object>(Engine, ToLocaleString), true, false, true);
  30. FastAddProperty("concat", new ClrFunctionInstance<object, object>(Engine, Concat), true, false, true);
  31. FastAddProperty("join", new ClrFunctionInstance<object, object>(Engine, Join), true, false, true);
  32. FastAddProperty("pop", new ClrFunctionInstance<ArrayInstance, object>(Engine, Pop), true, false, true);
  33. FastAddProperty("push", new ClrFunctionInstance<ArrayInstance, object>(Engine, Push), 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), true, false, true);
  37. FastAddProperty("sort", new ClrFunctionInstance<ArrayInstance, object>(Engine, Sort), true, false, true);
  38. FastAddProperty("splice", new ClrFunctionInstance<ArrayInstance, object>(Engine, Splice), 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. throw new NotImplementedException();
  109. }
  110. private object ToLocaleString(object thisObj, object[] arguments)
  111. {
  112. var array = TypeConverter.ToObject(Engine, thisObj);
  113. var arrayLen = array.Get("length");
  114. var len = TypeConverter.ToUint32(arrayLen);
  115. var separator = ",";
  116. if (len == 0)
  117. {
  118. return "";
  119. }
  120. object r;
  121. var firstElement = array.Get("0");
  122. if (firstElement == Null.Instance || firstElement == Undefined.Instance)
  123. {
  124. r = "";
  125. }
  126. else
  127. {
  128. var elementObj = TypeConverter.ToObject(Engine, firstElement);
  129. var func = elementObj.Get("toLocaleString") as ICallable;
  130. if (func == null)
  131. {
  132. throw new JavaScriptException(Engine.TypeError);
  133. }
  134. r = func.Call(elementObj, Arguments.Empty);
  135. }
  136. for (var k = 1; k < len; k++)
  137. {
  138. string s = r + separator;
  139. var nextElement = array.Get(k.ToString());
  140. if (nextElement == Undefined.Instance || nextElement == Null.Instance)
  141. {
  142. r = "";
  143. }
  144. else
  145. {
  146. var elementObj = TypeConverter.ToObject(Engine, firstElement);
  147. var func = elementObj.Get("toLocaleString") as ICallable;
  148. if (func == null)
  149. {
  150. throw new JavaScriptException(Engine.TypeError);
  151. }
  152. r = func.Call(elementObj, Arguments.Empty);
  153. }
  154. r = s + r;
  155. }
  156. return r;
  157. }
  158. private object Concat(object thisObj, object[] arguments)
  159. {
  160. throw new NotImplementedException();
  161. }
  162. private object ToString(object thisObj, object[] arguments)
  163. {
  164. var array = TypeConverter.ToObject(Engine, thisObj);
  165. var func = array.Get("join") as ICallable;
  166. if (func == null)
  167. {
  168. func = Engine.Object.PrototypeObject.Get("toString") as ICallable;
  169. if (func == null)
  170. {
  171. throw new ArgumentException();
  172. }
  173. }
  174. return func.Call(array, Arguments.Empty);
  175. }
  176. private object ReduceRight(ArrayInstance arg1, object[] arg2)
  177. {
  178. throw new NotImplementedException();
  179. }
  180. public object Push(object thisObject, object[] arguments)
  181. {
  182. ObjectInstance o = TypeConverter.ToObject(Engine, thisObject);
  183. object lenVal = o.Get("length");
  184. uint n = TypeConverter.ToUint32(lenVal);
  185. foreach (object e in arguments)
  186. {
  187. o.Put(TypeConverter.ToString(n), e, true);
  188. n++;
  189. }
  190. o.Put("length", n, true);
  191. return n;
  192. }
  193. public object Pop(object thisObject, object[] arguments)
  194. {
  195. ObjectInstance o = TypeConverter.ToObject(Engine, thisObject);
  196. object lenVal = o.Get("length");
  197. uint len = TypeConverter.ToUint32(lenVal);
  198. if (len == 0)
  199. {
  200. o.Put("length", 0, true);
  201. return Undefined.Instance;
  202. }
  203. else
  204. {
  205. string indx = TypeConverter.ToString(len - 1);
  206. object element = o.Get(indx);
  207. o.Delete(indx, true);
  208. o.Put("length", indx, true);
  209. return element;
  210. }
  211. }
  212. }
  213. }