MathInstance.cs 7.2 KB

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