MathInstance.cs 36 KB

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