MathInstance.cs 6.4 KB

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