MathInstance.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  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 readonly Random _random = new Random();
  11. private MathInstance(Engine engine) : base(engine, "Math")
  12. {
  13. }
  14. public static MathInstance CreateMathObject(Engine engine)
  15. {
  16. var math = new MathInstance(engine);
  17. math.Extensible = true;
  18. math.Prototype = engine.Object.PrototypeObject;
  19. return math;
  20. }
  21. public void Configure()
  22. {
  23. FastAddProperty("abs", new ClrFunctionInstance(Engine, Abs), true, false, true);
  24. FastAddProperty("acos", new ClrFunctionInstance(Engine, Acos), true, false, true);
  25. FastAddProperty("asin", new ClrFunctionInstance(Engine, Asin), true, false, true);
  26. FastAddProperty("atan", new ClrFunctionInstance(Engine, Atan), true, false, true);
  27. FastAddProperty("atan2", new ClrFunctionInstance(Engine, Atan2), true, false, true);
  28. FastAddProperty("ceil", new ClrFunctionInstance(Engine, Ceil), true, false, true);
  29. FastAddProperty("cos", new ClrFunctionInstance(Engine, Cos), true, false, true);
  30. FastAddProperty("exp", new ClrFunctionInstance(Engine, Exp), true, false, true);
  31. FastAddProperty("floor", new ClrFunctionInstance(Engine, Floor), true, false, true);
  32. FastAddProperty("log", new ClrFunctionInstance(Engine, Log), true, false, true);
  33. FastAddProperty("max", new ClrFunctionInstance(Engine, Max, 2), true, false, true);
  34. FastAddProperty("min", new ClrFunctionInstance(Engine, Min, 2), true, false, true);
  35. FastAddProperty("pow", new ClrFunctionInstance(Engine, Pow, 2), true, false, true);
  36. FastAddProperty("random", new ClrFunctionInstance(Engine, Random), true, false, true);
  37. FastAddProperty("round", new ClrFunctionInstance(Engine, Round), true, false, true);
  38. FastAddProperty("sin", new ClrFunctionInstance(Engine, Sin), true, false, true);
  39. FastAddProperty("sqrt", new ClrFunctionInstance(Engine, Sqrt), true, false, true);
  40. FastAddProperty("tan", new ClrFunctionInstance(Engine, Tan), true, false, true);
  41. FastAddProperty("trunc", new ClrFunctionInstance(Engine, Truncate), true, false, true);
  42. FastAddProperty("sign", new ClrFunctionInstance(Engine, Sign), true, false, true);
  43. FastAddProperty("cbrt", new ClrFunctionInstance(Engine, Cbrt), true, false, true);
  44. FastAddProperty("E", System.Math.E, false, false, false);
  45. FastAddProperty("LN10", System.Math.Log(10), false, false, false);
  46. FastAddProperty("LN2", System.Math.Log(2), false, false, false);
  47. FastAddProperty("LOG2E", System.Math.Log(System.Math.E, 2), false, false, false);
  48. FastAddProperty("LOG10E", System.Math.Log(System.Math.E, 10), false, false, false);
  49. FastAddProperty("PI", System.Math.PI, false, false, false);
  50. FastAddProperty("SQRT1_2", System.Math.Sqrt(0.5), false, false, false);
  51. FastAddProperty("SQRT2", System.Math.Sqrt(2), false, false, false);
  52. }
  53. private static JsValue Abs(JsValue thisObject, JsValue[] arguments)
  54. {
  55. var x = TypeConverter.ToNumber(arguments.At(0));
  56. if (double.IsNaN(x))
  57. {
  58. return double.NaN;
  59. }
  60. else if (NumberInstance.IsNegativeZero(x))
  61. {
  62. return +0;
  63. }
  64. else if (double.IsInfinity(x))
  65. {
  66. return double.PositiveInfinity;
  67. }
  68. return System.Math.Abs(x);
  69. }
  70. private static JsValue Acos(JsValue thisObject, JsValue[] arguments)
  71. {
  72. var x = TypeConverter.ToNumber(arguments.At(0));
  73. if (double.IsNaN(x) || (x > 1) || (x < -1))
  74. {
  75. return double.NaN;
  76. }
  77. else if (x == 1)
  78. {
  79. return 0;
  80. }
  81. return System.Math.Acos(x);
  82. }
  83. private static JsValue Asin(JsValue thisObject, JsValue[] arguments)
  84. {
  85. var x = TypeConverter.ToNumber(arguments.At(0));
  86. if (double.IsNaN(x) || (x > 1) || (x < -1))
  87. {
  88. return double.NaN;
  89. }
  90. else if (NumberInstance.IsPositiveZero(x) || NumberInstance.IsNegativeZero(x))
  91. {
  92. return x;
  93. }
  94. return System.Math.Asin(x);
  95. }
  96. private static JsValue Atan(JsValue thisObject, JsValue[] arguments)
  97. {
  98. var x = TypeConverter.ToNumber(arguments.At(0));
  99. if (double.IsNaN(x))
  100. {
  101. return double.NaN;
  102. }
  103. else if (NumberInstance.IsPositiveZero(x) || NumberInstance.IsNegativeZero(x))
  104. {
  105. return x;
  106. }
  107. else if (double.IsPositiveInfinity(x))
  108. {
  109. return System.Math.PI / 2;
  110. }
  111. else if (double.IsNegativeInfinity(x))
  112. {
  113. return -System.Math.PI / 2;
  114. }
  115. return System.Math.Atan(x);
  116. }
  117. private static JsValue Atan2(JsValue thisObject, JsValue[] arguments)
  118. {
  119. var y = TypeConverter.ToNumber(arguments.At(0));
  120. var x = TypeConverter.ToNumber(arguments.At(1));
  121. // If either x or y is NaN, the result is NaN.
  122. if (double.IsNaN(x) || double.IsNaN(y))
  123. {
  124. return double.NaN;
  125. }
  126. if (y > 0 && x.Equals(0))
  127. {
  128. return System.Math.PI/2;
  129. }
  130. if (NumberInstance.IsPositiveZero(y))
  131. {
  132. // If y is +0 and x>0, the result is +0.
  133. if (x > 0)
  134. {
  135. return +0;
  136. }
  137. // If y is +0 and x is +0, the result is +0.
  138. if (NumberInstance.IsPositiveZero(x))
  139. {
  140. return +0;
  141. }
  142. // If y is +0 and x is −0, the result is an implementation-dependent approximation to +π.
  143. if (NumberInstance.IsNegativeZero(x))
  144. {
  145. return System.Math.PI;
  146. }
  147. // If y is +0 and x<0, the result is an implementation-dependent approximation to +π.
  148. if (x < 0)
  149. {
  150. return System.Math.PI;
  151. }
  152. }
  153. if (NumberInstance.IsNegativeZero(y))
  154. {
  155. // If y is −0 and x>0, the result is −0.
  156. if (x > 0)
  157. {
  158. return -0;
  159. }
  160. // If y is −0 and x is +0, the result is −0.
  161. if (NumberInstance.IsPositiveZero(x))
  162. {
  163. return -0;
  164. }
  165. // If y is −0 and x is −0, the result is an implementation-dependent approximation to −π.
  166. if (NumberInstance.IsNegativeZero(x))
  167. {
  168. return -System.Math.PI;
  169. }
  170. // If y is −0 and x<0, the result is an implementation-dependent approximation to −π.
  171. if (x < 0)
  172. {
  173. return -System.Math.PI;
  174. }
  175. }
  176. // If y<0 and x is +0, the result is an implementation-dependent approximation to −π/2.
  177. // If y<0 and x is −0, the result is an implementation-dependent approximation to −π/2.
  178. if (y < 0 && x.Equals(0))
  179. {
  180. return -System.Math.PI/2;
  181. }
  182. // If y>0 and y is finite and x is +∞, the result is +0.
  183. if (y > 0 && !double.IsInfinity(y))
  184. {
  185. if (double.IsPositiveInfinity(x))
  186. {
  187. return +0;
  188. }
  189. // If y>0 and y is finite and x is −∞, the result if an implementation-dependent approximation to +π.
  190. if (double.IsNegativeInfinity(x))
  191. {
  192. return System.Math.PI;
  193. }
  194. }
  195. // If y<0 and y is finite and x is +∞, the result is −0.
  196. // If y<0 and y is finite and x is −∞, the result is an implementation-dependent approximation to −π.
  197. if (y < 0 && !double.IsInfinity(y))
  198. {
  199. if (double.IsPositiveInfinity(x))
  200. {
  201. return -0;
  202. }
  203. // If y>0 and y is finite and x is −∞, the result if an implementation-dependent approximation to +π.
  204. if (double.IsNegativeInfinity(x))
  205. {
  206. return -System.Math.PI;
  207. }
  208. }
  209. // If y is +∞ and x is finite, the result is an implementation-dependent approximation to +π/2.
  210. if (double.IsPositiveInfinity(y) && !double.IsInfinity(x))
  211. {
  212. return System.Math.PI/2;
  213. }
  214. // If y is −∞ and x is finite, the result is an implementation-dependent approximation to −π/2.
  215. if (double.IsNegativeInfinity(y) && !double.IsInfinity(x))
  216. {
  217. return -System.Math.PI / 2;
  218. }
  219. // If y is +∞ and x is +∞, the result is an implementation-dependent approximation to +π/4.
  220. if (double.IsPositiveInfinity(y) && double.IsPositiveInfinity(x))
  221. {
  222. return System.Math.PI/4;
  223. }
  224. // If y is +∞ and x is −∞, the result is an implementation-dependent approximation to +3π/4.
  225. if (double.IsPositiveInfinity(y) && double.IsNegativeInfinity(x))
  226. {
  227. return 3 * System.Math.PI / 4;
  228. }
  229. // If y is −∞ and x is +∞, the result is an implementation-dependent approximation to −π/4.
  230. if (double.IsNegativeInfinity(y) && double.IsPositiveInfinity(x))
  231. {
  232. return -System.Math.PI / 4;
  233. }
  234. // If y is −∞ and x is −∞, the result is an implementation-dependent approximation to −3π/4.
  235. if (double.IsNegativeInfinity(y) && double.IsNegativeInfinity(x))
  236. {
  237. return - 3 * System.Math.PI / 4;
  238. }
  239. return System.Math.Atan2(y, x);
  240. }
  241. private static JsValue Ceil(JsValue thisObject, JsValue[] arguments)
  242. {
  243. var x = TypeConverter.ToNumber(arguments.At(0));
  244. if (double.IsNaN(x))
  245. {
  246. return double.NaN;
  247. }
  248. else if (NumberInstance.IsPositiveZero(x))
  249. {
  250. return +0;
  251. }
  252. else if (NumberInstance.IsNegativeZero(x))
  253. {
  254. return -0;
  255. }
  256. else if (double.IsPositiveInfinity(x))
  257. {
  258. return double.PositiveInfinity;
  259. }
  260. else if (double.IsNegativeInfinity(x))
  261. {
  262. return double.NegativeInfinity;
  263. }
  264. return System.Math.Ceiling(x);
  265. }
  266. private static JsValue Cos(JsValue thisObject, JsValue[] arguments)
  267. {
  268. var x = TypeConverter.ToNumber(arguments.At(0));
  269. if (double.IsNaN(x))
  270. {
  271. return double.NaN;
  272. }
  273. else if (NumberInstance.IsPositiveZero(x))
  274. {
  275. return 1;
  276. }
  277. else if (NumberInstance.IsNegativeZero(x))
  278. {
  279. return 1;
  280. }
  281. else if (double.IsInfinity(x))
  282. {
  283. return double.NaN;
  284. }
  285. return System.Math.Cos(x);
  286. }
  287. private static JsValue Exp(JsValue thisObject, JsValue[] arguments)
  288. {
  289. var x = TypeConverter.ToNumber(arguments.At(0));
  290. if (double.IsNaN(x))
  291. {
  292. return double.NaN;
  293. }
  294. else if (NumberInstance.IsPositiveZero(x) || NumberInstance.IsNegativeZero(x))
  295. {
  296. return 1;
  297. }
  298. else if (double.IsPositiveInfinity(x))
  299. {
  300. return double.PositiveInfinity;
  301. }
  302. else if (double.IsNegativeInfinity(x))
  303. {
  304. return +0;
  305. }
  306. return System.Math.Exp(x);
  307. }
  308. private static JsValue Floor(JsValue thisObject, JsValue[] arguments)
  309. {
  310. var x = TypeConverter.ToNumber(arguments.At(0));
  311. if (double.IsNaN(x))
  312. {
  313. return double.NaN;
  314. }
  315. else if (NumberInstance.IsPositiveZero(x))
  316. {
  317. return +0;
  318. }
  319. else if (NumberInstance.IsNegativeZero(x))
  320. {
  321. return -0;
  322. }
  323. else if (double.IsPositiveInfinity(x))
  324. {
  325. return double.PositiveInfinity;
  326. }
  327. else if (double.IsNegativeInfinity(x))
  328. {
  329. return double.NegativeInfinity;
  330. }
  331. return System.Math.Floor(x);
  332. }
  333. private static JsValue Log(JsValue thisObject, JsValue[] arguments)
  334. {
  335. var x = TypeConverter.ToNumber(arguments.At(0));
  336. if (double.IsNaN(x))
  337. {
  338. return double.NaN;
  339. }
  340. if (x < 0)
  341. {
  342. return double.NaN;
  343. }
  344. else if (x == 0)
  345. {
  346. return double.NegativeInfinity;
  347. }
  348. else if (double.IsPositiveInfinity(x))
  349. {
  350. return double.PositiveInfinity;
  351. }
  352. else if (x == 1)
  353. {
  354. return +0;
  355. }
  356. return System.Math.Log(x);
  357. }
  358. private static JsValue Max(JsValue thisObject, JsValue[] arguments)
  359. {
  360. if (arguments.Length == 0)
  361. {
  362. return Double.NegativeInfinity;
  363. }
  364. double max = TypeConverter.ToNumber(arguments.At(0));
  365. if (double.IsNaN(max))
  366. {
  367. return double.NaN;
  368. }
  369. for (int i = 0; i < arguments.Length; i++)
  370. {
  371. var value = TypeConverter.ToNumber(arguments[i]);
  372. if (double.IsNaN(value))
  373. {
  374. return double.NaN;
  375. }
  376. max = System.Math.Max(max, value);
  377. }
  378. return max;
  379. }
  380. private static JsValue Min(JsValue thisObject, JsValue[] arguments)
  381. {
  382. if (arguments.Length == 0)
  383. {
  384. return Double.PositiveInfinity;
  385. }
  386. double min = TypeConverter.ToNumber(arguments.At(0));
  387. for (int i = 0; i < arguments.Length; i++)
  388. {
  389. min = System.Math.Min(min, TypeConverter.ToNumber(arguments[i]));
  390. }
  391. return min;
  392. }
  393. private static JsValue Pow(JsValue thisObject, JsValue[] arguments)
  394. {
  395. var x = TypeConverter.ToNumber(arguments.At(0));
  396. var y = TypeConverter.ToNumber(arguments.At(1));
  397. if (double.IsNaN(y))
  398. {
  399. return double.NaN;
  400. }
  401. if (y.Equals(0))
  402. {
  403. return 1;
  404. }
  405. if (double.IsNaN(x) && !y.Equals(0))
  406. {
  407. return double.NaN;
  408. }
  409. if (System.Math.Abs(x) > 1)
  410. {
  411. if (double.IsPositiveInfinity(y))
  412. {
  413. return double.PositiveInfinity;
  414. }
  415. if (double.IsNegativeInfinity(y))
  416. {
  417. return +0;
  418. }
  419. }
  420. if (System.Math.Abs(x).Equals(1))
  421. {
  422. if (double.IsInfinity(y))
  423. {
  424. return double.NaN;
  425. }
  426. }
  427. if (System.Math.Abs(x) < 1)
  428. {
  429. if (double.IsPositiveInfinity(y))
  430. {
  431. return 0;
  432. }
  433. if (double.IsNegativeInfinity(y))
  434. {
  435. return double.PositiveInfinity;
  436. }
  437. }
  438. if (double.IsPositiveInfinity(x))
  439. {
  440. if (y > 0)
  441. {
  442. return double.PositiveInfinity;
  443. }
  444. if (y < 0)
  445. {
  446. return +0;
  447. }
  448. }
  449. if (double.IsNegativeInfinity(x))
  450. {
  451. if (y > 0)
  452. {
  453. if (System.Math.Abs(y % 2).Equals(1))
  454. {
  455. return double.NegativeInfinity;
  456. }
  457. return double.PositiveInfinity;
  458. }
  459. if (y < 0)
  460. {
  461. if (System.Math.Abs(y % 2).Equals(1))
  462. {
  463. return -0;
  464. }
  465. return +0;
  466. }
  467. }
  468. if (NumberInstance.IsPositiveZero(x))
  469. {
  470. // If x is +0 and y>0, the result is +0.
  471. if (y > 0)
  472. {
  473. return 0;
  474. }
  475. // If x is +0 and y<0, the result is +∞.
  476. if (y < 0)
  477. {
  478. return double.PositiveInfinity;
  479. }
  480. }
  481. if (NumberInstance.IsNegativeZero(x))
  482. {
  483. if (y > 0)
  484. {
  485. // If x is −0 and y>0 and y is an odd integer, the result is −0.
  486. if (System.Math.Abs(y % 2).Equals(1))
  487. {
  488. return -0;
  489. }
  490. // If x is −0 and y>0 and y is not an odd integer, the result is +0.
  491. return +0;
  492. }
  493. if (y < 0)
  494. {
  495. // If x is −0 and y<0 and y is an odd integer, the result is −∞.
  496. if (System.Math.Abs(y % 2).Equals(1))
  497. {
  498. return double.NegativeInfinity;
  499. }
  500. // If x is −0 and y<0 and y is not an odd integer, the result is +∞.
  501. return double.PositiveInfinity;
  502. }
  503. }
  504. // If x<0 and x is finite and y is finite and y is not an integer, the result is NaN.
  505. if (x < 0 && !double.IsInfinity(x) && !double.IsInfinity(y) && !y.Equals((int)y))
  506. {
  507. return double.NaN;
  508. }
  509. return System.Math.Pow(x, y);
  510. }
  511. private static JsValue Random(JsValue thisObject, JsValue[] arguments)
  512. {
  513. return _random.NextDouble();
  514. }
  515. private static JsValue Round(JsValue thisObject, JsValue[] arguments)
  516. {
  517. var x = TypeConverter.ToNumber(arguments.At(0));
  518. var round = System.Math.Round(x);
  519. if (round.Equals(x - 0.5))
  520. {
  521. return round + 1;
  522. }
  523. return round;
  524. }
  525. private static JsValue Sin(JsValue thisObject, JsValue[] arguments)
  526. {
  527. var x = TypeConverter.ToNumber(arguments.At(0));
  528. if (double.IsNaN(x))
  529. {
  530. return double.NaN;
  531. }
  532. else if (NumberInstance.IsPositiveZero(x))
  533. {
  534. return +0;
  535. }
  536. else if (NumberInstance.IsNegativeZero(x))
  537. {
  538. return -0;
  539. }
  540. else if (double.IsInfinity(x))
  541. {
  542. return double.NaN;
  543. }
  544. return System.Math.Sin(x);
  545. }
  546. private static JsValue Sqrt(JsValue thisObject, JsValue[] arguments)
  547. {
  548. var x = TypeConverter.ToNumber(arguments.At(0));
  549. return System.Math.Sqrt(x);
  550. }
  551. private static JsValue Tan(JsValue thisObject, JsValue[] arguments)
  552. {
  553. var x = TypeConverter.ToNumber(arguments.At(0));
  554. return System.Math.Tan(x);
  555. }
  556. private static JsValue Truncate(JsValue thisObject, JsValue[] arguments)
  557. {
  558. var x = TypeConverter.ToNumber(arguments.At(0));
  559. if (double.IsNaN(x))
  560. {
  561. return double.NaN;
  562. }
  563. if (NumberInstance.IsPositiveZero(x) || NumberInstance.IsNegativeZero(x))
  564. {
  565. return x;
  566. }
  567. if (double.IsPositiveInfinity(x))
  568. {
  569. return double.PositiveInfinity;
  570. }
  571. if (double.IsNegativeInfinity(x))
  572. {
  573. return double.NegativeInfinity;
  574. }
  575. return System.Math.Truncate(x);
  576. }
  577. private static JsValue Sign(JsValue thisObject, JsValue[] arguments)
  578. {
  579. var x = TypeConverter.ToNumber(arguments.At(0));
  580. if (double.IsNaN(x))
  581. {
  582. return double.NaN;
  583. }
  584. if (NumberInstance.IsPositiveZero(x) || NumberInstance.IsNegativeZero(x))
  585. {
  586. return x;
  587. }
  588. if (double.IsPositiveInfinity(x))
  589. {
  590. return 1;
  591. }
  592. if (double.IsNegativeInfinity(x))
  593. {
  594. return -1;
  595. }
  596. return System.Math.Sign(x);
  597. }
  598. private static JsValue Cbrt(JsValue thisObject, JsValue[] arguments)
  599. {
  600. var x = TypeConverter.ToNumber(arguments.At(0));
  601. if (double.IsNaN(x))
  602. {
  603. return double.NaN;
  604. }
  605. else if (NumberInstance.IsPositiveZero(x) || NumberInstance.IsNegativeZero(x))
  606. {
  607. return x;
  608. }
  609. else if (double.IsPositiveInfinity(x))
  610. {
  611. return double.PositiveInfinity;
  612. }
  613. else if (double.IsNegativeInfinity(x))
  614. {
  615. return double.NegativeInfinity;
  616. }
  617. if (System.Math.Sign(x) >= 0)
  618. {
  619. return System.Math.Pow(x, 1.0/3.0);
  620. }
  621. return -1 * System.Math.Pow(System.Math.Abs(x), 1.0 / 3.0);
  622. }
  623. }
  624. }