ArrayPrototype.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 : ObjectInstance
  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) {Extensible = true};
  18. obj.FastAddProperty("constructor", arrayConstructor, false, false, false);
  19. return obj;
  20. }
  21. public void Configure()
  22. {
  23. FastAddProperty("toString", new ClrFunctionInstance<object, object>(Engine, ToArrayString), false, false, false);
  24. FastAddProperty("toLocaleString", new ClrFunctionInstance<object, object>(Engine, ToLocaleString), false, false, false);
  25. FastAddProperty("concat", new ClrFunctionInstance<object, object>(Engine, Concat), false, false, false);
  26. FastAddProperty("join", new ClrFunctionInstance<object, object>(Engine, Join), false, false, false);
  27. FastAddProperty("pop", new ClrFunctionInstance<ArrayInstance, object>(Engine, Pop), false, false, false);
  28. FastAddProperty("push", new ClrFunctionInstance<ArrayInstance, object>(Engine, Push), false, false, false);
  29. FastAddProperty("reverse", new ClrFunctionInstance<object, object>(Engine, Reverse), false, false, false);
  30. FastAddProperty("shift", new ClrFunctionInstance<ArrayInstance, object>(Engine, Shift), false, false, false);
  31. FastAddProperty("slice", new ClrFunctionInstance<ArrayInstance, object>(Engine, Slice), false, false, false);
  32. FastAddProperty("sort", new ClrFunctionInstance<ArrayInstance, object>(Engine, Sort), false, false, false);
  33. FastAddProperty("splice", new ClrFunctionInstance<ArrayInstance, object>(Engine, Splice), false, false, false);
  34. FastAddProperty("unshift", new ClrFunctionInstance<ArrayInstance, object>(Engine, Unshift), false, false, false);
  35. FastAddProperty("indexOf", new ClrFunctionInstance<ArrayInstance, object>(Engine, IndexOf), false, false, false);
  36. FastAddProperty("lastIndexOf", new ClrFunctionInstance<ArrayInstance, object>(Engine, LastIndexOf), false, false, false);
  37. FastAddProperty("every", new ClrFunctionInstance<ArrayInstance, object>(Engine, Every), false, false, false);
  38. FastAddProperty("some", new ClrFunctionInstance<ArrayInstance, object>(Engine, Some), false, false, false);
  39. FastAddProperty("forEach", new ClrFunctionInstance<ArrayInstance, object>(Engine, ForEach), false, false, false);
  40. FastAddProperty("map", new ClrFunctionInstance<ArrayInstance, object>(Engine, Map), false, false, false);
  41. FastAddProperty("filter", new ClrFunctionInstance<ArrayInstance, object>(Engine, Filter), false, false, false);
  42. FastAddProperty("reduce", new ClrFunctionInstance<ArrayInstance, object>(Engine, Reduce), false, false, false);
  43. FastAddProperty("reduceRight", new ClrFunctionInstance<ArrayInstance, object>(Engine, ReduceRight), false, false, false);
  44. }
  45. private object LastIndexOf(ArrayInstance arg1, object[] arg2)
  46. {
  47. throw new NotImplementedException();
  48. }
  49. private object Reduce(ArrayInstance arg1, object[] arg2)
  50. {
  51. throw new NotImplementedException();
  52. }
  53. private object Filter(ArrayInstance arg1, object[] arg2)
  54. {
  55. throw new NotImplementedException();
  56. }
  57. private object Map(ArrayInstance arg1, object[] arg2)
  58. {
  59. throw new NotImplementedException();
  60. }
  61. private object ForEach(ArrayInstance arg1, object[] arg2)
  62. {
  63. throw new NotImplementedException();
  64. }
  65. private object Some(ArrayInstance arg1, object[] arg2)
  66. {
  67. throw new NotImplementedException();
  68. }
  69. private object Every(ArrayInstance arg1, object[] arg2)
  70. {
  71. throw new NotImplementedException();
  72. }
  73. private object IndexOf(ArrayInstance arg1, object[] arg2)
  74. {
  75. throw new NotImplementedException();
  76. }
  77. private object Splice(ArrayInstance arg1, object[] arg2)
  78. {
  79. throw new NotImplementedException();
  80. }
  81. private object Unshift(ArrayInstance arg1, object[] arg2)
  82. {
  83. throw new NotImplementedException();
  84. }
  85. private object Sort(ArrayInstance arg1, object[] arg2)
  86. {
  87. throw new NotImplementedException();
  88. }
  89. private object Slice(ArrayInstance arg1, object[] arg2)
  90. {
  91. throw new NotImplementedException();
  92. }
  93. private object Shift(ArrayInstance arg1, object[] arg2)
  94. {
  95. throw new NotImplementedException();
  96. }
  97. private object Reverse(object arg1, object[] arg2)
  98. {
  99. throw new NotImplementedException();
  100. }
  101. private object Join(object arg1, object[] arg2)
  102. {
  103. throw new NotImplementedException();
  104. }
  105. private object ToLocaleString(object arg1, object[] arg2)
  106. {
  107. throw new NotImplementedException();
  108. }
  109. private object Concat(object arg1, object[] arg2)
  110. {
  111. throw new NotImplementedException();
  112. }
  113. private object ToArrayString(object arg1, object[] arg2)
  114. {
  115. throw new NotImplementedException();
  116. }
  117. private object ReduceRight(ArrayInstance arg1, object[] arg2)
  118. {
  119. throw new NotImplementedException();
  120. }
  121. public object Push(object thisObject, object[] arguments)
  122. {
  123. ObjectInstance o = TypeConverter.ToObject(Engine, thisObject);
  124. object lenVal = o.Get("length");
  125. uint n = TypeConverter.ToUint32(lenVal);
  126. foreach (object e in arguments)
  127. {
  128. o.Put(TypeConverter.ToString(n), e, true);
  129. n++;
  130. }
  131. o.Put("length", n, true);
  132. return n;
  133. }
  134. public object Pop(object thisObject, object[] arguments)
  135. {
  136. ObjectInstance o = TypeConverter.ToObject(Engine, thisObject);
  137. object lenVal = o.Get("length");
  138. uint len = TypeConverter.ToUint32(lenVal);
  139. if (len == 0)
  140. {
  141. o.Put("length", 0, true);
  142. return Undefined.Instance;
  143. }
  144. else
  145. {
  146. string indx = TypeConverter.ToString(len - 1);
  147. object element = o.Get(indx);
  148. o.Delete(indx, true);
  149. o.Put("length", indx, true);
  150. return element;
  151. }
  152. }
  153. }
  154. }