MathInstance.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using System;
  2. using Jint.Native.Object;
  3. using Jint.Runtime;
  4. using Jint.Runtime.Interop;
  5. namespace Jint.Native.Math
  6. {
  7. public sealed class MathInstance : ObjectInstance
  8. {
  9. private MathInstance(Engine engine):base(engine)
  10. {
  11. }
  12. public override string Class
  13. {
  14. get
  15. {
  16. return "Math";
  17. }
  18. }
  19. public static MathInstance CreateMathObject(Engine engine)
  20. {
  21. var math = new MathInstance(engine);
  22. math.Extensible = true;
  23. math.Prototype = engine.Object.Prototype;
  24. return math;
  25. }
  26. public void Configure()
  27. {
  28. FastAddProperty("abs", new ClrFunctionInstance<object, double>(Engine, Abs), false, false, false);
  29. FastAddProperty("acos", new ClrFunctionInstance<object, double>(Engine, Acos), false, false, false);
  30. FastAddProperty("asin", new ClrFunctionInstance<object, double>(Engine, Asin), false, false, false);
  31. FastAddProperty("atan", new ClrFunctionInstance<object, double>(Engine, Atan), false, false, false);
  32. FastAddProperty("atan2", new ClrFunctionInstance<object, double>(Engine, Atan2), false, false, false);
  33. FastAddProperty("ceil", new ClrFunctionInstance<object, double>(Engine, Ceil), false, false, false);
  34. FastAddProperty("cos", new ClrFunctionInstance<object, double>(Engine, Cos), false, false, false);
  35. FastAddProperty("exp", new ClrFunctionInstance<object, double>(Engine, Exp), false, false, false);
  36. FastAddProperty("floor", new ClrFunctionInstance<object, double>(Engine, Floor), false, false, false);
  37. FastAddProperty("log", new ClrFunctionInstance<object, double>(Engine, Log), false, false, false);
  38. FastAddProperty("max", new ClrFunctionInstance<object, double>(Engine, Max), false, false, false);
  39. FastAddProperty("min", new ClrFunctionInstance<object, double>(Engine, Min), false, false, false);
  40. FastAddProperty("pow", new ClrFunctionInstance<object, double>(Engine, Pow), false, false, false);
  41. FastAddProperty("random", new ClrFunctionInstance<object, double>(Engine, Random), false, false, false);
  42. FastAddProperty("round", new ClrFunctionInstance<object, double>(Engine, Round), false, false, false);
  43. FastAddProperty("sin", new ClrFunctionInstance<object, double>(Engine, Sin), false, false, false);
  44. FastAddProperty("sqrt", new ClrFunctionInstance<object, double>(Engine, Sqrt), false, false, false);
  45. FastAddProperty("tan", new ClrFunctionInstance<object, double>(Engine, Tan), false, false, false);
  46. FastAddProperty("E", System.Math.E, false, false, false);
  47. FastAddProperty("LN10", System.Math.Log(10), false, false, false);
  48. FastAddProperty("LN2", System.Math.Log(2), false, false, false);
  49. FastAddProperty("LOG2E", System.Math.Log(System.Math.E, 2), false, false, false);
  50. FastAddProperty("LOG10E", System.Math.Log(System.Math.E, 10), false, false, false);
  51. FastAddProperty("PI", System.Math.PI, false, false, false);
  52. FastAddProperty("SQRT1_2", System.Math.Sqrt(0.5), false, false, false);
  53. FastAddProperty("SQRT2", System.Math.Sqrt(2), false, false, false);
  54. }
  55. private static double Abs(object thisObject, object[] arguments)
  56. {
  57. var x = TypeConverter.ToNumber(arguments[0]);
  58. return System.Math.Abs(x);
  59. }
  60. private static double Acos(object thisObject, object[] arguments)
  61. {
  62. var x = TypeConverter.ToNumber(arguments[0]);
  63. return System.Math.Acos(x);
  64. }
  65. private static double Asin(object thisObject, object[] arguments)
  66. {
  67. var x = TypeConverter.ToNumber(arguments[0]);
  68. return System.Math.Asin(x);
  69. }
  70. private static double Atan(object thisObject, object[] arguments)
  71. {
  72. var x = TypeConverter.ToNumber(arguments[0]);
  73. return System.Math.Atan(x);
  74. }
  75. private static double Atan2(object thisObject, object[] arguments)
  76. {
  77. var x = TypeConverter.ToNumber(arguments[0]);
  78. var y = TypeConverter.ToNumber(arguments[1]);
  79. return System.Math.Atan2(x, y);
  80. }
  81. private static double Ceil(object thisObject, object[] arguments)
  82. {
  83. var x = TypeConverter.ToNumber(arguments[0]);
  84. return System.Math.Ceiling(x);
  85. }
  86. private static double Cos(object thisObject, object[] arguments)
  87. {
  88. var x = TypeConverter.ToNumber(arguments[0]);
  89. return System.Math.Cos(x);
  90. }
  91. private static double Exp(object thisObject, object[] arguments)
  92. {
  93. var x = TypeConverter.ToNumber(arguments[0]);
  94. return System.Math.Exp(x);
  95. }
  96. private static double Floor(object thisObject, object[] arguments)
  97. {
  98. var x = TypeConverter.ToNumber(arguments[0]);
  99. return System.Math.Floor(x);
  100. }
  101. private static double Log(object thisObject, object[] arguments)
  102. {
  103. var x = TypeConverter.ToNumber(arguments[0]);
  104. return System.Math.Log(x);
  105. }
  106. private static double Max(object thisObject, object[] arguments)
  107. {
  108. double max = TypeConverter.ToNumber(arguments[0]);
  109. for (int i = 0; i < arguments.Length; i++)
  110. {
  111. max = System.Math.Max(max, TypeConverter.ToNumber(arguments[i]));
  112. }
  113. return max;
  114. }
  115. private static double Min(object thisObject, object[] arguments)
  116. {
  117. double min = TypeConverter.ToNumber(arguments[0]);
  118. for (int i = 0; i < arguments.Length; i++)
  119. {
  120. min = System.Math.Min(min, TypeConverter.ToNumber(arguments[i]));
  121. }
  122. return min;
  123. }
  124. private static double Pow(object thisObject, object[] arguments)
  125. {
  126. var x = TypeConverter.ToNumber(arguments[0]);
  127. var y = TypeConverter.ToNumber(arguments[1]);
  128. return System.Math.Pow(x, y);
  129. }
  130. private static double Random(object thisObject, object[] arguments)
  131. {
  132. return new Random().NextDouble();
  133. }
  134. private static double Round(object thisObject, object[] arguments)
  135. {
  136. var x = TypeConverter.ToNumber(arguments[0]);
  137. return System.Math.Round(x);
  138. }
  139. private static double Sin(object thisObject, object[] arguments)
  140. {
  141. var x = TypeConverter.ToNumber(arguments[0]);
  142. return System.Math.Sin(x);
  143. }
  144. private static double Sqrt(object thisObject, object[] arguments)
  145. {
  146. var x = TypeConverter.ToNumber(arguments[0]);
  147. return System.Math.Sqrt(x);
  148. }
  149. private static double Tan(object thisObject, object[] arguments)
  150. {
  151. var x = TypeConverter.ToNumber(arguments[0]);
  152. return System.Math.Tan(x);
  153. }
  154. }
  155. }