MathInstance.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  1. using System;
  2. using Jint.Collections;
  3. using Jint.Native.Number;
  4. using Jint.Native.Object;
  5. using Jint.Runtime;
  6. using Jint.Runtime.Descriptors;
  7. using Jint.Runtime.Interop;
  8. namespace Jint.Native.Math
  9. {
  10. public sealed class MathInstance : ObjectInstance
  11. {
  12. private Random _random;
  13. private MathInstance(Engine engine) : base(engine, "Math")
  14. {
  15. }
  16. public static MathInstance CreateMathObject(Engine engine)
  17. {
  18. var math = new MathInstance(engine)
  19. {
  20. _prototype = engine.Object.PrototypeObject
  21. };
  22. return math;
  23. }
  24. protected override void Initialize()
  25. {
  26. _properties = new StringDictionarySlim<PropertyDescriptor>(45)
  27. {
  28. ["abs"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "abs", Abs, 1, PropertyFlag.Configurable), true, false, true),
  29. ["acos"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "acos", Acos, 1, PropertyFlag.Configurable), true, false, true),
  30. ["acosh"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "acosh", Acosh, 1, PropertyFlag.Configurable), true, false, true),
  31. ["asin"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "asin", Asin, 1, PropertyFlag.Configurable), true, false, true),
  32. ["asinh"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "asinh", Asinh, 1, PropertyFlag.Configurable), true, false, true),
  33. ["atan"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "atan", Atan, 1, PropertyFlag.Configurable), true, false, true),
  34. ["atanh"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "atanh", Atanh, 1, PropertyFlag.Configurable), true, false, true),
  35. ["atan2"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "atan2", Atan2, 2, PropertyFlag.Configurable), true, false, true),
  36. ["ceil"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "ceil", Ceil, 1, PropertyFlag.Configurable), true, false, true),
  37. ["cos"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "cos", Cos, 1, PropertyFlag.Configurable), true, false, true),
  38. ["cosh"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "cosh", Cosh, 1, PropertyFlag.Configurable), true, false, true),
  39. ["exp"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "exp", Exp, 1, PropertyFlag.Configurable), true, false, true),
  40. ["expm1"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "expm1", Expm1, 1, PropertyFlag.Configurable), true, false, true),
  41. ["floor"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "floor", Floor, 1, PropertyFlag.Configurable), true, false, true),
  42. ["log"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "log", Log, 1, PropertyFlag.Configurable), true, false, true),
  43. ["log1p"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "log1p", Log1p, 1, PropertyFlag.Configurable), true, false, true),
  44. ["log2"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "log2", Log2, 1, PropertyFlag.Configurable), true, false, true),
  45. ["log10"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "log10", Log10, 1, PropertyFlag.Configurable), true, false, true),
  46. ["max"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "max", Max, 2, PropertyFlag.Configurable), true, false, true),
  47. ["min"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "min", Min, 2, PropertyFlag.Configurable), true, false, true),
  48. ["pow"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "pow", Pow, 2, PropertyFlag.Configurable), true, false, true),
  49. ["random"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "random", Random, 0, PropertyFlag.Configurable), true, false, true),
  50. ["round"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "round", Round, 1, PropertyFlag.Configurable), true, false, true),
  51. ["fround"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "fround", Fround, 1, PropertyFlag.Configurable), true, false, true),
  52. ["sin"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "sin", Sin, 1, PropertyFlag.Configurable), true, false, true),
  53. ["sinh"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "sinh", Sinh, 1, PropertyFlag.Configurable), true, false, true),
  54. ["sqrt"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "sqrt", Sqrt, 1, PropertyFlag.Configurable), true, false, true),
  55. ["tan"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "tan", Tan, 1, PropertyFlag.Configurable), true, false, true),
  56. ["tanh"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "tanh", Tanh, 1, PropertyFlag.Configurable), true, false, true),
  57. ["trunc"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "trunc", Truncate, 1, PropertyFlag.Configurable), true, false, true),
  58. ["sign"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "sign", Sign, 1, PropertyFlag.Configurable), true, false, true),
  59. ["cbrt"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "cbrt", Cbrt, 1, PropertyFlag.Configurable), true, false, true),
  60. ["hypot"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "hypot", Hypot, 2, PropertyFlag.Configurable), true, false, true),
  61. ["imul"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "imul", Imul, 2, PropertyFlag.Configurable), true, false, true),
  62. ["clz32"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "clz32", Clz32, 1, PropertyFlag.Configurable), true, false, true),
  63. ["E"] = new PropertyDescriptor(System.Math.E, false, false, false),
  64. ["LN10"] = new PropertyDescriptor(System.Math.Log(10), false, false, false),
  65. ["LN2"] = new PropertyDescriptor(System.Math.Log(2), false, false, false),
  66. ["LOG2E"] = new PropertyDescriptor(System.Math.Log(System.Math.E, 2), false, false, false),
  67. ["LOG10E"] = new PropertyDescriptor(System.Math.Log(System.Math.E, 10), false, false, false),
  68. ["PI"] = new PropertyDescriptor(System.Math.PI, false, false, false),
  69. ["SQRT1_2"] = new PropertyDescriptor(System.Math.Sqrt(0.5), false, false, false),
  70. ["SQRT2"] = new PropertyDescriptor(System.Math.Sqrt(2), false, false, false)
  71. };
  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. return System.Math.Ceiling(x);
  316. }
  317. private static JsValue Cos(JsValue thisObject, JsValue[] arguments)
  318. {
  319. var x = TypeConverter.ToNumber(arguments.At(0));
  320. if (double.IsNaN(x))
  321. {
  322. return JsNumber.DoubleNaN;
  323. }
  324. else if (NumberInstance.IsPositiveZero(x))
  325. {
  326. return 1;
  327. }
  328. else if (NumberInstance.IsNegativeZero(x))
  329. {
  330. return 1;
  331. }
  332. else if (double.IsInfinity(x))
  333. {
  334. return JsNumber.DoubleNaN;
  335. }
  336. return System.Math.Cos(x);
  337. }
  338. private static JsValue Cosh(JsValue thisObject, JsValue[] arguments)
  339. {
  340. var x = TypeConverter.ToNumber(arguments.At(0));
  341. if (double.IsNaN(x))
  342. {
  343. return JsNumber.DoubleNaN;
  344. }
  345. else if (NumberInstance.IsPositiveZero(x))
  346. {
  347. return 1;
  348. }
  349. else if (NumberInstance.IsNegativeZero(x))
  350. {
  351. return 1;
  352. }
  353. else if (double.IsInfinity(x))
  354. {
  355. return JsNumber.DoublePositiveInfinity;
  356. }
  357. return System.Math.Cosh(x);
  358. }
  359. private static JsValue Exp(JsValue thisObject, JsValue[] arguments)
  360. {
  361. var x = TypeConverter.ToNumber(arguments.At(0));
  362. if (double.IsNaN(x))
  363. {
  364. return JsNumber.DoubleNaN;
  365. }
  366. else if (NumberInstance.IsPositiveZero(x) || NumberInstance.IsNegativeZero(x))
  367. {
  368. return 1;
  369. }
  370. else if (double.IsPositiveInfinity(x))
  371. {
  372. return JsNumber.DoublePositiveInfinity;
  373. }
  374. else if (double.IsNegativeInfinity(x))
  375. {
  376. return JsNumber.PositiveZero;
  377. }
  378. return System.Math.Exp(x);
  379. }
  380. private static JsValue Expm1(JsValue thisObject, JsValue[] arguments)
  381. {
  382. var x = TypeConverter.ToNumber(arguments.At(0));
  383. if (double.IsNaN(x) || NumberInstance.IsPositiveZero(x) || NumberInstance.IsNegativeZero(x) || double.IsPositiveInfinity(x))
  384. {
  385. return arguments.At(0);
  386. }
  387. if (double.IsNegativeInfinity(x))
  388. {
  389. return JsNumber.DoubleNegativeOne;
  390. }
  391. return System.Math.Exp(x) - 1.0;
  392. }
  393. private static JsValue Floor(JsValue thisObject, JsValue[] arguments)
  394. {
  395. var x = TypeConverter.ToNumber(arguments.At(0));
  396. if (double.IsNaN(x))
  397. {
  398. return JsNumber.DoubleNaN;
  399. }
  400. else if (NumberInstance.IsPositiveZero(x))
  401. {
  402. return JsNumber.PositiveZero;
  403. }
  404. else if (NumberInstance.IsNegativeZero(x))
  405. {
  406. return JsNumber.NegativeZero;
  407. }
  408. else if (double.IsPositiveInfinity(x))
  409. {
  410. return JsNumber.DoublePositiveInfinity;
  411. }
  412. else if (double.IsNegativeInfinity(x))
  413. {
  414. return JsNumber.DoubleNegativeInfinity;
  415. }
  416. return System.Math.Floor(x);
  417. }
  418. private static JsValue Log(JsValue thisObject, JsValue[] arguments)
  419. {
  420. var x = TypeConverter.ToNumber(arguments.At(0));
  421. if (double.IsNaN(x))
  422. {
  423. return JsNumber.DoubleNaN;
  424. }
  425. if (x < 0)
  426. {
  427. return JsNumber.DoubleNaN;
  428. }
  429. else if (x == 0)
  430. {
  431. return JsNumber.DoubleNegativeInfinity;
  432. }
  433. else if (double.IsPositiveInfinity(x))
  434. {
  435. return JsNumber.DoublePositiveInfinity;
  436. }
  437. else if (x == 1)
  438. {
  439. return JsNumber.PositiveZero;
  440. }
  441. return System.Math.Log(x);
  442. }
  443. private static JsValue Log1p(JsValue thisObject, JsValue[] arguments)
  444. {
  445. var x = TypeConverter.ToNumber(arguments.At(0));
  446. if (double.IsNaN(x))
  447. {
  448. return JsNumber.DoubleNaN;
  449. }
  450. if (x < -1)
  451. {
  452. return JsNumber.DoubleNaN;
  453. }
  454. if (x == -1)
  455. {
  456. return JsNumber.DoubleNegativeInfinity;
  457. }
  458. if (x == 0 || double.IsPositiveInfinity(x))
  459. {
  460. return arguments.At(0);
  461. }
  462. return System.Math.Log(1 + x);
  463. }
  464. private static JsValue Log2(JsValue thisObject, JsValue[] arguments)
  465. {
  466. var x = TypeConverter.ToNumber(arguments.At(0));
  467. if (double.IsNaN(x))
  468. {
  469. return JsNumber.DoubleNaN;
  470. }
  471. if (x < 0)
  472. {
  473. return JsNumber.DoubleNaN;
  474. }
  475. else if (x == 0)
  476. {
  477. return JsNumber.DoubleNegativeInfinity;
  478. }
  479. else if (double.IsPositiveInfinity(x))
  480. {
  481. return JsNumber.DoublePositiveInfinity;
  482. }
  483. else if (x == 1)
  484. {
  485. return JsNumber.PositiveZero;
  486. }
  487. return System.Math.Log(x, 2);
  488. }
  489. private static JsValue Log10(JsValue thisObject, JsValue[] arguments)
  490. {
  491. var x = TypeConverter.ToNumber(arguments.At(0));
  492. if (double.IsNaN(x))
  493. {
  494. return JsNumber.DoubleNaN;
  495. }
  496. if (x < 0)
  497. {
  498. return JsNumber.DoubleNaN;
  499. }
  500. else if (x == 0)
  501. {
  502. return JsNumber.DoubleNegativeInfinity;
  503. }
  504. else if (double.IsPositiveInfinity(x))
  505. {
  506. return JsNumber.DoublePositiveInfinity;
  507. }
  508. else if (x == 1)
  509. {
  510. return JsNumber.PositiveZero;
  511. }
  512. return System.Math.Log10(x);
  513. }
  514. private static JsValue Max(JsValue thisObject, JsValue[] arguments)
  515. {
  516. if (arguments.Length == 0)
  517. {
  518. return JsNumber.DoubleNegativeInfinity;
  519. }
  520. double max = TypeConverter.ToNumber(arguments.At(0));
  521. if (double.IsNaN(max))
  522. {
  523. return JsNumber.DoubleNaN;
  524. }
  525. for (int i = 0; i < arguments.Length; i++)
  526. {
  527. var value = TypeConverter.ToNumber(arguments[i]);
  528. if (double.IsNaN(value))
  529. {
  530. return JsNumber.DoubleNaN;
  531. }
  532. if (max == 0 && value == 0)
  533. {
  534. max = NumberInstance.IsNegativeZero(value)
  535. ? max
  536. : value;
  537. }
  538. else
  539. {
  540. max = System.Math.Max(max, value);
  541. }
  542. }
  543. return max;
  544. }
  545. private static JsValue Min(JsValue thisObject, JsValue[] arguments)
  546. {
  547. if (arguments.Length == 0)
  548. {
  549. return JsNumber.DoublePositiveInfinity;
  550. }
  551. double min = TypeConverter.ToNumber(arguments.At(0));
  552. for (int i = 0; i < arguments.Length; i++)
  553. {
  554. var value = TypeConverter.ToNumber(arguments[i]);
  555. if (min == 0 && value == 0)
  556. {
  557. min = NumberInstance.IsNegativeZero(min)
  558. ? min
  559. : value;
  560. }
  561. else
  562. {
  563. min = System.Math.Min(min, value);
  564. }
  565. }
  566. return min;
  567. }
  568. private static JsValue Pow(JsValue thisObject, JsValue[] arguments)
  569. {
  570. var x = TypeConverter.ToNumber(arguments.At(0));
  571. var y = TypeConverter.ToNumber(arguments.At(1));
  572. // check easy case where values are valid
  573. if (x > 1 && y > 1 && x < int.MaxValue && y < int.MaxValue)
  574. {
  575. return System.Math.Pow(x, y);
  576. }
  577. if (y == 0)
  578. {
  579. return 1;
  580. }
  581. return HandlePowUnlikely(y, x);
  582. }
  583. private static JsValue HandlePowUnlikely(double y, double x)
  584. {
  585. if (double.IsNaN(y))
  586. {
  587. return JsNumber.DoubleNaN;
  588. }
  589. if (double.IsNaN(x))
  590. {
  591. return JsNumber.DoubleNaN;
  592. }
  593. var absX = System.Math.Abs(x);
  594. if (absX > 1)
  595. {
  596. if (double.IsPositiveInfinity(y))
  597. {
  598. return JsNumber.DoublePositiveInfinity;
  599. }
  600. if (double.IsNegativeInfinity(y))
  601. {
  602. return JsNumber.PositiveZero;
  603. }
  604. }
  605. if (absX == 1)
  606. {
  607. if (double.IsInfinity(y))
  608. {
  609. return JsNumber.DoubleNaN;
  610. }
  611. }
  612. if (absX < 1)
  613. {
  614. if (double.IsPositiveInfinity(y))
  615. {
  616. return 0;
  617. }
  618. if (double.IsNegativeInfinity(y))
  619. {
  620. return JsNumber.DoublePositiveInfinity;
  621. }
  622. }
  623. if (double.IsPositiveInfinity(x))
  624. {
  625. if (y > 0)
  626. {
  627. return JsNumber.DoublePositiveInfinity;
  628. }
  629. if (y < 0)
  630. {
  631. return JsNumber.PositiveZero;
  632. }
  633. }
  634. if (double.IsNegativeInfinity(x))
  635. {
  636. if (y > 0)
  637. {
  638. if (System.Math.Abs(y % 2).Equals(1))
  639. {
  640. return JsNumber.DoubleNegativeInfinity;
  641. }
  642. return JsNumber.DoublePositiveInfinity;
  643. }
  644. if (y < 0)
  645. {
  646. if (System.Math.Abs(y % 2).Equals(1))
  647. {
  648. return JsNumber.NegativeZero;
  649. }
  650. return JsNumber.PositiveZero;
  651. }
  652. }
  653. if (NumberInstance.IsPositiveZero(x))
  654. {
  655. // If x is +0 and y>0, the result is +0.
  656. if (y > 0)
  657. {
  658. return 0;
  659. }
  660. // If x is +0 and y<0, the result is +∞.
  661. if (y < 0)
  662. {
  663. return JsNumber.DoublePositiveInfinity;
  664. }
  665. }
  666. if (NumberInstance.IsNegativeZero(x))
  667. {
  668. if (y > 0)
  669. {
  670. // If x is −0 and y>0 and y is an odd integer, the result is −0.
  671. if (System.Math.Abs(y % 2).Equals(1))
  672. {
  673. return JsNumber.NegativeZero;
  674. }
  675. // If x is −0 and y>0 and y is not an odd integer, the result is +0.
  676. return JsNumber.PositiveZero;
  677. }
  678. if (y < 0)
  679. {
  680. // If x is −0 and y<0 and y is an odd integer, the result is −∞.
  681. if (System.Math.Abs(y % 2).Equals(1))
  682. {
  683. return JsNumber.DoubleNegativeInfinity;
  684. }
  685. // If x is −0 and y<0 and y is not an odd integer, the result is +∞.
  686. return JsNumber.DoublePositiveInfinity;
  687. }
  688. }
  689. // If x<0 and x is finite and y is finite and y is not an integer, the result is NaN.
  690. if (x < 0 && !double.IsInfinity(x) && !double.IsInfinity(y) && !y.Equals((int) y))
  691. {
  692. return JsNumber.DoubleNaN;
  693. }
  694. return System.Math.Pow(x, y);
  695. }
  696. private JsValue Random(JsValue thisObject, JsValue[] arguments)
  697. {
  698. if(_random == null)
  699. {
  700. _random = new Random();
  701. }
  702. return _random.NextDouble();
  703. }
  704. private static JsValue Round(JsValue thisObject, JsValue[] arguments)
  705. {
  706. var x = TypeConverter.ToNumber(arguments.At(0));
  707. var round = System.Math.Round(x);
  708. if (round.Equals(x - 0.5))
  709. {
  710. return round + 1;
  711. }
  712. return round;
  713. }
  714. private static JsValue Fround(JsValue thisObject, JsValue[] arguments)
  715. {
  716. var x = TypeConverter.ToNumber(arguments.At(0));
  717. return (double) (float) x;
  718. }
  719. private static JsValue Sin(JsValue thisObject, JsValue[] arguments)
  720. {
  721. var x = TypeConverter.ToNumber(arguments.At(0));
  722. if (double.IsNaN(x))
  723. {
  724. return JsNumber.DoubleNaN;
  725. }
  726. else if (NumberInstance.IsPositiveZero(x))
  727. {
  728. return JsNumber.PositiveZero;
  729. }
  730. else if (NumberInstance.IsNegativeZero(x))
  731. {
  732. return JsNumber.NegativeZero;
  733. }
  734. else if (double.IsInfinity(x))
  735. {
  736. return JsNumber.DoubleNaN;
  737. }
  738. return System.Math.Sin(x);
  739. }
  740. private static JsValue Sinh(JsValue thisObject, JsValue[] arguments)
  741. {
  742. var x = TypeConverter.ToNumber(arguments.At(0));
  743. if (double.IsNaN(x))
  744. {
  745. return JsNumber.DoubleNaN;
  746. }
  747. else if (NumberInstance.IsPositiveZero(x))
  748. {
  749. return JsNumber.PositiveZero;
  750. }
  751. else if (NumberInstance.IsNegativeZero(x))
  752. {
  753. return JsNumber.NegativeZero;
  754. }
  755. else if (double.IsNegativeInfinity(x))
  756. {
  757. return JsNumber.DoubleNegativeInfinity;
  758. }
  759. else if (double.IsPositiveInfinity(x))
  760. {
  761. return JsNumber.DoublePositiveInfinity;
  762. }
  763. return System.Math.Sinh(x);
  764. }
  765. private static JsValue Sqrt(JsValue thisObject, JsValue[] arguments)
  766. {
  767. var x = TypeConverter.ToNumber(arguments.At(0));
  768. return System.Math.Sqrt(x);
  769. }
  770. private static JsValue Tan(JsValue thisObject, JsValue[] arguments)
  771. {
  772. var x = TypeConverter.ToNumber(arguments.At(0));
  773. return System.Math.Tan(x);
  774. }
  775. private static JsValue Tanh(JsValue thisObject, JsValue[] arguments)
  776. {
  777. var x = TypeConverter.ToNumber(arguments.At(0));
  778. return System.Math.Tanh(x);
  779. }
  780. private static JsValue Truncate(JsValue thisObject, JsValue[] arguments)
  781. {
  782. var x = TypeConverter.ToNumber(arguments.At(0));
  783. if (double.IsNaN(x))
  784. {
  785. return JsNumber.DoubleNaN;
  786. }
  787. if (NumberInstance.IsPositiveZero(x) || NumberInstance.IsNegativeZero(x))
  788. {
  789. return x;
  790. }
  791. if (double.IsPositiveInfinity(x))
  792. {
  793. return JsNumber.DoublePositiveInfinity;
  794. }
  795. if (double.IsNegativeInfinity(x))
  796. {
  797. return JsNumber.DoubleNegativeInfinity;
  798. }
  799. return System.Math.Truncate(x);
  800. }
  801. private static JsValue Sign(JsValue thisObject, JsValue[] arguments)
  802. {
  803. var x = TypeConverter.ToNumber(arguments.At(0));
  804. if (double.IsNaN(x))
  805. {
  806. return JsNumber.DoubleNaN;
  807. }
  808. if (NumberInstance.IsPositiveZero(x) || NumberInstance.IsNegativeZero(x))
  809. {
  810. return x;
  811. }
  812. if (double.IsPositiveInfinity(x))
  813. {
  814. return 1;
  815. }
  816. if (double.IsNegativeInfinity(x))
  817. {
  818. return -1;
  819. }
  820. return System.Math.Sign(x);
  821. }
  822. private static JsValue Cbrt(JsValue thisObject, JsValue[] arguments)
  823. {
  824. var x = TypeConverter.ToNumber(arguments.At(0));
  825. if (double.IsNaN(x))
  826. {
  827. return JsNumber.DoubleNaN;
  828. }
  829. else if (NumberInstance.IsPositiveZero(x) || NumberInstance.IsNegativeZero(x))
  830. {
  831. return x;
  832. }
  833. else if (double.IsPositiveInfinity(x))
  834. {
  835. return JsNumber.DoublePositiveInfinity;
  836. }
  837. else if (double.IsNegativeInfinity(x))
  838. {
  839. return JsNumber.DoubleNegativeInfinity;
  840. }
  841. if (System.Math.Sign(x) >= 0)
  842. {
  843. return System.Math.Pow(x, 1.0/3.0);
  844. }
  845. return -1 * System.Math.Pow(System.Math.Abs(x), 1.0 / 3.0);
  846. }
  847. private static JsValue Hypot(JsValue thisObject, JsValue[] arguments)
  848. {
  849. double y = 0;
  850. for (int i = 0; i < arguments.Length; ++i)
  851. {
  852. var number = TypeConverter.ToNumber(arguments[i]);
  853. if (double.IsInfinity(number))
  854. {
  855. return JsNumber.DoublePositiveInfinity;
  856. }
  857. y += number * number;
  858. }
  859. return System.Math.Sqrt(y);
  860. }
  861. private static JsValue Imul(JsValue thisObject, JsValue[] arguments)
  862. {
  863. var x = TypeConverter.ToInt32(arguments.At(0));
  864. var y = TypeConverter.ToInt32(arguments.At(1));
  865. return x * y;
  866. }
  867. private static JsValue Clz32(JsValue thisObject, JsValue[] arguments)
  868. {
  869. var x = TypeConverter.ToInt32(arguments.At(0));
  870. if (x < 0)
  871. {
  872. return 0;
  873. }
  874. if (x == 0)
  875. {
  876. return 32;
  877. }
  878. var res = 0;
  879. var shift = 16;
  880. while (x > 1)
  881. {
  882. var temp = x >> shift;
  883. if (temp != 0)
  884. {
  885. x = temp;
  886. res += shift;
  887. }
  888. shift >>= 1;
  889. }
  890. return 31 - res;
  891. }
  892. }
  893. }