123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- using System;
- using Jint.Native.Object;
- using Jint.Runtime;
- using Jint.Runtime.Descriptors.Specialized;
- namespace Jint.Native.Math
- {
- public sealed class MathInstance : ObjectInstance
- {
- private readonly Engine _engine;
- public MathInstance(Engine engine, ObjectInstance prototype)
- : base(engine, prototype)
- {
- _engine = engine;
- }
- public override string Class
- {
- get
- {
- return "Math";
- }
- }
- public static MathInstance CreateMathObject(Engine engine, ObjectInstance prototype)
- {
- var math = new MathInstance(engine, prototype);
- math.DefineOwnProperty("abs", new ClrDataDescriptor<MathInstance, double>(engine, Abs), false);
- math.DefineOwnProperty("acos", new ClrDataDescriptor<MathInstance, double>(engine, Acos), false);
- math.DefineOwnProperty("asin", new ClrDataDescriptor<MathInstance, double>(engine, Asin), false);
- math.DefineOwnProperty("atan", new ClrDataDescriptor<MathInstance, double>(engine, Atan), false);
- math.DefineOwnProperty("atan2", new ClrDataDescriptor<MathInstance, double>(engine, Atan2), false);
- math.DefineOwnProperty("ceil", new ClrDataDescriptor<MathInstance, double>(engine, Ceil), false);
- math.DefineOwnProperty("cos", new ClrDataDescriptor<MathInstance, double>(engine, Cos), false);
- math.DefineOwnProperty("exp", new ClrDataDescriptor<MathInstance, double>(engine, Exp), false);
- math.DefineOwnProperty("floor", new ClrDataDescriptor<MathInstance, double>(engine, Floor), false);
- math.DefineOwnProperty("log", new ClrDataDescriptor<MathInstance, double>(engine, Log), false);
- math.DefineOwnProperty("max", new ClrDataDescriptor<MathInstance, double>(engine, Max), false);
- math.DefineOwnProperty("min", new ClrDataDescriptor<MathInstance, double>(engine, Min), false);
- math.DefineOwnProperty("pow", new ClrDataDescriptor<MathInstance, double>(engine, Pow), false);
- math.DefineOwnProperty("random", new ClrDataDescriptor<MathInstance, double>(engine, Random), false);
- math.DefineOwnProperty("round", new ClrDataDescriptor<MathInstance, double>(engine, Round), false);
- math.DefineOwnProperty("sin", new ClrDataDescriptor<MathInstance, double>(engine, Sin), false);
- math.DefineOwnProperty("sqrt", new ClrDataDescriptor<MathInstance, double>(engine, Sqrt), false);
- math.DefineOwnProperty("tan", new ClrDataDescriptor<MathInstance, double>(engine, Tan), false);
- math.FastAddProperty("E", System.Math.E, false, false, false);
- math.FastAddProperty("LN10", System.Math.Log(10), false, false, false);
- math.FastAddProperty("LN2", System.Math.Log(2), false, false, false);
- math.FastAddProperty("LOG2E", System.Math.Log(System.Math.E, 2), false, false, false);
- math.FastAddProperty("LOG10E", System.Math.Log(System.Math.E, 10), false, false, false);
- math.FastAddProperty("PI", System.Math.PI, false, false, false);
- math.FastAddProperty("SQRT1_2", System.Math.Sqrt(0.5), false, false, false);
- math.FastAddProperty("SQRT2", System.Math.Sqrt(2), false, false, false);
- return math;
- }
- private static double Abs(MathInstance thisObject, object[] arguments)
- {
- var x = TypeConverter.ToNumber(arguments[0]);
- return System.Math.Abs(x);
- }
- private static double Acos(MathInstance thisObject, object[] arguments)
- {
- var x = TypeConverter.ToNumber(arguments[0]);
- return System.Math.Acos(x);
- }
- private static double Asin(MathInstance thisObject, object[] arguments)
- {
- var x = TypeConverter.ToNumber(arguments[0]);
- return System.Math.Asin(x);
- }
- private static double Atan(MathInstance thisObject, object[] arguments)
- {
- var x = TypeConverter.ToNumber(arguments[0]);
- return System.Math.Atan(x);
- }
- private static double Atan2(MathInstance thisObject, object[] arguments)
- {
- var x = TypeConverter.ToNumber(arguments[0]);
- var y = TypeConverter.ToNumber(arguments[1]);
- return System.Math.Atan2(x, y);
- }
- private static double Ceil(MathInstance thisObject, object[] arguments)
- {
- var x = TypeConverter.ToNumber(arguments[0]);
- return System.Math.Ceiling(x);
- }
- private static double Cos(MathInstance thisObject, object[] arguments)
- {
- var x = TypeConverter.ToNumber(arguments[0]);
- return System.Math.Cos(x);
- }
- private static double Exp(MathInstance thisObject, object[] arguments)
- {
- var x = TypeConverter.ToNumber(arguments[0]);
- return System.Math.Exp(x);
- }
- private static double Floor(MathInstance thisObject, object[] arguments)
- {
- var x = TypeConverter.ToNumber(arguments[0]);
- return System.Math.Floor(x);
- }
- private static double Log(MathInstance thisObject, object[] arguments)
- {
- var x = TypeConverter.ToNumber(arguments[0]);
- return System.Math.Log(x);
- }
- private static double Max(MathInstance thisObject, object[] arguments)
- {
- double max = TypeConverter.ToNumber(arguments[0]);
- for (int i = 0; i < arguments.Length; i++)
- {
- max = System.Math.Max(max, TypeConverter.ToNumber(arguments[i]));
- }
- return max;
- }
- private static double Min(MathInstance thisObject, object[] arguments)
- {
- double min = TypeConverter.ToNumber(arguments[0]);
- for (int i = 0; i < arguments.Length; i++)
- {
- min = System.Math.Min(min, TypeConverter.ToNumber(arguments[i]));
- }
- return min;
- }
- private static double Pow(MathInstance thisObject, object[] arguments)
- {
- var x = TypeConverter.ToNumber(arguments[0]);
- var y = TypeConverter.ToNumber(arguments[1]);
- return System.Math.Pow(x, y);
- }
- private static double Random(MathInstance thisObject, object[] arguments)
- {
- return new Random().NextDouble();
- }
- private static double Round(MathInstance thisObject, object[] arguments)
- {
- var x = TypeConverter.ToNumber(arguments[0]);
- return System.Math.Round(x);
- }
- private static double Sin(MathInstance thisObject, object[] arguments)
- {
- var x = TypeConverter.ToNumber(arguments[0]);
- return System.Math.Sin(x);
- }
- private static double Sqrt(MathInstance thisObject, object[] arguments)
- {
- var x = TypeConverter.ToNumber(arguments[0]);
- return System.Math.Sqrt(x);
- }
- private static double Tan(MathInstance thisObject, object[] arguments)
- {
- var x = TypeConverter.ToNumber(arguments[0]);
- return System.Math.Tan(x);
- }
- }
- }
|