MathInstance.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  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. {
  10. internal sealed class MathInstance : ObjectInstance
  11. {
  12. private Random? _random;
  13. internal MathInstance(Engine engine, ObjectPrototype objectPrototype) : base(engine)
  14. {
  15. _prototype = objectPrototype;
  16. }
  17. protected override void Initialize()
  18. {
  19. var properties = new PropertyDictionary(45, checkExistingKeys: false)
  20. {
  21. ["abs"] = new PropertyDescriptor(new ClrFunction(Engine, "abs", Abs, 1, PropertyFlag.Configurable), true, false, true),
  22. ["acos"] = new PropertyDescriptor(new ClrFunction(Engine, "acos", Acos, 1, PropertyFlag.Configurable), true, false, true),
  23. ["acosh"] = new PropertyDescriptor(new ClrFunction(Engine, "acosh", Acosh, 1, PropertyFlag.Configurable), true, false, true),
  24. ["asin"] = new PropertyDescriptor(new ClrFunction(Engine, "asin", Asin, 1, PropertyFlag.Configurable), true, false, true),
  25. ["asinh"] = new PropertyDescriptor(new ClrFunction(Engine, "asinh", Asinh, 1, PropertyFlag.Configurable), true, false, true),
  26. ["atan"] = new PropertyDescriptor(new ClrFunction(Engine, "atan", Atan, 1, PropertyFlag.Configurable), true, false, true),
  27. ["atanh"] = new PropertyDescriptor(new ClrFunction(Engine, "atanh", Atanh, 1, PropertyFlag.Configurable), true, false, true),
  28. ["atan2"] = new PropertyDescriptor(new ClrFunction(Engine, "atan2", Atan2, 2, PropertyFlag.Configurable), true, false, true),
  29. ["ceil"] = new PropertyDescriptor(new ClrFunction(Engine, "ceil", Ceil, 1, PropertyFlag.Configurable), true, false, true),
  30. ["cos"] = new PropertyDescriptor(new ClrFunction(Engine, "cos", Cos, 1, PropertyFlag.Configurable), true, false, true),
  31. ["cosh"] = new PropertyDescriptor(new ClrFunction(Engine, "cosh", Cosh, 1, PropertyFlag.Configurable), true, false, true),
  32. ["exp"] = new PropertyDescriptor(new ClrFunction(Engine, "exp", Exp, 1, PropertyFlag.Configurable), true, false, true),
  33. ["expm1"] = new PropertyDescriptor(new ClrFunction(Engine, "expm1", Expm1, 1, PropertyFlag.Configurable), true, false, true),
  34. ["floor"] = new PropertyDescriptor(new ClrFunction(Engine, "floor", Floor, 1, PropertyFlag.Configurable), true, false, true),
  35. ["log"] = new PropertyDescriptor(new ClrFunction(Engine, "log", Log, 1, PropertyFlag.Configurable), true, false, true),
  36. ["log1p"] = new PropertyDescriptor(new ClrFunction(Engine, "log1p", Log1p, 1, PropertyFlag.Configurable), true, false, true),
  37. ["log2"] = new PropertyDescriptor(new ClrFunction(Engine, "log2", Log2, 1, PropertyFlag.Configurable), true, false, true),
  38. ["log10"] = new PropertyDescriptor(new ClrFunction(Engine, "log10", Log10, 1, PropertyFlag.Configurable), true, false, true),
  39. ["max"] = new PropertyDescriptor(new ClrFunction(Engine, "max", Max, 2, PropertyFlag.Configurable), true, false, true),
  40. ["min"] = new PropertyDescriptor(new ClrFunction(Engine, "min", Min, 2, PropertyFlag.Configurable), true, false, true),
  41. ["pow"] = new PropertyDescriptor(new ClrFunction(Engine, "pow", Pow, 2, PropertyFlag.Configurable), true, false, true),
  42. ["random"] = new PropertyDescriptor(new ClrFunction(Engine, "random", Random, 0, PropertyFlag.Configurable), true, false, true),
  43. ["round"] = new PropertyDescriptor(new ClrFunction(Engine, "round", Round, 1, PropertyFlag.Configurable), true, false, true),
  44. ["fround"] = new PropertyDescriptor(new ClrFunction(Engine, "fround", Fround, 1, PropertyFlag.Configurable), true, false, true),
  45. ["sin"] = new PropertyDescriptor(new ClrFunction(Engine, "sin", Sin, 1, PropertyFlag.Configurable), true, false, true),
  46. ["sinh"] = new PropertyDescriptor(new ClrFunction(Engine, "sinh", Sinh, 1, PropertyFlag.Configurable), true, false, true),
  47. ["sqrt"] = new PropertyDescriptor(new ClrFunction(Engine, "sqrt", Sqrt, 1, PropertyFlag.Configurable), true, false, true),
  48. ["tan"] = new PropertyDescriptor(new ClrFunction(Engine, "tan", Tan, 1, PropertyFlag.Configurable), true, false, true),
  49. ["tanh"] = new PropertyDescriptor(new ClrFunction(Engine, "tanh", Tanh, 1, PropertyFlag.Configurable), true, false, true),
  50. ["trunc"] = new PropertyDescriptor(new ClrFunction(Engine, "trunc", Truncate, 1, PropertyFlag.Configurable), true, false, true),
  51. ["sign"] = new PropertyDescriptor(new ClrFunction(Engine, "sign", Sign, 1, PropertyFlag.Configurable), true, false, true),
  52. ["cbrt"] = new PropertyDescriptor(new ClrFunction(Engine, "cbrt", Cbrt, 1, PropertyFlag.Configurable), true, false, true),
  53. ["hypot"] = new PropertyDescriptor(new ClrFunction(Engine, "hypot", Hypot, 2, PropertyFlag.Configurable), true, false, true),
  54. ["imul"] = new PropertyDescriptor(new ClrFunction(Engine, "imul", Imul, 2, PropertyFlag.Configurable), true, false, true),
  55. ["clz32"] = new PropertyDescriptor(new ClrFunction(Engine, "clz32", Clz32, 1, PropertyFlag.Configurable), true, false, true),
  56. ["E"] = new PropertyDescriptor(System.Math.E, false, false, false),
  57. ["LN10"] = new PropertyDescriptor(System.Math.Log(10), false, false, false),
  58. ["LN2"] = new PropertyDescriptor(System.Math.Log(2), false, false, false),
  59. ["LOG2E"] = new PropertyDescriptor(System.Math.Log(System.Math.E, 2), false, false, false),
  60. ["LOG10E"] = new PropertyDescriptor(System.Math.Log(System.Math.E, 10), false, false, false),
  61. ["PI"] = new PropertyDescriptor(System.Math.PI, false, false, false),
  62. ["SQRT1_2"] = new PropertyDescriptor(System.Math.Sqrt(0.5), false, false, false),
  63. ["SQRT2"] = new PropertyDescriptor(System.Math.Sqrt(2), false, false, false)
  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, JsValue[] 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, JsValue[] 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, JsValue[] 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, JsValue[] 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, JsValue[] 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, JsValue[] 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, JsValue[] 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, JsValue[] 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, JsValue[] 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, JsValue[] 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, JsValue[] 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, JsValue[] 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, JsValue[] 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, JsValue[] 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, JsValue[] 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, JsValue[] 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, JsValue[] 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, JsValue[] 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, JsValue[] 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, JsValue[] 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, JsValue[] 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, JsValue[] arguments)
  702. {
  703. if(_random == null)
  704. {
  705. _random = new Random();
  706. }
  707. return _random.NextDouble();
  708. }
  709. private static JsValue Round(JsValue thisObject, JsValue[] 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, JsValue[] arguments)
  720. {
  721. var x = TypeConverter.ToNumber(arguments.At(0));
  722. return (double) (float) x;
  723. }
  724. private static JsValue Sin(JsValue thisObject, JsValue[] arguments)
  725. {
  726. var x = TypeConverter.ToNumber(arguments.At(0));
  727. if (double.IsNaN(x))
  728. {
  729. return JsNumber.DoubleNaN;
  730. }
  731. else if (NumberInstance.IsPositiveZero(x))
  732. {
  733. return JsNumber.PositiveZero;
  734. }
  735. else if (NumberInstance.IsNegativeZero(x))
  736. {
  737. return JsNumber.NegativeZero;
  738. }
  739. else if (double.IsInfinity(x))
  740. {
  741. return JsNumber.DoubleNaN;
  742. }
  743. return System.Math.Sin(x);
  744. }
  745. private static JsValue Sinh(JsValue thisObject, JsValue[] arguments)
  746. {
  747. var x = TypeConverter.ToNumber(arguments.At(0));
  748. if (double.IsNaN(x))
  749. {
  750. return JsNumber.DoubleNaN;
  751. }
  752. else if (NumberInstance.IsPositiveZero(x))
  753. {
  754. return JsNumber.PositiveZero;
  755. }
  756. else if (NumberInstance.IsNegativeZero(x))
  757. {
  758. return JsNumber.NegativeZero;
  759. }
  760. else if (double.IsNegativeInfinity(x))
  761. {
  762. return JsNumber.DoubleNegativeInfinity;
  763. }
  764. else if (double.IsPositiveInfinity(x))
  765. {
  766. return JsNumber.DoublePositiveInfinity;
  767. }
  768. return System.Math.Sinh(x);
  769. }
  770. private static JsValue Sqrt(JsValue thisObject, JsValue[] arguments)
  771. {
  772. var x = TypeConverter.ToNumber(arguments.At(0));
  773. return System.Math.Sqrt(x);
  774. }
  775. private static JsValue Tan(JsValue thisObject, JsValue[] arguments)
  776. {
  777. var x = TypeConverter.ToNumber(arguments.At(0));
  778. return System.Math.Tan(x);
  779. }
  780. private static JsValue Tanh(JsValue thisObject, JsValue[] arguments)
  781. {
  782. var x = TypeConverter.ToNumber(arguments.At(0));
  783. return System.Math.Tanh(x);
  784. }
  785. private static JsValue Truncate(JsValue thisObject, JsValue[] arguments)
  786. {
  787. var x = TypeConverter.ToNumber(arguments.At(0));
  788. if (double.IsNaN(x))
  789. {
  790. return JsNumber.DoubleNaN;
  791. }
  792. if (NumberInstance.IsPositiveZero(x) || NumberInstance.IsNegativeZero(x))
  793. {
  794. return x;
  795. }
  796. if (double.IsPositiveInfinity(x))
  797. {
  798. return JsNumber.DoublePositiveInfinity;
  799. }
  800. if (double.IsNegativeInfinity(x))
  801. {
  802. return JsNumber.DoubleNegativeInfinity;
  803. }
  804. return System.Math.Truncate(x);
  805. }
  806. private static JsValue Sign(JsValue thisObject, JsValue[] arguments)
  807. {
  808. var x = TypeConverter.ToNumber(arguments.At(0));
  809. if (double.IsNaN(x))
  810. {
  811. return JsNumber.DoubleNaN;
  812. }
  813. if (NumberInstance.IsPositiveZero(x) || NumberInstance.IsNegativeZero(x))
  814. {
  815. return x;
  816. }
  817. if (double.IsPositiveInfinity(x))
  818. {
  819. return 1;
  820. }
  821. if (double.IsNegativeInfinity(x))
  822. {
  823. return -1;
  824. }
  825. return System.Math.Sign(x);
  826. }
  827. private static JsValue Cbrt(JsValue thisObject, JsValue[] arguments)
  828. {
  829. var x = TypeConverter.ToNumber(arguments.At(0));
  830. if (double.IsNaN(x))
  831. {
  832. return JsNumber.DoubleNaN;
  833. }
  834. else if (NumberInstance.IsPositiveZero(x) || NumberInstance.IsNegativeZero(x))
  835. {
  836. return x;
  837. }
  838. else if (double.IsPositiveInfinity(x))
  839. {
  840. return JsNumber.DoublePositiveInfinity;
  841. }
  842. else if (double.IsNegativeInfinity(x))
  843. {
  844. return JsNumber.DoubleNegativeInfinity;
  845. }
  846. if (System.Math.Sign(x) >= 0)
  847. {
  848. return System.Math.Pow(x, 1.0/3.0);
  849. }
  850. return -1 * System.Math.Pow(System.Math.Abs(x), 1.0 / 3.0);
  851. }
  852. /// <summary>
  853. /// https://tc39.es/ecma262/#sec-math.hypot
  854. /// </summary>
  855. private static JsValue Hypot(JsValue thisObject, JsValue[] arguments)
  856. {
  857. var coerced = Coerced(arguments);
  858. foreach (var number in coerced)
  859. {
  860. if (double.IsInfinity(number))
  861. {
  862. return JsNumber.DoublePositiveInfinity;
  863. }
  864. }
  865. var onlyZero = true;
  866. double y = 0;
  867. foreach (var number in coerced)
  868. {
  869. if (double.IsNaN(number))
  870. {
  871. return JsNumber.DoubleNaN;
  872. }
  873. if (onlyZero && number != 0)
  874. {
  875. onlyZero = false;
  876. }
  877. y += number * number;
  878. }
  879. if (onlyZero)
  880. {
  881. return JsNumber.PositiveZero;
  882. }
  883. return System.Math.Sqrt(y);
  884. }
  885. private static double[] Coerced(JsValue[] arguments)
  886. {
  887. // TODO stackalloc
  888. var coerced = new double[arguments.Length];
  889. for (var i = 0; i < arguments.Length; i++)
  890. {
  891. var argument = arguments[i];
  892. coerced[i] = TypeConverter.ToNumber(argument);
  893. }
  894. return coerced;
  895. }
  896. private static JsValue Imul(JsValue thisObject, JsValue[] arguments)
  897. {
  898. var x = TypeConverter.ToInt32(arguments.At(0));
  899. var y = TypeConverter.ToInt32(arguments.At(1));
  900. return x * y;
  901. }
  902. private static JsValue Clz32(JsValue thisObject, JsValue[] arguments)
  903. {
  904. var x = TypeConverter.ToInt32(arguments.At(0));
  905. if (x < 0)
  906. {
  907. return 0;
  908. }
  909. if (x == 0)
  910. {
  911. return 32;
  912. }
  913. var res = 0;
  914. var shift = 16;
  915. while (x > 1)
  916. {
  917. var temp = x >> shift;
  918. if (temp != 0)
  919. {
  920. x = temp;
  921. res += shift;
  922. }
  923. shift >>= 1;
  924. }
  925. return 31 - res;
  926. }
  927. }
  928. }