MathInstance.cs 23 KB

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