MathInstance.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  1. using System;
  2. using Jint.Collections;
  3. using Jint.Native.Number;
  4. using Jint.Native.Object;
  5. using Jint.Native.Symbol;
  6. using Jint.Runtime;
  7. using Jint.Runtime.Descriptors;
  8. using Jint.Runtime.Interop;
  9. namespace Jint.Native.Math
  10. {
  11. public sealed class MathInstance : ObjectInstance
  12. {
  13. private Random _random;
  14. internal MathInstance(Engine engine, ObjectPrototype objectPrototype) : base(engine, ObjectClass.Math)
  15. {
  16. _prototype = objectPrototype;
  17. }
  18. protected override void Initialize()
  19. {
  20. var properties = new PropertyDictionary(45, checkExistingKeys: false)
  21. {
  22. ["abs"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "abs", Abs, 1, PropertyFlag.Configurable), true, false, true),
  23. ["acos"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "acos", Acos, 1, PropertyFlag.Configurable), true, false, true),
  24. ["acosh"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "acosh", Acosh, 1, PropertyFlag.Configurable), true, false, true),
  25. ["asin"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "asin", Asin, 1, PropertyFlag.Configurable), true, false, true),
  26. ["asinh"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "asinh", Asinh, 1, PropertyFlag.Configurable), true, false, true),
  27. ["atan"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "atan", Atan, 1, PropertyFlag.Configurable), true, false, true),
  28. ["atanh"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "atanh", Atanh, 1, PropertyFlag.Configurable), true, false, true),
  29. ["atan2"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "atan2", Atan2, 2, PropertyFlag.Configurable), true, false, true),
  30. ["ceil"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "ceil", Ceil, 1, PropertyFlag.Configurable), true, false, true),
  31. ["cos"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "cos", Cos, 1, PropertyFlag.Configurable), true, false, true),
  32. ["cosh"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "cosh", Cosh, 1, PropertyFlag.Configurable), true, false, true),
  33. ["exp"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "exp", Exp, 1, PropertyFlag.Configurable), true, false, true),
  34. ["expm1"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "expm1", Expm1, 1, PropertyFlag.Configurable), true, false, true),
  35. ["floor"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "floor", Floor, 1, PropertyFlag.Configurable), true, false, true),
  36. ["log"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "log", Log, 1, PropertyFlag.Configurable), true, false, true),
  37. ["log1p"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "log1p", Log1p, 1, PropertyFlag.Configurable), true, false, true),
  38. ["log2"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "log2", Log2, 1, PropertyFlag.Configurable), true, false, true),
  39. ["log10"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "log10", Log10, 1, PropertyFlag.Configurable), true, false, true),
  40. ["max"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "max", Max, 2, PropertyFlag.Configurable), true, false, true),
  41. ["min"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "min", Min, 2, PropertyFlag.Configurable), true, false, true),
  42. ["pow"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "pow", Pow, 2, PropertyFlag.Configurable), true, false, true),
  43. ["random"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "random", Random, 0, PropertyFlag.Configurable), true, false, true),
  44. ["round"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "round", Round, 1, PropertyFlag.Configurable), true, false, true),
  45. ["fround"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "fround", Fround, 1, PropertyFlag.Configurable), true, false, true),
  46. ["sin"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "sin", Sin, 1, PropertyFlag.Configurable), true, false, true),
  47. ["sinh"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "sinh", Sinh, 1, PropertyFlag.Configurable), true, false, true),
  48. ["sqrt"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "sqrt", Sqrt, 1, PropertyFlag.Configurable), true, false, true),
  49. ["tan"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "tan", Tan, 1, PropertyFlag.Configurable), true, false, true),
  50. ["tanh"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "tanh", Tanh, 1, PropertyFlag.Configurable), true, false, true),
  51. ["trunc"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "trunc", Truncate, 1, PropertyFlag.Configurable), true, false, true),
  52. ["sign"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "sign", Sign, 1, PropertyFlag.Configurable), true, false, true),
  53. ["cbrt"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "cbrt", Cbrt, 1, PropertyFlag.Configurable), true, false, true),
  54. ["hypot"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "hypot", Hypot, 2, PropertyFlag.Configurable), true, false, true),
  55. ["imul"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "imul", Imul, 2, PropertyFlag.Configurable), true, false, true),
  56. ["clz32"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "clz32", Clz32, 1, PropertyFlag.Configurable), true, false, true),
  57. ["E"] = new PropertyDescriptor(System.Math.E, false, false, false),
  58. ["LN10"] = new PropertyDescriptor(System.Math.Log(10), false, false, false),
  59. ["LN2"] = new PropertyDescriptor(System.Math.Log(2), false, false, false),
  60. ["LOG2E"] = new PropertyDescriptor(System.Math.Log(System.Math.E, 2), false, false, false),
  61. ["LOG10E"] = new PropertyDescriptor(System.Math.Log(System.Math.E, 10), false, false, false),
  62. ["PI"] = new PropertyDescriptor(System.Math.PI, false, false, false),
  63. ["SQRT1_2"] = new PropertyDescriptor(System.Math.Sqrt(0.5), false, false, false),
  64. ["SQRT2"] = new PropertyDescriptor(System.Math.Sqrt(2), false, false, false)
  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. private static JsValue Max(JsValue thisObject, JsValue[] arguments)
  521. {
  522. if (arguments.Length == 0)
  523. {
  524. return JsNumber.DoubleNegativeInfinity;
  525. }
  526. double max = TypeConverter.ToNumber(arguments.At(0));
  527. if (double.IsNaN(max))
  528. {
  529. return JsNumber.DoubleNaN;
  530. }
  531. for (int i = 0; i < arguments.Length; i++)
  532. {
  533. var value = TypeConverter.ToNumber(arguments[i]);
  534. if (double.IsNaN(value))
  535. {
  536. return JsNumber.DoubleNaN;
  537. }
  538. if (max == 0 && value == 0)
  539. {
  540. max = NumberInstance.IsNegativeZero(value)
  541. ? max
  542. : value;
  543. }
  544. else
  545. {
  546. max = System.Math.Max(max, value);
  547. }
  548. }
  549. return max;
  550. }
  551. private static JsValue Min(JsValue thisObject, JsValue[] arguments)
  552. {
  553. if (arguments.Length == 0)
  554. {
  555. return JsNumber.DoublePositiveInfinity;
  556. }
  557. double min = TypeConverter.ToNumber(arguments.At(0));
  558. for (int i = 0; i < arguments.Length; i++)
  559. {
  560. var value = TypeConverter.ToNumber(arguments[i]);
  561. if (min == 0 && value == 0)
  562. {
  563. min = NumberInstance.IsNegativeZero(min)
  564. ? min
  565. : value;
  566. }
  567. else
  568. {
  569. min = System.Math.Min(min, value);
  570. }
  571. }
  572. return min;
  573. }
  574. private static JsValue Pow(JsValue thisObject, JsValue[] arguments)
  575. {
  576. var x = TypeConverter.ToNumber(arguments.At(0));
  577. var y = TypeConverter.ToNumber(arguments.At(1));
  578. // check easy case where values are valid
  579. if (x > 1 && y > 1 && x < int.MaxValue && y < int.MaxValue)
  580. {
  581. return System.Math.Pow(x, y);
  582. }
  583. if (y == 0)
  584. {
  585. return 1;
  586. }
  587. return HandlePowUnlikely(y, x);
  588. }
  589. private static JsValue HandlePowUnlikely(double y, double x)
  590. {
  591. if (double.IsNaN(y))
  592. {
  593. return JsNumber.DoubleNaN;
  594. }
  595. if (double.IsNaN(x))
  596. {
  597. return JsNumber.DoubleNaN;
  598. }
  599. var absX = System.Math.Abs(x);
  600. if (absX > 1)
  601. {
  602. if (double.IsPositiveInfinity(y))
  603. {
  604. return JsNumber.DoublePositiveInfinity;
  605. }
  606. if (double.IsNegativeInfinity(y))
  607. {
  608. return JsNumber.PositiveZero;
  609. }
  610. }
  611. if (absX == 1)
  612. {
  613. if (double.IsInfinity(y))
  614. {
  615. return JsNumber.DoubleNaN;
  616. }
  617. }
  618. if (absX < 1)
  619. {
  620. if (double.IsPositiveInfinity(y))
  621. {
  622. return 0;
  623. }
  624. if (double.IsNegativeInfinity(y))
  625. {
  626. return JsNumber.DoublePositiveInfinity;
  627. }
  628. }
  629. if (double.IsPositiveInfinity(x))
  630. {
  631. if (y > 0)
  632. {
  633. return JsNumber.DoublePositiveInfinity;
  634. }
  635. if (y < 0)
  636. {
  637. return JsNumber.PositiveZero;
  638. }
  639. }
  640. if (double.IsNegativeInfinity(x))
  641. {
  642. if (y > 0)
  643. {
  644. if (System.Math.Abs(y % 2).Equals(1))
  645. {
  646. return JsNumber.DoubleNegativeInfinity;
  647. }
  648. return JsNumber.DoublePositiveInfinity;
  649. }
  650. if (y < 0)
  651. {
  652. if (System.Math.Abs(y % 2).Equals(1))
  653. {
  654. return JsNumber.NegativeZero;
  655. }
  656. return JsNumber.PositiveZero;
  657. }
  658. }
  659. if (NumberInstance.IsPositiveZero(x))
  660. {
  661. // If x is +0 and y>0, the result is +0.
  662. if (y > 0)
  663. {
  664. return 0;
  665. }
  666. // If x is +0 and y<0, the result is +∞.
  667. if (y < 0)
  668. {
  669. return JsNumber.DoublePositiveInfinity;
  670. }
  671. }
  672. if (NumberInstance.IsNegativeZero(x))
  673. {
  674. if (y > 0)
  675. {
  676. // If x is −0 and y>0 and y is an odd integer, the result is −0.
  677. if (System.Math.Abs(y % 2).Equals(1))
  678. {
  679. return JsNumber.NegativeZero;
  680. }
  681. // If x is −0 and y>0 and y is not an odd integer, the result is +0.
  682. return JsNumber.PositiveZero;
  683. }
  684. if (y < 0)
  685. {
  686. // If x is −0 and y<0 and y is an odd integer, the result is −∞.
  687. if (System.Math.Abs(y % 2).Equals(1))
  688. {
  689. return JsNumber.DoubleNegativeInfinity;
  690. }
  691. // If x is −0 and y<0 and y is not an odd integer, the result is +∞.
  692. return JsNumber.DoublePositiveInfinity;
  693. }
  694. }
  695. // If x<0 and x is finite and y is finite and y is not an integer, the result is NaN.
  696. if (x < 0 && !double.IsInfinity(x) && !double.IsInfinity(y) && !y.Equals((int) y))
  697. {
  698. return JsNumber.DoubleNaN;
  699. }
  700. return System.Math.Pow(x, y);
  701. }
  702. private JsValue Random(JsValue thisObject, JsValue[] arguments)
  703. {
  704. if(_random == null)
  705. {
  706. _random = new Random();
  707. }
  708. return _random.NextDouble();
  709. }
  710. private static JsValue Round(JsValue thisObject, JsValue[] arguments)
  711. {
  712. var x = TypeConverter.ToNumber(arguments.At(0));
  713. var round = System.Math.Round(x);
  714. if (round.Equals(x - 0.5))
  715. {
  716. return round + 1;
  717. }
  718. return round;
  719. }
  720. private static JsValue Fround(JsValue thisObject, JsValue[] arguments)
  721. {
  722. var x = TypeConverter.ToNumber(arguments.At(0));
  723. return (double) (float) x;
  724. }
  725. private static JsValue Sin(JsValue thisObject, JsValue[] arguments)
  726. {
  727. var x = TypeConverter.ToNumber(arguments.At(0));
  728. if (double.IsNaN(x))
  729. {
  730. return JsNumber.DoubleNaN;
  731. }
  732. else if (NumberInstance.IsPositiveZero(x))
  733. {
  734. return JsNumber.PositiveZero;
  735. }
  736. else if (NumberInstance.IsNegativeZero(x))
  737. {
  738. return JsNumber.NegativeZero;
  739. }
  740. else if (double.IsInfinity(x))
  741. {
  742. return JsNumber.DoubleNaN;
  743. }
  744. return System.Math.Sin(x);
  745. }
  746. private static JsValue Sinh(JsValue thisObject, JsValue[] arguments)
  747. {
  748. var x = TypeConverter.ToNumber(arguments.At(0));
  749. if (double.IsNaN(x))
  750. {
  751. return JsNumber.DoubleNaN;
  752. }
  753. else if (NumberInstance.IsPositiveZero(x))
  754. {
  755. return JsNumber.PositiveZero;
  756. }
  757. else if (NumberInstance.IsNegativeZero(x))
  758. {
  759. return JsNumber.NegativeZero;
  760. }
  761. else if (double.IsNegativeInfinity(x))
  762. {
  763. return JsNumber.DoubleNegativeInfinity;
  764. }
  765. else if (double.IsPositiveInfinity(x))
  766. {
  767. return JsNumber.DoublePositiveInfinity;
  768. }
  769. return System.Math.Sinh(x);
  770. }
  771. private static JsValue Sqrt(JsValue thisObject, JsValue[] arguments)
  772. {
  773. var x = TypeConverter.ToNumber(arguments.At(0));
  774. return System.Math.Sqrt(x);
  775. }
  776. private static JsValue Tan(JsValue thisObject, JsValue[] arguments)
  777. {
  778. var x = TypeConverter.ToNumber(arguments.At(0));
  779. return System.Math.Tan(x);
  780. }
  781. private static JsValue Tanh(JsValue thisObject, JsValue[] arguments)
  782. {
  783. var x = TypeConverter.ToNumber(arguments.At(0));
  784. return System.Math.Tanh(x);
  785. }
  786. private static JsValue Truncate(JsValue thisObject, JsValue[] arguments)
  787. {
  788. var x = TypeConverter.ToNumber(arguments.At(0));
  789. if (double.IsNaN(x))
  790. {
  791. return JsNumber.DoubleNaN;
  792. }
  793. if (NumberInstance.IsPositiveZero(x) || NumberInstance.IsNegativeZero(x))
  794. {
  795. return x;
  796. }
  797. if (double.IsPositiveInfinity(x))
  798. {
  799. return JsNumber.DoublePositiveInfinity;
  800. }
  801. if (double.IsNegativeInfinity(x))
  802. {
  803. return JsNumber.DoubleNegativeInfinity;
  804. }
  805. return System.Math.Truncate(x);
  806. }
  807. private static JsValue Sign(JsValue thisObject, JsValue[] arguments)
  808. {
  809. var x = TypeConverter.ToNumber(arguments.At(0));
  810. if (double.IsNaN(x))
  811. {
  812. return JsNumber.DoubleNaN;
  813. }
  814. if (NumberInstance.IsPositiveZero(x) || NumberInstance.IsNegativeZero(x))
  815. {
  816. return x;
  817. }
  818. if (double.IsPositiveInfinity(x))
  819. {
  820. return 1;
  821. }
  822. if (double.IsNegativeInfinity(x))
  823. {
  824. return -1;
  825. }
  826. return System.Math.Sign(x);
  827. }
  828. private static JsValue Cbrt(JsValue thisObject, JsValue[] arguments)
  829. {
  830. var x = TypeConverter.ToNumber(arguments.At(0));
  831. if (double.IsNaN(x))
  832. {
  833. return JsNumber.DoubleNaN;
  834. }
  835. else if (NumberInstance.IsPositiveZero(x) || NumberInstance.IsNegativeZero(x))
  836. {
  837. return x;
  838. }
  839. else if (double.IsPositiveInfinity(x))
  840. {
  841. return JsNumber.DoublePositiveInfinity;
  842. }
  843. else if (double.IsNegativeInfinity(x))
  844. {
  845. return JsNumber.DoubleNegativeInfinity;
  846. }
  847. if (System.Math.Sign(x) >= 0)
  848. {
  849. return System.Math.Pow(x, 1.0/3.0);
  850. }
  851. return -1 * System.Math.Pow(System.Math.Abs(x), 1.0 / 3.0);
  852. }
  853. private static JsValue Hypot(JsValue thisObject, JsValue[] arguments)
  854. {
  855. double y = 0;
  856. for (int i = 0; i < arguments.Length; ++i)
  857. {
  858. var number = TypeConverter.ToNumber(arguments[i]);
  859. if (double.IsInfinity(number))
  860. {
  861. return JsNumber.DoublePositiveInfinity;
  862. }
  863. y += number * number;
  864. }
  865. return System.Math.Sqrt(y);
  866. }
  867. private static JsValue Imul(JsValue thisObject, JsValue[] arguments)
  868. {
  869. var x = TypeConverter.ToInt32(arguments.At(0));
  870. var y = TypeConverter.ToInt32(arguments.At(1));
  871. return x * y;
  872. }
  873. private static JsValue Clz32(JsValue thisObject, JsValue[] arguments)
  874. {
  875. var x = TypeConverter.ToInt32(arguments.At(0));
  876. if (x < 0)
  877. {
  878. return 0;
  879. }
  880. if (x == 0)
  881. {
  882. return 32;
  883. }
  884. var res = 0;
  885. var shift = 16;
  886. while (x > 1)
  887. {
  888. var temp = x >> shift;
  889. if (temp != 0)
  890. {
  891. x = temp;
  892. res += shift;
  893. }
  894. shift >>= 1;
  895. }
  896. return 31 - res;
  897. }
  898. }
  899. }