MathInstance.cs 6.9 KB

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