123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209 |
- using Jint.Native.Number;
- using Jint.Native.Object;
- using Jint.Native.Symbol;
- using Jint.Runtime;
- using Jint.Runtime.Descriptors;
- using Jint.Runtime.Interop;
- namespace Jint.Native.Math;
- internal sealed class MathInstance : ObjectInstance
- {
- private Random? _random;
- internal MathInstance(Engine engine, ObjectPrototype objectPrototype) : base(engine)
- {
- _prototype = objectPrototype;
- }
- protected override void Initialize()
- {
- var properties = new PropertyDictionary(45, checkExistingKeys: false)
- {
- ["E"] = new PropertyDescriptor(System.Math.E, PropertyFlag.AllForbidden),
- ["LN10"] = new PropertyDescriptor(System.Math.Log(10), PropertyFlag.AllForbidden),
- ["LN2"] = new PropertyDescriptor(System.Math.Log(2), PropertyFlag.AllForbidden),
- ["LOG10E"] = new PropertyDescriptor(System.Math.Log(System.Math.E, 10), PropertyFlag.AllForbidden),
- ["LOG2E"] = new PropertyDescriptor(System.Math.Log(System.Math.E, 2), PropertyFlag.AllForbidden),
- ["PI"] = new PropertyDescriptor(System.Math.PI, PropertyFlag.AllForbidden),
- ["SQRT1_2"] = new PropertyDescriptor(System.Math.Sqrt(0.5), PropertyFlag.AllForbidden),
- ["SQRT2"] = new PropertyDescriptor(System.Math.Sqrt(2), PropertyFlag.AllForbidden),
- ["abs"] = new PropertyDescriptor(new ClrFunction(Engine, "abs", Abs, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
- ["acos"] = new PropertyDescriptor(new ClrFunction(Engine, "acos", Acos, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
- ["acosh"] = new PropertyDescriptor(new ClrFunction(Engine, "acosh", Acosh, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
- ["asin"] = new PropertyDescriptor(new ClrFunction(Engine, "asin", Asin, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
- ["asinh"] = new PropertyDescriptor(new ClrFunction(Engine, "asinh", Asinh, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
- ["atan"] = new PropertyDescriptor(new ClrFunction(Engine, "atan", Atan, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
- ["atan2"] = new PropertyDescriptor(new ClrFunction(Engine, "atan2", Atan2, 2, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
- ["atanh"] = new PropertyDescriptor(new ClrFunction(Engine, "atanh", Atanh, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
- ["cbrt"] = new PropertyDescriptor(new ClrFunction(Engine, "cbrt", Cbrt, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
- ["ceil"] = new PropertyDescriptor(new ClrFunction(Engine, "ceil", Ceil, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
- ["clz32"] = new PropertyDescriptor(new ClrFunction(Engine, "clz32", Clz32, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
- ["cos"] = new PropertyDescriptor(new ClrFunction(Engine, "cos", Cos, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
- ["cosh"] = new PropertyDescriptor(new ClrFunction(Engine, "cosh", Cosh, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
- ["exp"] = new PropertyDescriptor(new ClrFunction(Engine, "exp", Exp, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
- ["expm1"] = new PropertyDescriptor(new ClrFunction(Engine, "expm1", Expm1, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
- ["f16round"] = new PropertyDescriptor(new ClrFunction(Engine, "f16round", F16Round, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
- ["floor"] = new PropertyDescriptor(new ClrFunction(Engine, "floor", Floor, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
- ["fround"] = new PropertyDescriptor(new ClrFunction(Engine, "fround", Fround, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
- ["hypot"] = new PropertyDescriptor(new ClrFunction(Engine, "hypot", Hypot, 2, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
- ["imul"] = new PropertyDescriptor(new ClrFunction(Engine, "imul", Imul, 2, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
- ["log"] = new PropertyDescriptor(new ClrFunction(Engine, "log", Log, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
- ["log10"] = new PropertyDescriptor(new ClrFunction(Engine, "log10", Log10, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
- ["log1p"] = new PropertyDescriptor(new ClrFunction(Engine, "log1p", Log1p, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
- ["log2"] = new PropertyDescriptor(new ClrFunction(Engine, "log2", Log2, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
- ["max"] = new PropertyDescriptor(new ClrFunction(Engine, "max", Max, 2, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
- ["min"] = new PropertyDescriptor(new ClrFunction(Engine, "min", Min, 2, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
- ["pow"] = new PropertyDescriptor(new ClrFunction(Engine, "pow", Pow, 2, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
- ["random"] = new PropertyDescriptor(new ClrFunction(Engine, "random", Random, 0, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
- ["round"] = new PropertyDescriptor(new ClrFunction(Engine, "round", Round, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
- ["sign"] = new PropertyDescriptor(new ClrFunction(Engine, "sign", Sign, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
- ["sin"] = new PropertyDescriptor(new ClrFunction(Engine, "sin", Sin, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
- ["sinh"] = new PropertyDescriptor(new ClrFunction(Engine, "sinh", Sinh, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
- ["sumPrecise"] = new PropertyDescriptor(new ClrFunction(Engine, "sumPrecise", SumPrecise, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
- ["sqrt"] = new PropertyDescriptor(new ClrFunction(Engine, "sqrt", Sqrt, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
- ["tan"] = new PropertyDescriptor(new ClrFunction(Engine, "tan", Tan, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
- ["tanh"] = new PropertyDescriptor(new ClrFunction(Engine, "tanh", Tanh, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
- ["trunc"] = new PropertyDescriptor(new ClrFunction(Engine, "trunc", Truncate, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable),
- };
- SetProperties(properties);
- var symbols = new SymbolDictionary(1)
- {
- [GlobalSymbolRegistry.ToStringTag] = new PropertyDescriptor(new JsString("Math"), PropertyFlag.Configurable)
- };
- SetSymbols(symbols);
- }
- private static JsValue Abs(JsValue thisObject, JsCallArguments arguments)
- {
- var x = TypeConverter.ToNumber(arguments.At(0));
- if (double.IsNaN(x))
- {
- return JsNumber.DoubleNaN;
- }
- else if (NumberInstance.IsNegativeZero(x))
- {
- return JsNumber.PositiveZero;
- }
- else if (double.IsInfinity(x))
- {
- return JsNumber.DoublePositiveInfinity;
- }
- return System.Math.Abs(x);
- }
- private static JsValue Acos(JsValue thisObject, JsCallArguments arguments)
- {
- var x = TypeConverter.ToNumber(arguments.At(0));
- if (double.IsNaN(x) || (x > 1) || (x < -1))
- {
- return JsNumber.DoubleNaN;
- }
- else if (x == 1)
- {
- return 0;
- }
- return System.Math.Acos(x);
- }
- private static JsValue Acosh(JsValue thisObject, JsCallArguments arguments)
- {
- var x = TypeConverter.ToNumber(arguments.At(0));
- if (double.IsNaN(x) || x < 1)
- {
- return JsNumber.DoubleNaN;
- }
- return System.Math.Log(x + System.Math.Sqrt(x * x - 1.0));
- }
- private static JsValue Asin(JsValue thisObject, JsCallArguments arguments)
- {
- var x = TypeConverter.ToNumber(arguments.At(0));
- if (double.IsNaN(x) || (x > 1) || (x < -1))
- {
- return JsNumber.DoubleNaN;
- }
- else if (NumberInstance.IsPositiveZero(x) || NumberInstance.IsNegativeZero(x))
- {
- return x;
- }
- return System.Math.Asin(x);
- }
- private static JsValue Asinh(JsValue thisObject, JsCallArguments arguments)
- {
- var x = TypeConverter.ToNumber(arguments.At(0));
- if (double.IsInfinity(x) || NumberInstance.IsPositiveZero(x) || NumberInstance.IsNegativeZero(x))
- {
- return x;
- }
- return System.Math.Log(x + System.Math.Sqrt(x * x + 1.0));
- }
- private static JsValue Atan(JsValue thisObject, JsCallArguments arguments)
- {
- var x = TypeConverter.ToNumber(arguments.At(0));
- if (double.IsNaN(x))
- {
- return JsNumber.DoubleNaN;
- }
- else if (NumberInstance.IsPositiveZero(x) || NumberInstance.IsNegativeZero(x))
- {
- return x;
- }
- else if (double.IsPositiveInfinity(x))
- {
- return System.Math.PI / 2;
- }
- else if (double.IsNegativeInfinity(x))
- {
- return -System.Math.PI / 2;
- }
- return System.Math.Atan(x);
- }
- private static JsValue Atanh(JsValue thisObject, JsCallArguments arguments)
- {
- var x = TypeConverter.ToNumber(arguments.At(0));
- if (double.IsNaN(x))
- {
- return JsNumber.DoubleNaN;
- }
- if (NumberInstance.IsPositiveZero(x) || NumberInstance.IsNegativeZero(x))
- {
- return x;
- }
- return 0.5 * System.Math.Log((1.0 + x) / (1.0 - x));
- }
- private static JsValue Atan2(JsValue thisObject, JsCallArguments arguments)
- {
- var y = TypeConverter.ToNumber(arguments.At(0));
- var x = TypeConverter.ToNumber(arguments.At(1));
- // If either x or y is NaN, the result is NaN.
- if (double.IsNaN(x) || double.IsNaN(y))
- {
- return JsNumber.DoubleNaN;
- }
- if (y > 0 && x.Equals(0))
- {
- return System.Math.PI / 2;
- }
- if (NumberInstance.IsPositiveZero(y))
- {
- // If y is +0 and x>0, the result is +0.
- if (x > 0)
- {
- return JsNumber.PositiveZero;
- }
- // If y is +0 and x is +0, the result is +0.
- if (NumberInstance.IsPositiveZero(x))
- {
- return JsNumber.PositiveZero;
- }
- // If y is +0 and x is −0, the result is an implementation-dependent approximation to +π.
- if (NumberInstance.IsNegativeZero(x))
- {
- return JsNumber.PI;
- }
- // If y is +0 and x<0, the result is an implementation-dependent approximation to +π.
- if (x < 0)
- {
- return JsNumber.PI;
- }
- }
- if (NumberInstance.IsNegativeZero(y))
- {
- // If y is −0 and x>0, the result is −0.
- if (x > 0)
- {
- return JsNumber.NegativeZero;
- }
- // If y is −0 and x is +0, the result is −0.
- if (NumberInstance.IsPositiveZero(x))
- {
- return JsNumber.NegativeZero;
- }
- // If y is −0 and x is −0, the result is an implementation-dependent approximation to −π.
- if (NumberInstance.IsNegativeZero(x))
- {
- return -System.Math.PI;
- }
- // If y is −0 and x<0, the result is an implementation-dependent approximation to −π.
- if (x < 0)
- {
- return -System.Math.PI;
- }
- }
- // If y<0 and x is +0, the result is an implementation-dependent approximation to −π/2.
- // If y<0 and x is −0, the result is an implementation-dependent approximation to −π/2.
- if (y < 0 && x.Equals(0))
- {
- return -System.Math.PI / 2;
- }
- // If y>0 and y is finite and x is +∞, the result is +0.
- if (y > 0 && !double.IsInfinity(y))
- {
- if (double.IsPositiveInfinity(x))
- {
- return JsNumber.PositiveZero;
- }
- // If y>0 and y is finite and x is −∞, the result if an implementation-dependent approximation to +π.
- if (double.IsNegativeInfinity(x))
- {
- return JsNumber.PI;
- }
- }
- // If y<0 and y is finite and x is +∞, the result is −0.
- // If y<0 and y is finite and x is −∞, the result is an implementation-dependent approximation to −π.
- if (y < 0 && !double.IsInfinity(y))
- {
- if (double.IsPositiveInfinity(x))
- {
- return JsNumber.NegativeZero;
- }
- // If y>0 and y is finite and x is −∞, the result if an implementation-dependent approximation to +π.
- if (double.IsNegativeInfinity(x))
- {
- return -System.Math.PI;
- }
- }
- // If y is +∞ and x is finite, the result is an implementation-dependent approximation to +π/2.
- if (double.IsPositiveInfinity(y) && !double.IsInfinity(x))
- {
- return System.Math.PI / 2;
- }
- // If y is −∞ and x is finite, the result is an implementation-dependent approximation to −π/2.
- if (double.IsNegativeInfinity(y) && !double.IsInfinity(x))
- {
- return -System.Math.PI / 2;
- }
- // If y is +∞ and x is +∞, the result is an implementation-dependent approximation to +π/4.
- if (double.IsPositiveInfinity(y) && double.IsPositiveInfinity(x))
- {
- return System.Math.PI / 4;
- }
- // If y is +∞ and x is −∞, the result is an implementation-dependent approximation to +3π/4.
- if (double.IsPositiveInfinity(y) && double.IsNegativeInfinity(x))
- {
- return 3 * System.Math.PI / 4;
- }
- // If y is −∞ and x is +∞, the result is an implementation-dependent approximation to −π/4.
- if (double.IsNegativeInfinity(y) && double.IsPositiveInfinity(x))
- {
- return -System.Math.PI / 4;
- }
- // If y is −∞ and x is −∞, the result is an implementation-dependent approximation to −3π/4.
- if (double.IsNegativeInfinity(y) && double.IsNegativeInfinity(x))
- {
- return -3 * System.Math.PI / 4;
- }
- return System.Math.Atan2(y, x);
- }
- private static JsValue Ceil(JsValue thisObject, JsCallArguments arguments)
- {
- var x = TypeConverter.ToNumber(arguments.At(0));
- if (double.IsNaN(x))
- {
- return JsNumber.DoubleNaN;
- }
- else if (NumberInstance.IsPositiveZero(x))
- {
- return JsNumber.PositiveZero;
- }
- else if (NumberInstance.IsNegativeZero(x))
- {
- return JsNumber.NegativeZero;
- }
- else if (double.IsPositiveInfinity(x))
- {
- return JsNumber.DoublePositiveInfinity;
- }
- else if (double.IsNegativeInfinity(x))
- {
- return JsNumber.DoubleNegativeInfinity;
- }
- #if NETFRAMEWORK
- if (x < 0 && x > -1)
- {
- return JsNumber.NegativeZero;
- }
- #endif
- return System.Math.Ceiling(x);
- }
- private static JsValue Cos(JsValue thisObject, JsCallArguments arguments)
- {
- var x = TypeConverter.ToNumber(arguments.At(0));
- if (double.IsNaN(x))
- {
- return JsNumber.DoubleNaN;
- }
- else if (NumberInstance.IsPositiveZero(x))
- {
- return 1;
- }
- else if (NumberInstance.IsNegativeZero(x))
- {
- return 1;
- }
- else if (double.IsInfinity(x))
- {
- return JsNumber.DoubleNaN;
- }
- return System.Math.Cos(x);
- }
- private static JsValue Cosh(JsValue thisObject, JsCallArguments arguments)
- {
- var x = TypeConverter.ToNumber(arguments.At(0));
- if (double.IsNaN(x))
- {
- return JsNumber.DoubleNaN;
- }
- else if (NumberInstance.IsPositiveZero(x))
- {
- return 1;
- }
- else if (NumberInstance.IsNegativeZero(x))
- {
- return 1;
- }
- else if (double.IsInfinity(x))
- {
- return JsNumber.DoublePositiveInfinity;
- }
- return System.Math.Cosh(x);
- }
- private static JsValue Exp(JsValue thisObject, JsCallArguments arguments)
- {
- var x = TypeConverter.ToNumber(arguments.At(0));
- if (double.IsNaN(x))
- {
- return JsNumber.DoubleNaN;
- }
- else if (NumberInstance.IsPositiveZero(x) || NumberInstance.IsNegativeZero(x))
- {
- return 1;
- }
- else if (double.IsPositiveInfinity(x))
- {
- return JsNumber.DoublePositiveInfinity;
- }
- else if (double.IsNegativeInfinity(x))
- {
- return JsNumber.PositiveZero;
- }
- return System.Math.Exp(x);
- }
- private static JsValue Expm1(JsValue thisObject, JsCallArguments arguments)
- {
- var x = TypeConverter.ToNumber(arguments.At(0));
- if (double.IsNaN(x) || NumberInstance.IsPositiveZero(x) || NumberInstance.IsNegativeZero(x) || double.IsPositiveInfinity(x))
- {
- return arguments.At(0);
- }
- if (double.IsNegativeInfinity(x))
- {
- return JsNumber.DoubleNegativeOne;
- }
- return System.Math.Exp(x) - 1.0;
- }
- private static JsValue Floor(JsValue thisObject, JsCallArguments arguments)
- {
- var x = TypeConverter.ToNumber(arguments.At(0));
- if (double.IsNaN(x))
- {
- return JsNumber.DoubleNaN;
- }
- else if (NumberInstance.IsPositiveZero(x))
- {
- return JsNumber.PositiveZero;
- }
- else if (NumberInstance.IsNegativeZero(x))
- {
- return JsNumber.NegativeZero;
- }
- else if (double.IsPositiveInfinity(x))
- {
- return JsNumber.DoublePositiveInfinity;
- }
- else if (double.IsNegativeInfinity(x))
- {
- return JsNumber.DoubleNegativeInfinity;
- }
- return System.Math.Floor(x);
- }
- private static JsValue Log(JsValue thisObject, JsCallArguments arguments)
- {
- var x = TypeConverter.ToNumber(arguments.At(0));
- if (double.IsNaN(x))
- {
- return JsNumber.DoubleNaN;
- }
- if (x < 0)
- {
- return JsNumber.DoubleNaN;
- }
- else if (x == 0)
- {
- return JsNumber.DoubleNegativeInfinity;
- }
- else if (double.IsPositiveInfinity(x))
- {
- return JsNumber.DoublePositiveInfinity;
- }
- else if (x == 1)
- {
- return JsNumber.PositiveZero;
- }
- return System.Math.Log(x);
- }
- private static JsValue Log1p(JsValue thisObject, JsCallArguments arguments)
- {
- var x = TypeConverter.ToNumber(arguments.At(0));
- if (double.IsNaN(x))
- {
- return JsNumber.DoubleNaN;
- }
- if (x < -1)
- {
- return JsNumber.DoubleNaN;
- }
- if (x == -1)
- {
- return JsNumber.DoubleNegativeInfinity;
- }
- if (x == 0 || double.IsPositiveInfinity(x))
- {
- return arguments.At(0);
- }
- return System.Math.Log(1 + x);
- }
- private static JsValue Log2(JsValue thisObject, JsCallArguments arguments)
- {
- var x = TypeConverter.ToNumber(arguments.At(0));
- if (double.IsNaN(x))
- {
- return JsNumber.DoubleNaN;
- }
- if (x < 0)
- {
- return JsNumber.DoubleNaN;
- }
- else if (x == 0)
- {
- return JsNumber.DoubleNegativeInfinity;
- }
- else if (double.IsPositiveInfinity(x))
- {
- return JsNumber.DoublePositiveInfinity;
- }
- else if (x == 1)
- {
- return JsNumber.PositiveZero;
- }
- return System.Math.Log(x, 2);
- }
- private static JsValue Log10(JsValue thisObject, JsCallArguments arguments)
- {
- var x = TypeConverter.ToNumber(arguments.At(0));
- if (double.IsNaN(x))
- {
- return JsNumber.DoubleNaN;
- }
- if (x < 0)
- {
- return JsNumber.DoubleNaN;
- }
- else if (x == 0)
- {
- return JsNumber.DoubleNegativeInfinity;
- }
- else if (double.IsPositiveInfinity(x))
- {
- return JsNumber.DoublePositiveInfinity;
- }
- else if (x == 1)
- {
- return JsNumber.PositiveZero;
- }
- return System.Math.Log10(x);
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-math.max
- /// </summary>
- private static JsValue Max(JsValue thisObject, JsCallArguments arguments)
- {
- if (arguments.Length == 0)
- {
- return JsNumber.DoubleNegativeInfinity;
- }
- var highest = double.NegativeInfinity;
- foreach (var number in Coerced(arguments))
- {
- if (double.IsNaN(number))
- {
- return JsNumber.DoubleNaN;
- }
- if (NumberInstance.IsPositiveZero(number) && NumberInstance.IsNegativeZero(highest))
- {
- highest = 0;
- }
- if (number > highest)
- {
- highest = number;
- }
- }
- return highest;
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-math.min
- /// </summary>
- private static JsValue Min(JsValue thisObject, JsCallArguments arguments)
- {
- if (arguments.Length == 0)
- {
- return JsNumber.DoublePositiveInfinity;
- }
- var lowest = double.PositiveInfinity;
- foreach (var number in Coerced(arguments))
- {
- if (double.IsNaN(number))
- {
- return JsNumber.DoubleNaN;
- }
- if (NumberInstance.IsNegativeZero(number) && NumberInstance.IsPositiveZero(lowest))
- {
- lowest = JsNumber.NegativeZero._value;
- }
- if (number < lowest)
- {
- lowest = number;
- }
- }
- return lowest;
- }
- private static JsValue Pow(JsValue thisObject, JsCallArguments arguments)
- {
- var x = TypeConverter.ToNumber(arguments.At(0));
- var y = TypeConverter.ToNumber(arguments.At(1));
- // check easy case where values are valid
- if (x > 1 && y > 1 && x < int.MaxValue && y < int.MaxValue)
- {
- return System.Math.Pow(x, y);
- }
- if (y == 0)
- {
- return 1;
- }
- return HandlePowUnlikely(y, x);
- }
- private static JsValue HandlePowUnlikely(double y, double x)
- {
- if (double.IsNaN(y))
- {
- return JsNumber.DoubleNaN;
- }
- if (double.IsNaN(x))
- {
- return JsNumber.DoubleNaN;
- }
- var absX = System.Math.Abs(x);
- if (absX > 1)
- {
- if (double.IsPositiveInfinity(y))
- {
- return JsNumber.DoublePositiveInfinity;
- }
- if (double.IsNegativeInfinity(y))
- {
- return JsNumber.PositiveZero;
- }
- }
- if (absX == 1)
- {
- if (double.IsInfinity(y))
- {
- return JsNumber.DoubleNaN;
- }
- }
- if (absX < 1)
- {
- if (double.IsPositiveInfinity(y))
- {
- return 0;
- }
- if (double.IsNegativeInfinity(y))
- {
- return JsNumber.DoublePositiveInfinity;
- }
- }
- if (double.IsPositiveInfinity(x))
- {
- if (y > 0)
- {
- return JsNumber.DoublePositiveInfinity;
- }
- if (y < 0)
- {
- return JsNumber.PositiveZero;
- }
- }
- if (double.IsNegativeInfinity(x))
- {
- if (y > 0)
- {
- if (System.Math.Abs(y % 2).Equals(1))
- {
- return JsNumber.DoubleNegativeInfinity;
- }
- return JsNumber.DoublePositiveInfinity;
- }
- if (y < 0)
- {
- if (System.Math.Abs(y % 2).Equals(1))
- {
- return JsNumber.NegativeZero;
- }
- return JsNumber.PositiveZero;
- }
- }
- if (NumberInstance.IsPositiveZero(x))
- {
- // If x is +0 and y>0, the result is +0.
- if (y > 0)
- {
- return 0;
- }
- // If x is +0 and y<0, the result is +∞.
- if (y < 0)
- {
- return JsNumber.DoublePositiveInfinity;
- }
- }
- if (NumberInstance.IsNegativeZero(x))
- {
- if (y > 0)
- {
- // If x is −0 and y>0 and y is an odd integer, the result is −0.
- if (System.Math.Abs(y % 2).Equals(1))
- {
- return JsNumber.NegativeZero;
- }
- // If x is −0 and y>0 and y is not an odd integer, the result is +0.
- return JsNumber.PositiveZero;
- }
- if (y < 0)
- {
- // If x is −0 and y<0 and y is an odd integer, the result is −∞.
- if (System.Math.Abs(y % 2).Equals(1))
- {
- return JsNumber.DoubleNegativeInfinity;
- }
- // If x is −0 and y<0 and y is not an odd integer, the result is +∞.
- return JsNumber.DoublePositiveInfinity;
- }
- }
- // If x<0 and x is finite and y is finite and y is not an integer, the result is NaN.
- if (x < 0 && !double.IsInfinity(x) && !double.IsInfinity(y) && !y.Equals((int) y))
- {
- return JsNumber.DoubleNaN;
- }
- return System.Math.Pow(x, y);
- }
- private JsValue Random(JsValue thisObject, JsCallArguments arguments)
- {
- if (_random == null)
- {
- _random = new Random();
- }
- return _random.NextDouble();
- }
- private static JsValue Round(JsValue thisObject, JsCallArguments arguments)
- {
- var x = TypeConverter.ToNumber(arguments.At(0));
- var round = System.Math.Round(x);
- if (round.Equals(x - 0.5))
- {
- return round + 1;
- }
- return round;
- }
- private static JsValue Fround(JsValue thisObject, JsCallArguments arguments)
- {
- var x = arguments.At(0);
- var n = TypeConverter.ToNumber(x);
- return (double) (float) n;
- }
- /// <summary>
- /// https://tc39.es/proposal-float16array/#sec-math.f16round
- /// </summary>
- private static JsValue F16Round(JsValue thisObject, JsCallArguments arguments)
- {
- #if SUPPORTS_HALF
- var x = arguments.At(0);
- var n = TypeConverter.ToNumber(x);
- if (double.IsNaN(n))
- {
- return JsNumber.DoubleNaN;
- }
- if (double.IsInfinity(n) || NumberInstance.IsPositiveZero(n) || NumberInstance.IsNegativeZero(n))
- {
- return x;
- }
- return (double) (Half) n;
- #else
- ExceptionHelper.ThrowNotImplementedException("Float16/Half type is not supported in this build");
- return default;
- #endif
- }
- private static JsValue Sin(JsValue thisObject, JsCallArguments arguments)
- {
- var x = TypeConverter.ToNumber(arguments.At(0));
- if (double.IsNaN(x))
- {
- return JsNumber.DoubleNaN;
- }
- else if (NumberInstance.IsPositiveZero(x))
- {
- return JsNumber.PositiveZero;
- }
- else if (NumberInstance.IsNegativeZero(x))
- {
- return JsNumber.NegativeZero;
- }
- else if (double.IsInfinity(x))
- {
- return JsNumber.DoubleNaN;
- }
- return System.Math.Sin(x);
- }
- private static JsValue Sinh(JsValue thisObject, JsCallArguments arguments)
- {
- var x = TypeConverter.ToNumber(arguments.At(0));
- if (double.IsNaN(x))
- {
- return JsNumber.DoubleNaN;
- }
- else if (NumberInstance.IsPositiveZero(x))
- {
- return JsNumber.PositiveZero;
- }
- else if (NumberInstance.IsNegativeZero(x))
- {
- return JsNumber.NegativeZero;
- }
- else if (double.IsNegativeInfinity(x))
- {
- return JsNumber.DoubleNegativeInfinity;
- }
- else if (double.IsPositiveInfinity(x))
- {
- return JsNumber.DoublePositiveInfinity;
- }
- return System.Math.Sinh(x);
- }
- private static JsValue Sqrt(JsValue thisObject, JsCallArguments arguments)
- {
- var x = TypeConverter.ToNumber(arguments.At(0));
- return System.Math.Sqrt(x);
- }
- private static JsValue Tan(JsValue thisObject, JsCallArguments arguments)
- {
- var x = TypeConverter.ToNumber(arguments.At(0));
- return System.Math.Tan(x);
- }
- private static JsValue Tanh(JsValue thisObject, JsCallArguments arguments)
- {
- var x = TypeConverter.ToNumber(arguments.At(0));
- return System.Math.Tanh(x);
- }
- private static JsValue Truncate(JsValue thisObject, JsCallArguments arguments)
- {
- var x = TypeConverter.ToNumber(arguments.At(0));
- if (double.IsNaN(x))
- {
- return JsNumber.DoubleNaN;
- }
- if (NumberInstance.IsPositiveZero(x) || NumberInstance.IsNegativeZero(x))
- {
- return x;
- }
- if (double.IsPositiveInfinity(x))
- {
- return JsNumber.DoublePositiveInfinity;
- }
- if (double.IsNegativeInfinity(x))
- {
- return JsNumber.DoubleNegativeInfinity;
- }
- return System.Math.Truncate(x);
- }
- private static JsValue Sign(JsValue thisObject, JsCallArguments arguments)
- {
- var x = TypeConverter.ToNumber(arguments.At(0));
- if (double.IsNaN(x))
- {
- return JsNumber.DoubleNaN;
- }
- if (NumberInstance.IsPositiveZero(x) || NumberInstance.IsNegativeZero(x))
- {
- return x;
- }
- if (double.IsPositiveInfinity(x))
- {
- return 1;
- }
- if (double.IsNegativeInfinity(x))
- {
- return -1;
- }
- return System.Math.Sign(x);
- }
- private static JsValue Cbrt(JsValue thisObject, JsCallArguments arguments)
- {
- var x = TypeConverter.ToNumber(arguments.At(0));
- if (double.IsNaN(x))
- {
- return JsNumber.DoubleNaN;
- }
- else if (NumberInstance.IsPositiveZero(x) || NumberInstance.IsNegativeZero(x))
- {
- return x;
- }
- else if (double.IsPositiveInfinity(x))
- {
- return JsNumber.DoublePositiveInfinity;
- }
- else if (double.IsNegativeInfinity(x))
- {
- return JsNumber.DoubleNegativeInfinity;
- }
- if (System.Math.Sign(x) >= 0)
- {
- return System.Math.Pow(x, 1.0 / 3.0);
- }
- return -1 * System.Math.Pow(System.Math.Abs(x), 1.0 / 3.0);
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-math.hypot
- /// </summary>
- private static JsValue Hypot(JsValue thisObject, JsCallArguments arguments)
- {
- var coerced = Coerced(arguments);
- foreach (var number in coerced)
- {
- if (double.IsInfinity(number))
- {
- return JsNumber.DoublePositiveInfinity;
- }
- }
- var onlyZero = true;
- double y = 0;
- foreach (var number in coerced)
- {
- if (double.IsNaN(number))
- {
- return JsNumber.DoubleNaN;
- }
- if (onlyZero && number != 0)
- {
- onlyZero = false;
- }
- y += number * number;
- }
- if (onlyZero)
- {
- return JsNumber.PositiveZero;
- }
- return System.Math.Sqrt(y);
- }
- /// <summary>
- /// https://github.com/tc39/proposal-math-sum
- /// </summary>
- private JsValue SumPrecise(JsValue thisObject, JsCallArguments arguments)
- {
- var items = arguments.At(0);
- if (items.IsNullOrUndefined())
- {
- ExceptionHelper.ThrowTypeError(_engine.Realm);
- }
- var iteratorRecord = items.GetIterator(_engine.Realm);
- var state = JsNumber.NegativeZero._value;
- List<double> sum = [];
- long count = 0;
- const double Finite = 1;
- try
- {
- while (iteratorRecord.TryIteratorStep(out var next))
- {
- next.TryGetValue(CommonProperties.Value, out var value);
- count++;
- if (count > 9007199254740992)
- {
- ExceptionHelper.ThrowRangeError(_engine.Realm);
- }
- if (value is not JsNumber jsNumber)
- {
- ExceptionHelper.ThrowTypeError(_engine.Realm, "Input is not a number: " + next);
- return default;
- }
- if (!double.IsNaN(state))
- {
- var n = jsNumber._value;
- if (double.IsNaN(n))
- {
- state = double.NaN;
- }
- else if (double.IsPositiveInfinity(n))
- {
- if (double.IsNegativeInfinity(state))
- {
- state = double.NaN;
- }
- else
- {
- state = double.PositiveInfinity;
- }
- }
- else if (double.IsNegativeInfinity(n))
- {
- if (double.IsPositiveInfinity(state))
- {
- state = double.NaN;
- }
- else
- {
- state = double.NegativeInfinity;
- }
- }
- else if (!NumberInstance.IsNegativeZero(n) && (NumberInstance.IsNegativeZero(state) || state == Finite))
- {
- state = Finite;
- sum.Add(n);
- }
- }
- }
- }
- catch
- {
- iteratorRecord.Close(CompletionType.Throw);
- iteratorRecord = null;
- throw;
- }
- finally
- {
- iteratorRecord?.Close(CompletionType.Normal);
- }
- if (state != Finite)
- {
- return state;
- }
- return Math.SumPrecise.Sum(sum);
- }
- private static double[] Coerced(JsCallArguments arguments)
- {
- // TODO stackalloc
- var coerced = new double[arguments.Length];
- for (var i = 0; i < arguments.Length; i++)
- {
- var argument = arguments[i];
- coerced[i] = TypeConverter.ToNumber(argument);
- }
- return coerced;
- }
- private static JsValue Imul(JsValue thisObject, JsCallArguments arguments)
- {
- var x = TypeConverter.ToInt32(arguments.At(0));
- var y = TypeConverter.ToInt32(arguments.At(1));
- return x * y;
- }
- private static JsValue Clz32(JsValue thisObject, JsCallArguments arguments)
- {
- var x = TypeConverter.ToInt32(arguments.At(0));
- if (x < 0)
- {
- return 0;
- }
- if (x == 0)
- {
- return 32;
- }
- var res = 0;
- var shift = 16;
- while (x > 1)
- {
- var temp = x >> shift;
- if (temp != 0)
- {
- x = temp;
- res += shift;
- }
- shift >>= 1;
- }
- return 31 - res;
- }
- }
|