MathInstance.cs 35 KB

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