MathInstance.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. using System;
  2. using Jint.Native.Number;
  3. using Jint.Native.Object;
  4. using Jint.Runtime;
  5. using Jint.Runtime.Interop;
  6. namespace Jint.Native.Math
  7. {
  8. public sealed class MathInstance : ObjectInstance
  9. {
  10. private static Random _random = new Random();
  11. private MathInstance(Engine engine):base(engine)
  12. {
  13. }
  14. public override string Class
  15. {
  16. get
  17. {
  18. return "Math";
  19. }
  20. }
  21. public static MathInstance CreateMathObject(Engine engine)
  22. {
  23. var math = new MathInstance(engine);
  24. math.Extensible = true;
  25. math.Prototype = engine.Object.PrototypeObject;
  26. return math;
  27. }
  28. public void Configure()
  29. {
  30. FastAddProperty("abs", new ClrFunctionInstance(Engine, Abs), true, false, true);
  31. FastAddProperty("acos", new ClrFunctionInstance(Engine, Acos), true, false, true);
  32. FastAddProperty("asin", new ClrFunctionInstance(Engine, Asin), true, false, true);
  33. FastAddProperty("atan", new ClrFunctionInstance(Engine, Atan), true, false, true);
  34. FastAddProperty("atan2", new ClrFunctionInstance(Engine, Atan2), true, false, true);
  35. FastAddProperty("ceil", new ClrFunctionInstance(Engine, Ceil), true, false, true);
  36. FastAddProperty("cos", new ClrFunctionInstance(Engine, Cos), true, false, true);
  37. FastAddProperty("exp", new ClrFunctionInstance(Engine, Exp), true, false, true);
  38. FastAddProperty("floor", new ClrFunctionInstance(Engine, Floor), true, false, true);
  39. FastAddProperty("log", new ClrFunctionInstance(Engine, Log), true, false, true);
  40. FastAddProperty("max", new ClrFunctionInstance(Engine, Max, 2), true, false, true);
  41. FastAddProperty("min", new ClrFunctionInstance(Engine, Min, 2), true, false, true);
  42. FastAddProperty("pow", new ClrFunctionInstance(Engine, Pow, 2), true, false, true);
  43. FastAddProperty("random", new ClrFunctionInstance(Engine, Random), true, false, true);
  44. FastAddProperty("round", new ClrFunctionInstance(Engine, Round), true, false, true);
  45. FastAddProperty("sin", new ClrFunctionInstance(Engine, Sin), true, false, true);
  46. FastAddProperty("sqrt", new ClrFunctionInstance(Engine, Sqrt), true, false, true);
  47. FastAddProperty("tan", new ClrFunctionInstance(Engine, Tan), true, false, true);
  48. FastAddProperty("E", System.Math.E, false, false, false);
  49. FastAddProperty("LN10", System.Math.Log(10), false, false, false);
  50. FastAddProperty("LN2", System.Math.Log(2), false, false, false);
  51. FastAddProperty("LOG2E", System.Math.Log(System.Math.E, 2), false, false, false);
  52. FastAddProperty("LOG10E", System.Math.Log(System.Math.E, 10), false, false, false);
  53. FastAddProperty("PI", System.Math.PI, false, false, false);
  54. FastAddProperty("SQRT1_2", System.Math.Sqrt(0.5), false, false, false);
  55. FastAddProperty("SQRT2", System.Math.Sqrt(2), false, false, false);
  56. }
  57. private static JsValue Abs(JsValue thisObject, JsValue[] arguments)
  58. {
  59. var x = TypeConverter.ToNumber(arguments.At(0));
  60. return System.Math.Abs(x);
  61. }
  62. private static JsValue Acos(JsValue thisObject, JsValue[] arguments)
  63. {
  64. var x = TypeConverter.ToNumber(arguments.At(0));
  65. return System.Math.Acos(x);
  66. }
  67. private static JsValue Asin(JsValue thisObject, JsValue[] arguments)
  68. {
  69. var x = TypeConverter.ToNumber(arguments.At(0));
  70. return System.Math.Asin(x);
  71. }
  72. private static JsValue Atan(JsValue thisObject, JsValue[] arguments)
  73. {
  74. var x = TypeConverter.ToNumber(arguments.At(0));
  75. return System.Math.Atan(x);
  76. }
  77. private static JsValue Atan2(JsValue thisObject, JsValue[] arguments)
  78. {
  79. var y = TypeConverter.ToNumber(arguments.At(0));
  80. var x = TypeConverter.ToNumber(arguments.At(1));
  81. // If either x or y is NaN, the result is NaN.
  82. if (double.IsNaN(x) || double.IsNaN(y))
  83. {
  84. return double.NaN;
  85. }
  86. if (y > 0 && x.Equals(0))
  87. {
  88. return System.Math.PI/2;
  89. }
  90. if (NumberInstance.IsPositiveZero(y))
  91. {
  92. // If y is +0 and x>0, the result is +0.
  93. if (x > 0)
  94. {
  95. return +0;
  96. }
  97. // If y is +0 and x is +0, the result is +0.
  98. if (NumberInstance.IsPositiveZero(x))
  99. {
  100. return +0;
  101. }
  102. // If y is +0 and x is −0, the result is an implementation-dependent approximation to +π.
  103. if (NumberInstance.IsNegativeZero(x))
  104. {
  105. return System.Math.PI;
  106. }
  107. // If y is +0 and x<0, the result is an implementation-dependent approximation to +π.
  108. if (x < 0)
  109. {
  110. return System.Math.PI;
  111. }
  112. }
  113. if (NumberInstance.IsNegativeZero(y))
  114. {
  115. // If y is −0 and x>0, the result is −0.
  116. if (x > 0)
  117. {
  118. return -0;
  119. }
  120. // If y is −0 and x is +0, the result is −0.
  121. if (NumberInstance.IsPositiveZero(x))
  122. {
  123. return -0;
  124. }
  125. // If y is −0 and x is −0, the result is an implementation-dependent approximation to −π.
  126. if (NumberInstance.IsNegativeZero(x))
  127. {
  128. return -System.Math.PI;
  129. }
  130. // If y is −0 and x<0, the result is an implementation-dependent approximation to −π.
  131. if (x < 0)
  132. {
  133. return -System.Math.PI;
  134. }
  135. }
  136. // If y<0 and x is +0, the result is an implementation-dependent approximation to −π/2.
  137. // If y<0 and x is −0, the result is an implementation-dependent approximation to −π/2.
  138. if (y < 0 && x.Equals(0))
  139. {
  140. return -System.Math.PI/2;
  141. }
  142. // If y>0 and y is finite and x is +∞, the result is +0.
  143. if (y > 0 && !double.IsInfinity(y))
  144. {
  145. if (double.IsPositiveInfinity(x))
  146. {
  147. return +0;
  148. }
  149. // If y>0 and y is finite and x is −∞, the result if an implementation-dependent approximation to +π.
  150. if (double.IsNegativeInfinity(x))
  151. {
  152. return System.Math.PI;
  153. }
  154. }
  155. // If y<0 and y is finite and x is +∞, the result is −0.
  156. // If y<0 and y is finite and x is −∞, the result is an implementation-dependent approximation to −π.
  157. if (y < 0 && !double.IsInfinity(y))
  158. {
  159. if (double.IsPositiveInfinity(x))
  160. {
  161. return -0;
  162. }
  163. // If y>0 and y is finite and x is −∞, the result if an implementation-dependent approximation to +π.
  164. if (double.IsNegativeInfinity(x))
  165. {
  166. return -System.Math.PI;
  167. }
  168. }
  169. // If y is +∞ and x is finite, the result is an implementation-dependent approximation to +π/2.
  170. if (double.IsPositiveInfinity(y) && !double.IsInfinity(x))
  171. {
  172. return System.Math.PI/2;
  173. }
  174. // If y is −∞ and x is finite, the result is an implementation-dependent approximation to −π/2.
  175. if (double.IsNegativeInfinity(y) && !double.IsInfinity(x))
  176. {
  177. return -System.Math.PI / 2;
  178. }
  179. // If y is +∞ and x is +∞, the result is an implementation-dependent approximation to +π/4.
  180. if (double.IsPositiveInfinity(y) && double.IsPositiveInfinity(x))
  181. {
  182. return System.Math.PI/4;
  183. }
  184. // If y is +∞ and x is −∞, the result is an implementation-dependent approximation to +3π/4.
  185. if (double.IsPositiveInfinity(y) && double.IsNegativeInfinity(x))
  186. {
  187. return 3 * System.Math.PI / 4;
  188. }
  189. // If y is −∞ and x is +∞, the result is an implementation-dependent approximation to −π/4.
  190. if (double.IsNegativeInfinity(y) && double.IsPositiveInfinity(x))
  191. {
  192. return -System.Math.PI / 4;
  193. }
  194. // If y is −∞ and x is −∞, the result is an implementation-dependent approximation to −3π/4.
  195. if (double.IsNegativeInfinity(y) && double.IsNegativeInfinity(x))
  196. {
  197. return - 3 * System.Math.PI / 4;
  198. }
  199. return System.Math.Atan2(y, x);
  200. }
  201. private static JsValue Ceil(JsValue thisObject, JsValue[] arguments)
  202. {
  203. var x = TypeConverter.ToNumber(arguments.At(0));
  204. return System.Math.Ceiling(x);
  205. }
  206. private static JsValue Cos(JsValue thisObject, JsValue[] arguments)
  207. {
  208. var x = TypeConverter.ToNumber(arguments.At(0));
  209. return System.Math.Cos(x);
  210. }
  211. private static JsValue Exp(JsValue thisObject, JsValue[] arguments)
  212. {
  213. var x = TypeConverter.ToNumber(arguments.At(0));
  214. return System.Math.Exp(x);
  215. }
  216. private static JsValue Floor(JsValue thisObject, JsValue[] arguments)
  217. {
  218. var x = TypeConverter.ToNumber(arguments.At(0));
  219. return System.Math.Floor(x);
  220. }
  221. private static JsValue Log(JsValue thisObject, JsValue[] arguments)
  222. {
  223. var x = TypeConverter.ToNumber(arguments.At(0));
  224. return System.Math.Log(x);
  225. }
  226. private static JsValue Max(JsValue thisObject, JsValue[] arguments)
  227. {
  228. if (arguments.Length == 0)
  229. {
  230. return Double.NegativeInfinity;
  231. }
  232. double max = TypeConverter.ToNumber(arguments.At(0));
  233. for (int i = 0; i < arguments.Length; i++)
  234. {
  235. max = System.Math.Max(max, TypeConverter.ToNumber(arguments[i]));
  236. }
  237. return max;
  238. }
  239. private static JsValue Min(JsValue thisObject, JsValue[] arguments)
  240. {
  241. if (arguments.Length == 0)
  242. {
  243. return Double.PositiveInfinity;
  244. }
  245. double min = TypeConverter.ToNumber(arguments.At(0));
  246. for (int i = 0; i < arguments.Length; i++)
  247. {
  248. min = System.Math.Min(min, TypeConverter.ToNumber(arguments[i]));
  249. }
  250. return min;
  251. }
  252. private static JsValue Pow(JsValue thisObject, JsValue[] arguments)
  253. {
  254. var x = TypeConverter.ToNumber(arguments.At(0));
  255. var y = TypeConverter.ToNumber(arguments.At(1));
  256. if (double.IsNaN(y))
  257. {
  258. return double.NaN;
  259. }
  260. if (y.Equals(0))
  261. {
  262. return 1;
  263. }
  264. if (double.IsNaN(x) && !y.Equals(0))
  265. {
  266. return double.NaN;
  267. }
  268. if (System.Math.Abs(x) > 1)
  269. {
  270. if (double.IsPositiveInfinity(y))
  271. {
  272. return double.PositiveInfinity;
  273. }
  274. if (double.IsNegativeInfinity(y))
  275. {
  276. return +0;
  277. }
  278. }
  279. if (System.Math.Abs(x).Equals(1))
  280. {
  281. if (double.IsInfinity(y))
  282. {
  283. return double.NaN;
  284. }
  285. }
  286. if (System.Math.Abs(x) < 1)
  287. {
  288. if (double.IsPositiveInfinity(y))
  289. {
  290. return 0;
  291. }
  292. if (double.IsNegativeInfinity(y))
  293. {
  294. return double.PositiveInfinity;
  295. }
  296. }
  297. if (double.IsPositiveInfinity(x))
  298. {
  299. if (y > 0)
  300. {
  301. return double.PositiveInfinity;
  302. }
  303. if (y < 0)
  304. {
  305. return +0;
  306. }
  307. }
  308. if (double.IsNegativeInfinity(x))
  309. {
  310. if (y > 0)
  311. {
  312. if (System.Math.Abs(y % 2).Equals(1))
  313. {
  314. return double.NegativeInfinity;
  315. }
  316. return double.PositiveInfinity;
  317. }
  318. if (y < 0)
  319. {
  320. if (System.Math.Abs(y % 2).Equals(1))
  321. {
  322. return -0;
  323. }
  324. return +0;
  325. }
  326. }
  327. if (NumberInstance.IsPositiveZero(x))
  328. {
  329. // If x is +0 and y>0, the result is +0.
  330. if (y > 0)
  331. {
  332. return 0;
  333. }
  334. // If x is +0 and y<0, the result is +∞.
  335. if (y < 0)
  336. {
  337. return double.PositiveInfinity;
  338. }
  339. }
  340. if (NumberInstance.IsNegativeZero(x))
  341. {
  342. if (y > 0)
  343. {
  344. // If x is −0 and y>0 and y is an odd integer, the result is −0.
  345. if (System.Math.Abs(y % 2).Equals(1))
  346. {
  347. return -0;
  348. }
  349. // If x is −0 and y>0 and y is not an odd integer, the result is +0.
  350. return +0;
  351. }
  352. if (y < 0)
  353. {
  354. // If x is −0 and y<0 and y is an odd integer, the result is −∞.
  355. if (System.Math.Abs(y % 2).Equals(1))
  356. {
  357. return double.NegativeInfinity;
  358. }
  359. // If x is −0 and y<0 and y is not an odd integer, the result is +∞.
  360. return double.PositiveInfinity;
  361. }
  362. }
  363. // If x<0 and x is finite and y is finite and y is not an integer, the result is NaN.
  364. if (x < 0 && !double.IsInfinity(x) && !double.IsInfinity(y) && !y.Equals((int)y))
  365. {
  366. return double.NaN;
  367. }
  368. return System.Math.Pow(x, y);
  369. }
  370. private static JsValue Random(JsValue thisObject, JsValue[] arguments)
  371. {
  372. return _random.NextDouble();
  373. }
  374. private static JsValue Round(JsValue thisObject, JsValue[] arguments)
  375. {
  376. var x = TypeConverter.ToNumber(arguments.At(0));
  377. var round = System.Math.Round(x);
  378. if (round.Equals(x - 0.5))
  379. {
  380. return round + 1;
  381. }
  382. return round;
  383. }
  384. private static JsValue Sin(JsValue thisObject, JsValue[] arguments)
  385. {
  386. var x = TypeConverter.ToNumber(arguments.At(0));
  387. return System.Math.Sin(x);
  388. }
  389. private static JsValue Sqrt(JsValue thisObject, JsValue[] arguments)
  390. {
  391. var x = TypeConverter.ToNumber(arguments.At(0));
  392. return System.Math.Sqrt(x);
  393. }
  394. private static JsValue Tan(JsValue thisObject, JsValue[] arguments)
  395. {
  396. var x = TypeConverter.ToNumber(arguments.At(0));
  397. return System.Math.Tan(x);
  398. }
  399. }
  400. }