MathInstance.cs 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  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 Random _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, 1, PropertyFlag.Configurable), true, false, true);
  25. FastAddProperty("acos", new ClrFunctionInstance(Engine, "acos", Acos, 1, PropertyFlag.Configurable), true, false, true);
  26. FastAddProperty("acosh", new ClrFunctionInstance(Engine, "acosh", Acosh, 1, PropertyFlag.Configurable), true, false, true);
  27. FastAddProperty("asin", new ClrFunctionInstance(Engine, "asin", Asin, 1, PropertyFlag.Configurable), true, false, true);
  28. FastAddProperty("asinh", new ClrFunctionInstance(Engine, "asinh", Asinh, 1, PropertyFlag.Configurable), true, false, true);
  29. FastAddProperty("atan", new ClrFunctionInstance(Engine, "atan", Atan, 1, PropertyFlag.Configurable), true, false, true);
  30. FastAddProperty("atanh", new ClrFunctionInstance(Engine, "atanh", Atanh, 1, PropertyFlag.Configurable), true, false, true);
  31. FastAddProperty("atan2", new ClrFunctionInstance(Engine, "atan2", Atan2, 2, PropertyFlag.Configurable), true, false, true);
  32. FastAddProperty("ceil", new ClrFunctionInstance(Engine, "ceil", Ceil, 1, PropertyFlag.Configurable), true, false, true);
  33. FastAddProperty("cos", new ClrFunctionInstance(Engine, "cos", Cos, 1, PropertyFlag.Configurable), true, false, true);
  34. FastAddProperty("cosh", new ClrFunctionInstance(Engine, "cosh", Cosh, 1, PropertyFlag.Configurable), true, false, true);
  35. FastAddProperty("exp", new ClrFunctionInstance(Engine, "exp", Exp, 1, PropertyFlag.Configurable), true, false, true);
  36. FastAddProperty("expm1", new ClrFunctionInstance(Engine, "expm1", Expm1, 1, PropertyFlag.Configurable), true, false, true);
  37. FastAddProperty("floor", new ClrFunctionInstance(Engine, "floor", Floor, 1, PropertyFlag.Configurable), true, false, true);
  38. FastAddProperty("log", new ClrFunctionInstance(Engine, "log", Log, 1, PropertyFlag.Configurable), true, false, true);
  39. FastAddProperty("log1p", new ClrFunctionInstance(Engine, "log1p", Log1p, 1, PropertyFlag.Configurable), true, false, true);
  40. FastAddProperty("log2", new ClrFunctionInstance(Engine, "log2", Log2, 1, PropertyFlag.Configurable), true, false, true);
  41. FastAddProperty("log10", new ClrFunctionInstance(Engine, "log10", Log10, 1, PropertyFlag.Configurable), true, false, true);
  42. FastAddProperty("max", new ClrFunctionInstance(Engine, "max", Max, 2, PropertyFlag.Configurable), true, false, true);
  43. FastAddProperty("min", new ClrFunctionInstance(Engine, "min", Min, 2, PropertyFlag.Configurable), true, false, true);
  44. FastAddProperty("pow", new ClrFunctionInstance(Engine, "pow", Pow, 2, PropertyFlag.Configurable), true, false, true);
  45. FastAddProperty("random", new ClrFunctionInstance(Engine, "random", Random, 0, PropertyFlag.Configurable), true, false, true);
  46. FastAddProperty("round", new ClrFunctionInstance(Engine, "round", Round, 1, PropertyFlag.Configurable), true, false, true);
  47. FastAddProperty("fround", new ClrFunctionInstance(Engine, "fround", Fround, 1, PropertyFlag.Configurable), true, false, true);
  48. FastAddProperty("sin", new ClrFunctionInstance(Engine, "sin", Sin, 1, PropertyFlag.Configurable), true, false, true);
  49. FastAddProperty("sinh", new ClrFunctionInstance(Engine, "sinh", Sinh, 1, PropertyFlag.Configurable), true, false, true);
  50. FastAddProperty("sqrt", new ClrFunctionInstance(Engine, "sqrt", Sqrt, 1, PropertyFlag.Configurable), true, false, true);
  51. FastAddProperty("tan", new ClrFunctionInstance(Engine, "tan", Tan, 1, PropertyFlag.Configurable), true, false, true);
  52. FastAddProperty("tanh", new ClrFunctionInstance(Engine, "tanh", Tanh, 1, PropertyFlag.Configurable), true, false, true);
  53. FastAddProperty("trunc", new ClrFunctionInstance(Engine, "trunc", Truncate, 1, PropertyFlag.Configurable), true, false, true);
  54. FastAddProperty("sign", new ClrFunctionInstance(Engine, "sign", Sign, 1, PropertyFlag.Configurable), true, false, true);
  55. FastAddProperty("cbrt", new ClrFunctionInstance(Engine, "cbrt", Cbrt, 1, PropertyFlag.Configurable), true, false, true);
  56. FastAddProperty("hypot", new ClrFunctionInstance(Engine, "hypot", Hypot, 2, PropertyFlag.Configurable), true, false, true);
  57. FastAddProperty("imul", new ClrFunctionInstance(Engine, "imul", Imul, 2, PropertyFlag.Configurable), true, false, true);
  58. FastAddProperty("clz32", new ClrFunctionInstance(Engine, "clz32", Clz32, 1, PropertyFlag.Configurable), true, false, true);
  59. FastAddProperty("E", System.Math.E, false, false, false);
  60. FastAddProperty("LN10", System.Math.Log(10), false, false, false);
  61. FastAddProperty("LN2", System.Math.Log(2), false, false, false);
  62. FastAddProperty("LOG2E", System.Math.Log(System.Math.E, 2), false, false, false);
  63. FastAddProperty("LOG10E", System.Math.Log(System.Math.E, 10), false, false, false);
  64. FastAddProperty("PI", System.Math.PI, false, false, false);
  65. FastAddProperty("SQRT1_2", System.Math.Sqrt(0.5), false, false, false);
  66. FastAddProperty("SQRT2", System.Math.Sqrt(2), false, false, false);
  67. }
  68. private static JsValue Abs(JsValue thisObject, JsValue[] arguments)
  69. {
  70. var x = TypeConverter.ToNumber(arguments.At(0));
  71. if (double.IsNaN(x))
  72. {
  73. return JsNumber.DoubleNaN;
  74. }
  75. else if (NumberInstance.IsNegativeZero(x))
  76. {
  77. return JsNumber.PositiveZero;
  78. }
  79. else if (double.IsInfinity(x))
  80. {
  81. return JsNumber.DoublePositiveInfinity;
  82. }
  83. return System.Math.Abs(x);
  84. }
  85. private static JsValue Acos(JsValue thisObject, JsValue[] arguments)
  86. {
  87. var x = TypeConverter.ToNumber(arguments.At(0));
  88. if (double.IsNaN(x) || (x > 1) || (x < -1))
  89. {
  90. return JsNumber.DoubleNaN;
  91. }
  92. else if (x == 1)
  93. {
  94. return 0;
  95. }
  96. return System.Math.Acos(x);
  97. }
  98. private static JsValue Acosh(JsValue thisObject, JsValue[] arguments)
  99. {
  100. var x = TypeConverter.ToNumber(arguments.At(0));
  101. if (double.IsNaN(x) || x < 1)
  102. {
  103. return JsNumber.DoubleNaN;
  104. }
  105. return System.Math.Log(x + System.Math.Sqrt(x * x - 1.0));
  106. }
  107. private static JsValue Asin(JsValue thisObject, JsValue[] arguments)
  108. {
  109. var x = TypeConverter.ToNumber(arguments.At(0));
  110. if (double.IsNaN(x) || (x > 1) || (x < -1))
  111. {
  112. return JsNumber.DoubleNaN;
  113. }
  114. else if (NumberInstance.IsPositiveZero(x) || NumberInstance.IsNegativeZero(x))
  115. {
  116. return x;
  117. }
  118. return System.Math.Asin(x);
  119. }
  120. private static JsValue Asinh(JsValue thisObject, JsValue[] arguments)
  121. {
  122. var x = TypeConverter.ToNumber(arguments.At(0));
  123. if (double.IsInfinity(x) || NumberInstance.IsPositiveZero(x) || NumberInstance.IsNegativeZero(x))
  124. {
  125. return x;
  126. }
  127. return System.Math.Log(x + System.Math.Sqrt(x * x + 1.0));
  128. }
  129. private static JsValue Atan(JsValue thisObject, JsValue[] arguments)
  130. {
  131. var x = TypeConverter.ToNumber(arguments.At(0));
  132. if (double.IsNaN(x))
  133. {
  134. return JsNumber.DoubleNaN;
  135. }
  136. else if (NumberInstance.IsPositiveZero(x) || NumberInstance.IsNegativeZero(x))
  137. {
  138. return x;
  139. }
  140. else if (double.IsPositiveInfinity(x))
  141. {
  142. return System.Math.PI / 2;
  143. }
  144. else if (double.IsNegativeInfinity(x))
  145. {
  146. return -System.Math.PI / 2;
  147. }
  148. return System.Math.Atan(x);
  149. }
  150. private static JsValue Atanh(JsValue thisObject, JsValue[] arguments)
  151. {
  152. var x = TypeConverter.ToNumber(arguments.At(0));
  153. if (double.IsNaN(x))
  154. {
  155. return JsNumber.DoubleNaN;
  156. }
  157. if (NumberInstance.IsPositiveZero(x) || NumberInstance.IsNegativeZero(x))
  158. {
  159. return x;
  160. }
  161. return 0.5 * System.Math.Log((1.0 + x) / (1.0 - x));
  162. }
  163. private static JsValue Atan2(JsValue thisObject, JsValue[] arguments)
  164. {
  165. var y = TypeConverter.ToNumber(arguments.At(0));
  166. var x = TypeConverter.ToNumber(arguments.At(1));
  167. // If either x or y is NaN, the result is NaN.
  168. if (double.IsNaN(x) || double.IsNaN(y))
  169. {
  170. return JsNumber.DoubleNaN;
  171. }
  172. if (y > 0 && x.Equals(0))
  173. {
  174. return System.Math.PI/2;
  175. }
  176. if (NumberInstance.IsPositiveZero(y))
  177. {
  178. // If y is +0 and x>0, the result is +0.
  179. if (x > 0)
  180. {
  181. return JsNumber.PositiveZero;
  182. }
  183. // If y is +0 and x is +0, the result is +0.
  184. if (NumberInstance.IsPositiveZero(x))
  185. {
  186. return JsNumber.PositiveZero;
  187. }
  188. // If y is +0 and x is −0, the result is an implementation-dependent approximation to +π.
  189. if (NumberInstance.IsNegativeZero(x))
  190. {
  191. return JsNumber.PI;
  192. }
  193. // If y is +0 and x<0, the result is an implementation-dependent approximation to +π.
  194. if (x < 0)
  195. {
  196. return JsNumber.PI;
  197. }
  198. }
  199. if (NumberInstance.IsNegativeZero(y))
  200. {
  201. // If y is −0 and x>0, the result is −0.
  202. if (x > 0)
  203. {
  204. return JsNumber.NegativeZero;
  205. }
  206. // If y is −0 and x is +0, the result is −0.
  207. if (NumberInstance.IsPositiveZero(x))
  208. {
  209. return JsNumber.NegativeZero;
  210. }
  211. // If y is −0 and x is −0, the result is an implementation-dependent approximation to −π.
  212. if (NumberInstance.IsNegativeZero(x))
  213. {
  214. return -System.Math.PI;
  215. }
  216. // If y is −0 and x<0, the result is an implementation-dependent approximation to −π.
  217. if (x < 0)
  218. {
  219. return -System.Math.PI;
  220. }
  221. }
  222. // If y<0 and x is +0, the result is an implementation-dependent approximation to −π/2.
  223. // If y<0 and x is −0, the result is an implementation-dependent approximation to −π/2.
  224. if (y < 0 && x.Equals(0))
  225. {
  226. return -System.Math.PI/2;
  227. }
  228. // If y>0 and y is finite and x is +∞, the result is +0.
  229. if (y > 0 && !double.IsInfinity(y))
  230. {
  231. if (double.IsPositiveInfinity(x))
  232. {
  233. return JsNumber.PositiveZero;
  234. }
  235. // If y>0 and y is finite and x is −∞, the result if an implementation-dependent approximation to +π.
  236. if (double.IsNegativeInfinity(x))
  237. {
  238. return JsNumber.PI;
  239. }
  240. }
  241. // If y<0 and y is finite and x is +∞, the result is −0.
  242. // If y<0 and y is finite and x is −∞, the result is an implementation-dependent approximation to −π.
  243. if (y < 0 && !double.IsInfinity(y))
  244. {
  245. if (double.IsPositiveInfinity(x))
  246. {
  247. return JsNumber.NegativeZero;
  248. }
  249. // If y>0 and y is finite and x is −∞, the result if an implementation-dependent approximation to +π.
  250. if (double.IsNegativeInfinity(x))
  251. {
  252. return -System.Math.PI;
  253. }
  254. }
  255. // If y is +∞ and x is finite, the result is an implementation-dependent approximation to +π/2.
  256. if (double.IsPositiveInfinity(y) && !double.IsInfinity(x))
  257. {
  258. return System.Math.PI/2;
  259. }
  260. // If y is −∞ and x is finite, the result is an implementation-dependent approximation to −π/2.
  261. if (double.IsNegativeInfinity(y) && !double.IsInfinity(x))
  262. {
  263. return -System.Math.PI / 2;
  264. }
  265. // If y is +∞ and x is +∞, the result is an implementation-dependent approximation to +π/4.
  266. if (double.IsPositiveInfinity(y) && double.IsPositiveInfinity(x))
  267. {
  268. return System.Math.PI/4;
  269. }
  270. // If y is +∞ and x is −∞, the result is an implementation-dependent approximation to +3π/4.
  271. if (double.IsPositiveInfinity(y) && double.IsNegativeInfinity(x))
  272. {
  273. return 3 * System.Math.PI / 4;
  274. }
  275. // If y is −∞ and x is +∞, the result is an implementation-dependent approximation to −π/4.
  276. if (double.IsNegativeInfinity(y) && double.IsPositiveInfinity(x))
  277. {
  278. return -System.Math.PI / 4;
  279. }
  280. // If y is −∞ and x is −∞, the result is an implementation-dependent approximation to −3π/4.
  281. if (double.IsNegativeInfinity(y) && double.IsNegativeInfinity(x))
  282. {
  283. return - 3 * System.Math.PI / 4;
  284. }
  285. return System.Math.Atan2(y, x);
  286. }
  287. private static JsValue Ceil(JsValue thisObject, JsValue[] arguments)
  288. {
  289. var x = TypeConverter.ToNumber(arguments.At(0));
  290. if (double.IsNaN(x))
  291. {
  292. return JsNumber.DoubleNaN;
  293. }
  294. else if (NumberInstance.IsPositiveZero(x))
  295. {
  296. return JsNumber.PositiveZero;
  297. }
  298. else if (NumberInstance.IsNegativeZero(x))
  299. {
  300. return JsNumber.NegativeZero;
  301. }
  302. else if (double.IsPositiveInfinity(x))
  303. {
  304. return JsNumber.DoublePositiveInfinity;
  305. }
  306. else if (double.IsNegativeInfinity(x))
  307. {
  308. return JsNumber.DoubleNegativeInfinity;
  309. }
  310. return System.Math.Ceiling(x);
  311. }
  312. private static JsValue Cos(JsValue thisObject, JsValue[] arguments)
  313. {
  314. var x = TypeConverter.ToNumber(arguments.At(0));
  315. if (double.IsNaN(x))
  316. {
  317. return JsNumber.DoubleNaN;
  318. }
  319. else if (NumberInstance.IsPositiveZero(x))
  320. {
  321. return 1;
  322. }
  323. else if (NumberInstance.IsNegativeZero(x))
  324. {
  325. return 1;
  326. }
  327. else if (double.IsInfinity(x))
  328. {
  329. return JsNumber.DoubleNaN;
  330. }
  331. return System.Math.Cos(x);
  332. }
  333. private static JsValue Cosh(JsValue thisObject, JsValue[] arguments)
  334. {
  335. var x = TypeConverter.ToNumber(arguments.At(0));
  336. if (double.IsNaN(x))
  337. {
  338. return JsNumber.DoubleNaN;
  339. }
  340. else if (NumberInstance.IsPositiveZero(x))
  341. {
  342. return 1;
  343. }
  344. else if (NumberInstance.IsNegativeZero(x))
  345. {
  346. return 1;
  347. }
  348. else if (double.IsInfinity(x))
  349. {
  350. return JsNumber.DoublePositiveInfinity;
  351. }
  352. return System.Math.Cosh(x);
  353. }
  354. private static JsValue Exp(JsValue thisObject, JsValue[] arguments)
  355. {
  356. var x = TypeConverter.ToNumber(arguments.At(0));
  357. if (double.IsNaN(x))
  358. {
  359. return JsNumber.DoubleNaN;
  360. }
  361. else if (NumberInstance.IsPositiveZero(x) || NumberInstance.IsNegativeZero(x))
  362. {
  363. return 1;
  364. }
  365. else if (double.IsPositiveInfinity(x))
  366. {
  367. return JsNumber.DoublePositiveInfinity;
  368. }
  369. else if (double.IsNegativeInfinity(x))
  370. {
  371. return JsNumber.PositiveZero;
  372. }
  373. return System.Math.Exp(x);
  374. }
  375. private static JsValue Expm1(JsValue thisObject, JsValue[] arguments)
  376. {
  377. var x = TypeConverter.ToNumber(arguments.At(0));
  378. if (double.IsNaN(x) || NumberInstance.IsPositiveZero(x) || NumberInstance.IsNegativeZero(x) || double.IsPositiveInfinity(x))
  379. {
  380. return arguments.At(0);
  381. }
  382. if (double.IsNegativeInfinity(x))
  383. {
  384. return JsNumber.DoubleNegativeOne;
  385. }
  386. return System.Math.Exp(x) - 1.0;
  387. }
  388. private static JsValue Floor(JsValue thisObject, JsValue[] arguments)
  389. {
  390. var x = TypeConverter.ToNumber(arguments.At(0));
  391. if (double.IsNaN(x))
  392. {
  393. return JsNumber.DoubleNaN;
  394. }
  395. else if (NumberInstance.IsPositiveZero(x))
  396. {
  397. return JsNumber.PositiveZero;
  398. }
  399. else if (NumberInstance.IsNegativeZero(x))
  400. {
  401. return JsNumber.NegativeZero;
  402. }
  403. else if (double.IsPositiveInfinity(x))
  404. {
  405. return JsNumber.DoublePositiveInfinity;
  406. }
  407. else if (double.IsNegativeInfinity(x))
  408. {
  409. return JsNumber.DoubleNegativeInfinity;
  410. }
  411. return System.Math.Floor(x);
  412. }
  413. private static JsValue Log(JsValue thisObject, JsValue[] arguments)
  414. {
  415. var x = TypeConverter.ToNumber(arguments.At(0));
  416. if (double.IsNaN(x))
  417. {
  418. return JsNumber.DoubleNaN;
  419. }
  420. if (x < 0)
  421. {
  422. return JsNumber.DoubleNaN;
  423. }
  424. else if (x == 0)
  425. {
  426. return JsNumber.DoubleNegativeInfinity;
  427. }
  428. else if (double.IsPositiveInfinity(x))
  429. {
  430. return JsNumber.DoublePositiveInfinity;
  431. }
  432. else if (x == 1)
  433. {
  434. return JsNumber.PositiveZero;
  435. }
  436. return System.Math.Log(x);
  437. }
  438. private static JsValue Log1p(JsValue thisObject, JsValue[] arguments)
  439. {
  440. var x = TypeConverter.ToNumber(arguments.At(0));
  441. if (double.IsNaN(x))
  442. {
  443. return JsNumber.DoubleNaN;
  444. }
  445. if (x < -1)
  446. {
  447. return JsNumber.DoubleNaN;
  448. }
  449. if (x == -1)
  450. {
  451. return JsNumber.DoubleNegativeInfinity;
  452. }
  453. if (x == 0 || double.IsPositiveInfinity(x))
  454. {
  455. return arguments.At(0);
  456. }
  457. return System.Math.Log(1 + x);
  458. }
  459. private static JsValue Log2(JsValue thisObject, JsValue[] arguments)
  460. {
  461. var x = TypeConverter.ToNumber(arguments.At(0));
  462. if (double.IsNaN(x))
  463. {
  464. return JsNumber.DoubleNaN;
  465. }
  466. if (x < 0)
  467. {
  468. return JsNumber.DoubleNaN;
  469. }
  470. else if (x == 0)
  471. {
  472. return JsNumber.DoubleNegativeInfinity;
  473. }
  474. else if (double.IsPositiveInfinity(x))
  475. {
  476. return JsNumber.DoublePositiveInfinity;
  477. }
  478. else if (x == 1)
  479. {
  480. return JsNumber.PositiveZero;
  481. }
  482. return System.Math.Log(x, 2);
  483. }
  484. private static JsValue Log10(JsValue thisObject, JsValue[] arguments)
  485. {
  486. var x = TypeConverter.ToNumber(arguments.At(0));
  487. if (double.IsNaN(x))
  488. {
  489. return JsNumber.DoubleNaN;
  490. }
  491. if (x < 0)
  492. {
  493. return JsNumber.DoubleNaN;
  494. }
  495. else if (x == 0)
  496. {
  497. return JsNumber.DoubleNegativeInfinity;
  498. }
  499. else if (double.IsPositiveInfinity(x))
  500. {
  501. return JsNumber.DoublePositiveInfinity;
  502. }
  503. else if (x == 1)
  504. {
  505. return JsNumber.PositiveZero;
  506. }
  507. return System.Math.Log10(x);
  508. }
  509. private static JsValue Max(JsValue thisObject, JsValue[] arguments)
  510. {
  511. if (arguments.Length == 0)
  512. {
  513. return JsNumber.DoubleNegativeInfinity;
  514. }
  515. double max = TypeConverter.ToNumber(arguments.At(0));
  516. if (double.IsNaN(max))
  517. {
  518. return JsNumber.DoubleNaN;
  519. }
  520. for (int i = 0; i < arguments.Length; i++)
  521. {
  522. var value = TypeConverter.ToNumber(arguments[i]);
  523. if (double.IsNaN(value))
  524. {
  525. return JsNumber.DoubleNaN;
  526. }
  527. if (max == 0 && value == 0)
  528. {
  529. max = NumberInstance.IsNegativeZero(value)
  530. ? max
  531. : value;
  532. }
  533. else
  534. {
  535. max = System.Math.Max(max, value);
  536. }
  537. }
  538. return max;
  539. }
  540. private static JsValue Min(JsValue thisObject, JsValue[] arguments)
  541. {
  542. if (arguments.Length == 0)
  543. {
  544. return JsNumber.DoublePositiveInfinity;
  545. }
  546. double min = TypeConverter.ToNumber(arguments.At(0));
  547. for (int i = 0; i < arguments.Length; i++)
  548. {
  549. var value = TypeConverter.ToNumber(arguments[i]);
  550. if (min == 0 && value == 0)
  551. {
  552. min = NumberInstance.IsNegativeZero(min)
  553. ? min
  554. : value;
  555. }
  556. else
  557. {
  558. min = System.Math.Min(min, value);
  559. }
  560. }
  561. return min;
  562. }
  563. private static JsValue Pow(JsValue thisObject, JsValue[] arguments)
  564. {
  565. var x = TypeConverter.ToNumber(arguments.At(0));
  566. var y = TypeConverter.ToNumber(arguments.At(1));
  567. // check easy case where values are valid
  568. if (x > 1 && y > 1 && x < int.MaxValue && y < int.MaxValue)
  569. {
  570. return System.Math.Pow(x, y);
  571. }
  572. if (y == 0)
  573. {
  574. return 1;
  575. }
  576. return HandlePowUnlikely(y, x);
  577. }
  578. private static JsValue HandlePowUnlikely(double y, double x)
  579. {
  580. if (double.IsNaN(y))
  581. {
  582. return JsNumber.DoubleNaN;
  583. }
  584. if (double.IsNaN(x))
  585. {
  586. return JsNumber.DoubleNaN;
  587. }
  588. var absX = System.Math.Abs(x);
  589. if (absX > 1)
  590. {
  591. if (double.IsPositiveInfinity(y))
  592. {
  593. return JsNumber.DoublePositiveInfinity;
  594. }
  595. if (double.IsNegativeInfinity(y))
  596. {
  597. return JsNumber.PositiveZero;
  598. }
  599. }
  600. if (absX == 1)
  601. {
  602. if (double.IsInfinity(y))
  603. {
  604. return JsNumber.DoubleNaN;
  605. }
  606. }
  607. if (absX < 1)
  608. {
  609. if (double.IsPositiveInfinity(y))
  610. {
  611. return 0;
  612. }
  613. if (double.IsNegativeInfinity(y))
  614. {
  615. return JsNumber.DoublePositiveInfinity;
  616. }
  617. }
  618. if (double.IsPositiveInfinity(x))
  619. {
  620. if (y > 0)
  621. {
  622. return JsNumber.DoublePositiveInfinity;
  623. }
  624. if (y < 0)
  625. {
  626. return JsNumber.PositiveZero;
  627. }
  628. }
  629. if (double.IsNegativeInfinity(x))
  630. {
  631. if (y > 0)
  632. {
  633. if (System.Math.Abs(y % 2).Equals(1))
  634. {
  635. return JsNumber.DoubleNegativeInfinity;
  636. }
  637. return JsNumber.DoublePositiveInfinity;
  638. }
  639. if (y < 0)
  640. {
  641. if (System.Math.Abs(y % 2).Equals(1))
  642. {
  643. return JsNumber.NegativeZero;
  644. }
  645. return JsNumber.PositiveZero;
  646. }
  647. }
  648. if (NumberInstance.IsPositiveZero(x))
  649. {
  650. // If x is +0 and y>0, the result is +0.
  651. if (y > 0)
  652. {
  653. return 0;
  654. }
  655. // If x is +0 and y<0, the result is +∞.
  656. if (y < 0)
  657. {
  658. return JsNumber.DoublePositiveInfinity;
  659. }
  660. }
  661. if (NumberInstance.IsNegativeZero(x))
  662. {
  663. if (y > 0)
  664. {
  665. // If x is −0 and y>0 and y is an odd integer, the result is −0.
  666. if (System.Math.Abs(y % 2).Equals(1))
  667. {
  668. return JsNumber.NegativeZero;
  669. }
  670. // If x is −0 and y>0 and y is not an odd integer, the result is +0.
  671. return JsNumber.PositiveZero;
  672. }
  673. if (y < 0)
  674. {
  675. // If x is −0 and y<0 and y is an odd integer, the result is −∞.
  676. if (System.Math.Abs(y % 2).Equals(1))
  677. {
  678. return JsNumber.DoubleNegativeInfinity;
  679. }
  680. // If x is −0 and y<0 and y is not an odd integer, the result is +∞.
  681. return JsNumber.DoublePositiveInfinity;
  682. }
  683. }
  684. // If x<0 and x is finite and y is finite and y is not an integer, the result is NaN.
  685. if (x < 0 && !double.IsInfinity(x) && !double.IsInfinity(y) && !y.Equals((int) y))
  686. {
  687. return JsNumber.DoubleNaN;
  688. }
  689. return System.Math.Pow(x, y);
  690. }
  691. private JsValue Random(JsValue thisObject, JsValue[] arguments)
  692. {
  693. if(_random == null)
  694. {
  695. _random = new Random();
  696. }
  697. return _random.NextDouble();
  698. }
  699. private static JsValue Round(JsValue thisObject, JsValue[] arguments)
  700. {
  701. var x = TypeConverter.ToNumber(arguments.At(0));
  702. var round = System.Math.Round(x);
  703. if (round.Equals(x - 0.5))
  704. {
  705. return round + 1;
  706. }
  707. return round;
  708. }
  709. private static JsValue Fround(JsValue thisObject, JsValue[] arguments)
  710. {
  711. var x = TypeConverter.ToNumber(arguments.At(0));
  712. return (double) (float) x;
  713. }
  714. private static JsValue Sin(JsValue thisObject, JsValue[] arguments)
  715. {
  716. var x = TypeConverter.ToNumber(arguments.At(0));
  717. if (double.IsNaN(x))
  718. {
  719. return JsNumber.DoubleNaN;
  720. }
  721. else if (NumberInstance.IsPositiveZero(x))
  722. {
  723. return JsNumber.PositiveZero;
  724. }
  725. else if (NumberInstance.IsNegativeZero(x))
  726. {
  727. return JsNumber.NegativeZero;
  728. }
  729. else if (double.IsInfinity(x))
  730. {
  731. return JsNumber.DoubleNaN;
  732. }
  733. return System.Math.Sin(x);
  734. }
  735. private static JsValue Sinh(JsValue thisObject, JsValue[] arguments)
  736. {
  737. var x = TypeConverter.ToNumber(arguments.At(0));
  738. if (double.IsNaN(x))
  739. {
  740. return JsNumber.DoubleNaN;
  741. }
  742. else if (NumberInstance.IsPositiveZero(x))
  743. {
  744. return JsNumber.PositiveZero;
  745. }
  746. else if (NumberInstance.IsNegativeZero(x))
  747. {
  748. return JsNumber.NegativeZero;
  749. }
  750. else if (double.IsNegativeInfinity(x))
  751. {
  752. return JsNumber.DoubleNegativeInfinity;
  753. }
  754. else if (double.IsPositiveInfinity(x))
  755. {
  756. return JsNumber.DoublePositiveInfinity;
  757. }
  758. return System.Math.Sinh(x);
  759. }
  760. private static JsValue Sqrt(JsValue thisObject, JsValue[] arguments)
  761. {
  762. var x = TypeConverter.ToNumber(arguments.At(0));
  763. return System.Math.Sqrt(x);
  764. }
  765. private static JsValue Tan(JsValue thisObject, JsValue[] arguments)
  766. {
  767. var x = TypeConverter.ToNumber(arguments.At(0));
  768. return System.Math.Tan(x);
  769. }
  770. private static JsValue Tanh(JsValue thisObject, JsValue[] arguments)
  771. {
  772. var x = TypeConverter.ToNumber(arguments.At(0));
  773. return System.Math.Tanh(x);
  774. }
  775. private static JsValue Truncate(JsValue thisObject, JsValue[] arguments)
  776. {
  777. var x = TypeConverter.ToNumber(arguments.At(0));
  778. if (double.IsNaN(x))
  779. {
  780. return JsNumber.DoubleNaN;
  781. }
  782. if (NumberInstance.IsPositiveZero(x) || NumberInstance.IsNegativeZero(x))
  783. {
  784. return x;
  785. }
  786. if (double.IsPositiveInfinity(x))
  787. {
  788. return JsNumber.DoublePositiveInfinity;
  789. }
  790. if (double.IsNegativeInfinity(x))
  791. {
  792. return JsNumber.DoubleNegativeInfinity;
  793. }
  794. return System.Math.Truncate(x);
  795. }
  796. private static JsValue Sign(JsValue thisObject, JsValue[] arguments)
  797. {
  798. var x = TypeConverter.ToNumber(arguments.At(0));
  799. if (double.IsNaN(x))
  800. {
  801. return JsNumber.DoubleNaN;
  802. }
  803. if (NumberInstance.IsPositiveZero(x) || NumberInstance.IsNegativeZero(x))
  804. {
  805. return x;
  806. }
  807. if (double.IsPositiveInfinity(x))
  808. {
  809. return 1;
  810. }
  811. if (double.IsNegativeInfinity(x))
  812. {
  813. return -1;
  814. }
  815. return System.Math.Sign(x);
  816. }
  817. private static JsValue Cbrt(JsValue thisObject, JsValue[] arguments)
  818. {
  819. var x = TypeConverter.ToNumber(arguments.At(0));
  820. if (double.IsNaN(x))
  821. {
  822. return JsNumber.DoubleNaN;
  823. }
  824. else if (NumberInstance.IsPositiveZero(x) || NumberInstance.IsNegativeZero(x))
  825. {
  826. return x;
  827. }
  828. else if (double.IsPositiveInfinity(x))
  829. {
  830. return JsNumber.DoublePositiveInfinity;
  831. }
  832. else if (double.IsNegativeInfinity(x))
  833. {
  834. return JsNumber.DoubleNegativeInfinity;
  835. }
  836. if (System.Math.Sign(x) >= 0)
  837. {
  838. return System.Math.Pow(x, 1.0/3.0);
  839. }
  840. return -1 * System.Math.Pow(System.Math.Abs(x), 1.0 / 3.0);
  841. }
  842. private static JsValue Hypot(JsValue thisObject, JsValue[] arguments)
  843. {
  844. double y = 0;
  845. for (int i = 0; i < arguments.Length; ++i)
  846. {
  847. var number = TypeConverter.ToNumber(arguments[i]);
  848. if (double.IsInfinity(number))
  849. {
  850. return JsNumber.DoublePositiveInfinity;
  851. }
  852. y += number * number;
  853. }
  854. return System.Math.Sqrt(y);
  855. }
  856. private static JsValue Imul(JsValue thisObject, JsValue[] arguments)
  857. {
  858. var x = TypeConverter.ToInt32(arguments.At(0));
  859. var y = TypeConverter.ToInt32(arguments.At(1));
  860. return x * y;
  861. }
  862. private static JsValue Clz32(JsValue thisObject, JsValue[] arguments)
  863. {
  864. var x = TypeConverter.ToInt32(arguments.At(0));
  865. if (x < 0)
  866. {
  867. return 0;
  868. }
  869. if (x == 0)
  870. {
  871. return 32;
  872. }
  873. var res = 0;
  874. var shift = 16;
  875. while (x > 1)
  876. {
  877. var temp = x >> shift;
  878. if (temp != 0)
  879. {
  880. x = temp;
  881. res += shift;
  882. }
  883. shift >>= 1;
  884. }
  885. return 31 - res;
  886. }
  887. }
  888. }