MathInstance.cs 35 KB

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