MathInstance.cs 40 KB

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