MathInstance.cs 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  1. using Jint.Collections;
  2. using Jint.Native.Number;
  3. using Jint.Native.Object;
  4. using Jint.Native.Symbol;
  5. using Jint.Runtime;
  6. using Jint.Runtime.Descriptors;
  7. using Jint.Runtime.Interop;
  8. namespace Jint.Native.Math;
  9. internal sealed class MathInstance : ObjectInstance
  10. {
  11. private Random? _random;
  12. internal MathInstance(Engine engine, ObjectPrototype objectPrototype) : base(engine)
  13. {
  14. _prototype = objectPrototype;
  15. }
  16. protected override void Initialize()
  17. {
  18. var properties = new PropertyDictionary(45, checkExistingKeys: false)
  19. {
  20. ["E"] = new PropertyDescriptor(System.Math.E, PropertyFlag.AllForbidden),
  21. ["LN10"] = new PropertyDescriptor(System.Math.Log(10), PropertyFlag.AllForbidden),
  22. ["LN2"] = new PropertyDescriptor(System.Math.Log(2), PropertyFlag.AllForbidden),
  23. ["LOG10E"] = new PropertyDescriptor(System.Math.Log(System.Math.E, 10), PropertyFlag.AllForbidden),
  24. ["LOG2E"] = new PropertyDescriptor(System.Math.Log(System.Math.E, 2), PropertyFlag.AllForbidden),
  25. ["PI"] = new PropertyDescriptor(System.Math.PI, PropertyFlag.AllForbidden),
  26. ["SQRT1_2"] = new PropertyDescriptor(System.Math.Sqrt(0.5), PropertyFlag.AllForbidden),
  27. ["SQRT2"] = new PropertyDescriptor(System.Math.Sqrt(2), PropertyFlag.AllForbidden),
  28. ["abs"] = new PropertyDescriptor(new ClrFunction(Engine, "abs", Abs, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
  29. ["acos"] = new PropertyDescriptor(new ClrFunction(Engine, "acos", Acos, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
  30. ["acosh"] = new PropertyDescriptor(new ClrFunction(Engine, "acosh", Acosh, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
  31. ["asin"] = new PropertyDescriptor(new ClrFunction(Engine, "asin", Asin, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
  32. ["asinh"] = new PropertyDescriptor(new ClrFunction(Engine, "asinh", Asinh, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
  33. ["atan"] = new PropertyDescriptor(new ClrFunction(Engine, "atan", Atan, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
  34. ["atan2"] = new PropertyDescriptor(new ClrFunction(Engine, "atan2", Atan2, 2, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
  35. ["atanh"] = new PropertyDescriptor(new ClrFunction(Engine, "atanh", Atanh, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
  36. ["cbrt"] = new PropertyDescriptor(new ClrFunction(Engine, "cbrt", Cbrt, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
  37. ["ceil"] = new PropertyDescriptor(new ClrFunction(Engine, "ceil", Ceil, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
  38. ["clz32"] = new PropertyDescriptor(new ClrFunction(Engine, "clz32", Clz32, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
  39. ["cos"] = new PropertyDescriptor(new ClrFunction(Engine, "cos", Cos, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
  40. ["cosh"] = new PropertyDescriptor(new ClrFunction(Engine, "cosh", Cosh, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
  41. ["exp"] = new PropertyDescriptor(new ClrFunction(Engine, "exp", Exp, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
  42. ["expm1"] = new PropertyDescriptor(new ClrFunction(Engine, "expm1", Expm1, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
  43. ["f16round"] = new PropertyDescriptor(new ClrFunction(Engine, "f16round", F16Round, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
  44. ["floor"] = new PropertyDescriptor(new ClrFunction(Engine, "floor", Floor, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
  45. ["fround"] = new PropertyDescriptor(new ClrFunction(Engine, "fround", Fround, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
  46. ["hypot"] = new PropertyDescriptor(new ClrFunction(Engine, "hypot", Hypot, 2, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
  47. ["imul"] = new PropertyDescriptor(new ClrFunction(Engine, "imul", Imul, 2, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
  48. ["log"] = new PropertyDescriptor(new ClrFunction(Engine, "log", Log, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
  49. ["log10"] = new PropertyDescriptor(new ClrFunction(Engine, "log10", Log10, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
  50. ["log1p"] = new PropertyDescriptor(new ClrFunction(Engine, "log1p", Log1p, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
  51. ["log2"] = new PropertyDescriptor(new ClrFunction(Engine, "log2", Log2, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
  52. ["max"] = new PropertyDescriptor(new ClrFunction(Engine, "max", Max, 2, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
  53. ["min"] = new PropertyDescriptor(new ClrFunction(Engine, "min", Min, 2, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
  54. ["pow"] = new PropertyDescriptor(new ClrFunction(Engine, "pow", Pow, 2, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
  55. ["random"] = new PropertyDescriptor(new ClrFunction(Engine, "random", Random, 0, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
  56. ["round"] = new PropertyDescriptor(new ClrFunction(Engine, "round", Round, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
  57. ["sign"] = new PropertyDescriptor(new ClrFunction(Engine, "sign", Sign, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
  58. ["sin"] = new PropertyDescriptor(new ClrFunction(Engine, "sin", Sin, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
  59. ["sinh"] = new PropertyDescriptor(new ClrFunction(Engine, "sinh", Sinh, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
  60. ["sumPrecise"] = new PropertyDescriptor(new ClrFunction(Engine, "sumPrecise", SumPrecise, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
  61. ["sqrt"] = new PropertyDescriptor(new ClrFunction(Engine, "sqrt", Sqrt, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
  62. ["tan"] = new PropertyDescriptor(new ClrFunction(Engine, "tan", Tan, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
  63. ["tanh"] = new PropertyDescriptor(new ClrFunction(Engine, "tanh", Tanh, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
  64. ["trunc"] = new PropertyDescriptor(new ClrFunction(Engine, "trunc", Truncate, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
  65. };
  66. SetProperties(properties);
  67. var symbols = new SymbolDictionary(1)
  68. {
  69. [GlobalSymbolRegistry.ToStringTag] = new PropertyDescriptor(new JsString("Math"), PropertyFlag.Configurable)
  70. };
  71. SetSymbols(symbols);
  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. /// <summary>
  521. /// https://tc39.es/ecma262/#sec-math.max
  522. /// </summary>
  523. private static JsValue Max(JsValue thisObject, JsValue[] arguments)
  524. {
  525. if (arguments.Length == 0)
  526. {
  527. return JsNumber.DoubleNegativeInfinity;
  528. }
  529. var highest = double.NegativeInfinity;
  530. foreach (var number in Coerced(arguments))
  531. {
  532. if (double.IsNaN(number))
  533. {
  534. return JsNumber.DoubleNaN;
  535. }
  536. if (NumberInstance.IsPositiveZero(number) && NumberInstance.IsNegativeZero(highest))
  537. {
  538. highest = 0;
  539. }
  540. if (number > highest)
  541. {
  542. highest = number;
  543. }
  544. }
  545. return highest;
  546. }
  547. /// <summary>
  548. /// https://tc39.es/ecma262/#sec-math.min
  549. /// </summary>
  550. private static JsValue Min(JsValue thisObject, JsValue[] arguments)
  551. {
  552. if (arguments.Length == 0)
  553. {
  554. return JsNumber.DoublePositiveInfinity;
  555. }
  556. var lowest = double.PositiveInfinity;
  557. foreach (var number in Coerced(arguments))
  558. {
  559. if (double.IsNaN(number))
  560. {
  561. return JsNumber.DoubleNaN;
  562. }
  563. if (NumberInstance.IsNegativeZero(number) && NumberInstance.IsPositiveZero(lowest))
  564. {
  565. lowest = JsNumber.NegativeZero._value;
  566. }
  567. if (number < lowest)
  568. {
  569. lowest = number;
  570. }
  571. }
  572. return lowest;
  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 = arguments.At(0);
  723. var n = TypeConverter.ToNumber(x);
  724. return (double) (float) n;
  725. }
  726. /// <summary>
  727. /// https://tc39.es/proposal-float16array/#sec-math.f16round
  728. /// </summary>
  729. private static JsValue F16Round(JsValue thisObject, JsValue[] arguments)
  730. {
  731. #if SUPPORTS_HALF
  732. var x = arguments.At(0);
  733. var n = TypeConverter.ToNumber(x);
  734. if (double.IsNaN(n))
  735. {
  736. return JsNumber.DoubleNaN;
  737. }
  738. if (double.IsInfinity(n) || NumberInstance.IsPositiveZero(n) || NumberInstance.IsNegativeZero(n))
  739. {
  740. return x;
  741. }
  742. return (double) (Half) n;
  743. #else
  744. ExceptionHelper.ThrowNotImplementedException("Float16/Half type is not supported in this build");
  745. return default;
  746. #endif
  747. }
  748. private static JsValue Sin(JsValue thisObject, JsValue[] arguments)
  749. {
  750. var x = TypeConverter.ToNumber(arguments.At(0));
  751. if (double.IsNaN(x))
  752. {
  753. return JsNumber.DoubleNaN;
  754. }
  755. else if (NumberInstance.IsPositiveZero(x))
  756. {
  757. return JsNumber.PositiveZero;
  758. }
  759. else if (NumberInstance.IsNegativeZero(x))
  760. {
  761. return JsNumber.NegativeZero;
  762. }
  763. else if (double.IsInfinity(x))
  764. {
  765. return JsNumber.DoubleNaN;
  766. }
  767. return System.Math.Sin(x);
  768. }
  769. private static JsValue Sinh(JsValue thisObject, JsValue[] arguments)
  770. {
  771. var x = TypeConverter.ToNumber(arguments.At(0));
  772. if (double.IsNaN(x))
  773. {
  774. return JsNumber.DoubleNaN;
  775. }
  776. else if (NumberInstance.IsPositiveZero(x))
  777. {
  778. return JsNumber.PositiveZero;
  779. }
  780. else if (NumberInstance.IsNegativeZero(x))
  781. {
  782. return JsNumber.NegativeZero;
  783. }
  784. else if (double.IsNegativeInfinity(x))
  785. {
  786. return JsNumber.DoubleNegativeInfinity;
  787. }
  788. else if (double.IsPositiveInfinity(x))
  789. {
  790. return JsNumber.DoublePositiveInfinity;
  791. }
  792. return System.Math.Sinh(x);
  793. }
  794. private static JsValue Sqrt(JsValue thisObject, JsValue[] arguments)
  795. {
  796. var x = TypeConverter.ToNumber(arguments.At(0));
  797. return System.Math.Sqrt(x);
  798. }
  799. private static JsValue Tan(JsValue thisObject, JsValue[] arguments)
  800. {
  801. var x = TypeConverter.ToNumber(arguments.At(0));
  802. return System.Math.Tan(x);
  803. }
  804. private static JsValue Tanh(JsValue thisObject, JsValue[] arguments)
  805. {
  806. var x = TypeConverter.ToNumber(arguments.At(0));
  807. return System.Math.Tanh(x);
  808. }
  809. private static JsValue Truncate(JsValue thisObject, JsValue[] arguments)
  810. {
  811. var x = TypeConverter.ToNumber(arguments.At(0));
  812. if (double.IsNaN(x))
  813. {
  814. return JsNumber.DoubleNaN;
  815. }
  816. if (NumberInstance.IsPositiveZero(x) || NumberInstance.IsNegativeZero(x))
  817. {
  818. return x;
  819. }
  820. if (double.IsPositiveInfinity(x))
  821. {
  822. return JsNumber.DoublePositiveInfinity;
  823. }
  824. if (double.IsNegativeInfinity(x))
  825. {
  826. return JsNumber.DoubleNegativeInfinity;
  827. }
  828. return System.Math.Truncate(x);
  829. }
  830. private static JsValue Sign(JsValue thisObject, JsValue[] arguments)
  831. {
  832. var x = TypeConverter.ToNumber(arguments.At(0));
  833. if (double.IsNaN(x))
  834. {
  835. return JsNumber.DoubleNaN;
  836. }
  837. if (NumberInstance.IsPositiveZero(x) || NumberInstance.IsNegativeZero(x))
  838. {
  839. return x;
  840. }
  841. if (double.IsPositiveInfinity(x))
  842. {
  843. return 1;
  844. }
  845. if (double.IsNegativeInfinity(x))
  846. {
  847. return -1;
  848. }
  849. return System.Math.Sign(x);
  850. }
  851. private static JsValue Cbrt(JsValue thisObject, JsValue[] arguments)
  852. {
  853. var x = TypeConverter.ToNumber(arguments.At(0));
  854. if (double.IsNaN(x))
  855. {
  856. return JsNumber.DoubleNaN;
  857. }
  858. else if (NumberInstance.IsPositiveZero(x) || NumberInstance.IsNegativeZero(x))
  859. {
  860. return x;
  861. }
  862. else if (double.IsPositiveInfinity(x))
  863. {
  864. return JsNumber.DoublePositiveInfinity;
  865. }
  866. else if (double.IsNegativeInfinity(x))
  867. {
  868. return JsNumber.DoubleNegativeInfinity;
  869. }
  870. if (System.Math.Sign(x) >= 0)
  871. {
  872. return System.Math.Pow(x, 1.0/3.0);
  873. }
  874. return -1 * System.Math.Pow(System.Math.Abs(x), 1.0 / 3.0);
  875. }
  876. /// <summary>
  877. /// https://tc39.es/ecma262/#sec-math.hypot
  878. /// </summary>
  879. private static JsValue Hypot(JsValue thisObject, JsValue[] arguments)
  880. {
  881. var coerced = Coerced(arguments);
  882. foreach (var number in coerced)
  883. {
  884. if (double.IsInfinity(number))
  885. {
  886. return JsNumber.DoublePositiveInfinity;
  887. }
  888. }
  889. var onlyZero = true;
  890. double y = 0;
  891. foreach (var number in coerced)
  892. {
  893. if (double.IsNaN(number))
  894. {
  895. return JsNumber.DoubleNaN;
  896. }
  897. if (onlyZero && number != 0)
  898. {
  899. onlyZero = false;
  900. }
  901. y += number * number;
  902. }
  903. if (onlyZero)
  904. {
  905. return JsNumber.PositiveZero;
  906. }
  907. return System.Math.Sqrt(y);
  908. }
  909. /// <summary>
  910. /// https://github.com/tc39/proposal-math-sum
  911. /// </summary>
  912. private JsValue SumPrecise(JsValue thisObject, JsValue[] arguments)
  913. {
  914. var items = arguments.At(0);
  915. if (items.IsNullOrUndefined())
  916. {
  917. ExceptionHelper.ThrowTypeError(_engine.Realm);
  918. }
  919. var iteratorRecord = items.GetIterator(_engine.Realm);
  920. var state = JsNumber.NegativeZero._value;
  921. List<double> sum = [];
  922. long count = 0;
  923. const double Finite = 1;
  924. try
  925. {
  926. while (iteratorRecord.TryIteratorStep(out var next))
  927. {
  928. next.TryGetValue(CommonProperties.Value, out var value);
  929. count++;
  930. if (count > 9007199254740992)
  931. {
  932. ExceptionHelper.ThrowRangeError(_engine.Realm);
  933. }
  934. if (value is not JsNumber jsNumber)
  935. {
  936. ExceptionHelper.ThrowTypeError(_engine.Realm, "Input is not a number: " + next);
  937. return default;
  938. }
  939. if (!double.IsNaN(state))
  940. {
  941. var n = jsNumber._value;
  942. if (double.IsNaN(n))
  943. {
  944. state = double.NaN;
  945. }
  946. else if (double.IsPositiveInfinity(n))
  947. {
  948. if (double.IsNegativeInfinity(state))
  949. {
  950. state = double.NaN;
  951. }
  952. else
  953. {
  954. state = double.PositiveInfinity;
  955. }
  956. }
  957. else if (double.IsNegativeInfinity(n))
  958. {
  959. if (double.IsPositiveInfinity(state))
  960. {
  961. state = double.NaN;
  962. }
  963. else
  964. {
  965. state = double.NegativeInfinity;
  966. }
  967. }
  968. else if (!NumberInstance.IsNegativeZero(n) && (NumberInstance.IsNegativeZero(state) || state == Finite))
  969. {
  970. state = Finite;
  971. sum.Add(n);
  972. }
  973. }
  974. }
  975. }
  976. catch
  977. {
  978. iteratorRecord.Close(CompletionType.Throw);
  979. iteratorRecord = null;
  980. throw;
  981. }
  982. finally
  983. {
  984. iteratorRecord?.Close(CompletionType.Normal);
  985. }
  986. if (state != Finite)
  987. {
  988. return state;
  989. }
  990. return sum.FSum();
  991. }
  992. private static double[] Coerced(JsValue[] arguments)
  993. {
  994. // TODO stackalloc
  995. var coerced = new double[arguments.Length];
  996. for (var i = 0; i < arguments.Length; i++)
  997. {
  998. var argument = arguments[i];
  999. coerced[i] = TypeConverter.ToNumber(argument);
  1000. }
  1001. return coerced;
  1002. }
  1003. private static JsValue Imul(JsValue thisObject, JsValue[] arguments)
  1004. {
  1005. var x = TypeConverter.ToInt32(arguments.At(0));
  1006. var y = TypeConverter.ToInt32(arguments.At(1));
  1007. return x * y;
  1008. }
  1009. private static JsValue Clz32(JsValue thisObject, JsValue[] arguments)
  1010. {
  1011. var x = TypeConverter.ToInt32(arguments.At(0));
  1012. if (x < 0)
  1013. {
  1014. return 0;
  1015. }
  1016. if (x == 0)
  1017. {
  1018. return 32;
  1019. }
  1020. var res = 0;
  1021. var shift = 16;
  1022. while (x > 1)
  1023. {
  1024. var temp = x >> shift;
  1025. if (temp != 0)
  1026. {
  1027. x = temp;
  1028. res += shift;
  1029. }
  1030. shift >>= 1;
  1031. }
  1032. return 31 - res;
  1033. }
  1034. }