MathInstance.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. return math;
  44. }
  45. private static double Abs(MathInstance thisObject, object[] arguments)
  46. {
  47. var x = TypeConverter.ToNumber(arguments[0]);
  48. return System.Math.Abs(x);
  49. }
  50. private static double Acos(MathInstance thisObject, object[] arguments)
  51. {
  52. var x = TypeConverter.ToNumber(arguments[0]);
  53. return System.Math.Acos(x);
  54. }
  55. private static double Asin(MathInstance thisObject, object[] arguments)
  56. {
  57. var x = TypeConverter.ToNumber(arguments[0]);
  58. return System.Math.Asin(x);
  59. }
  60. private static double Atan(MathInstance thisObject, object[] arguments)
  61. {
  62. var x = TypeConverter.ToNumber(arguments[0]);
  63. return System.Math.Atan(x);
  64. }
  65. private static double Atan2(MathInstance thisObject, object[] arguments)
  66. {
  67. var x = TypeConverter.ToNumber(arguments[0]);
  68. var y = TypeConverter.ToNumber(arguments[1]);
  69. return System.Math.Atan2(x, y);
  70. }
  71. private static double Ceil(MathInstance thisObject, object[] arguments)
  72. {
  73. var x = TypeConverter.ToNumber(arguments[0]);
  74. return System.Math.Ceiling(x);
  75. }
  76. private static double Cos(MathInstance thisObject, object[] arguments)
  77. {
  78. var x = TypeConverter.ToNumber(arguments[0]);
  79. return System.Math.Cos(x);
  80. }
  81. private static double Exp(MathInstance thisObject, object[] arguments)
  82. {
  83. var x = TypeConverter.ToNumber(arguments[0]);
  84. return System.Math.Exp(x);
  85. }
  86. private static double Floor(MathInstance thisObject, object[] arguments)
  87. {
  88. var x = TypeConverter.ToNumber(arguments[0]);
  89. return System.Math.Floor(x);
  90. }
  91. private static double Log(MathInstance thisObject, object[] arguments)
  92. {
  93. var x = TypeConverter.ToNumber(arguments[0]);
  94. return System.Math.Log(x);
  95. }
  96. private static double Max(MathInstance thisObject, object[] arguments)
  97. {
  98. double max = TypeConverter.ToNumber(arguments[0]);
  99. for (int i = 0; i < arguments.Length; i++)
  100. {
  101. max = System.Math.Max(max, TypeConverter.ToNumber(arguments[i]));
  102. }
  103. return max;
  104. }
  105. private static double Min(MathInstance thisObject, object[] arguments)
  106. {
  107. double min = TypeConverter.ToNumber(arguments[0]);
  108. for (int i = 0; i < arguments.Length; i++)
  109. {
  110. min = System.Math.Min(min, TypeConverter.ToNumber(arguments[i]));
  111. }
  112. return min;
  113. }
  114. private static double Pow(MathInstance thisObject, object[] arguments)
  115. {
  116. var x = TypeConverter.ToNumber(arguments[0]);
  117. var y = TypeConverter.ToNumber(arguments[1]);
  118. return System.Math.Pow(x, y);
  119. }
  120. private static double Random(MathInstance thisObject, object[] arguments)
  121. {
  122. return new Random().NextDouble();
  123. }
  124. private static double Round(MathInstance thisObject, object[] arguments)
  125. {
  126. var x = TypeConverter.ToNumber(arguments[0]);
  127. return System.Math.Round(x);
  128. }
  129. private static double Sin(MathInstance thisObject, object[] arguments)
  130. {
  131. var x = TypeConverter.ToNumber(arguments[0]);
  132. return System.Math.Sin(x);
  133. }
  134. private static double Sqrt(MathInstance thisObject, object[] arguments)
  135. {
  136. var x = TypeConverter.ToNumber(arguments[0]);
  137. return System.Math.Sqrt(x);
  138. }
  139. private static double Tan(MathInstance thisObject, object[] arguments)
  140. {
  141. var x = TypeConverter.ToNumber(arguments[0]);
  142. return System.Math.Tan(x);
  143. }
  144. }
  145. }