MathInstance.cs 16 KB

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