NumberConstructor.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using Jint.Native.Array;
  2. using Jint.Native.Function;
  3. using Jint.Native.Object;
  4. using Jint.Runtime;
  5. using Jint.Runtime.Descriptors;
  6. namespace Jint.Native.Number
  7. {
  8. public sealed class NumberConstructor : FunctionInstance, IConstructor
  9. {
  10. private readonly Engine _engine;
  11. public NumberConstructor(Engine engine)
  12. : base(engine, new ObjectInstance(engine.Object), null, null, false)
  13. {
  14. _engine = engine;
  15. // the constructor is the function constructor of an object
  16. this.Prototype.DefineOwnProperty("constructor", new DataDescriptor(this) { Writable = true, Enumerable = false, Configurable = false }, false);
  17. this.Prototype.DefineOwnProperty("prototype", new DataDescriptor(Prototype) { Writable = true, Enumerable = false, Configurable = false }, false);
  18. // Number prototype properties
  19. this.Prototype.DefineOwnProperty("NaN", new DataDescriptor(double.NaN), false);
  20. this.Prototype.DefineOwnProperty("MAX_VALUE", new DataDescriptor(double.MaxValue), false);
  21. this.Prototype.DefineOwnProperty("MIN_VALUE", new DataDescriptor(double.MinValue), false);
  22. this.Prototype.DefineOwnProperty("POSITIVE_INFINITY", new DataDescriptor(double.PositiveInfinity), false);
  23. this.Prototype.DefineOwnProperty("NEGATIVE_INFINITY", new DataDescriptor(double.NegativeInfinity), false);
  24. }
  25. public override object Call(object thisObject, object[] arguments)
  26. {
  27. return Construct(arguments);
  28. }
  29. /// <summary>
  30. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.7.2.1
  31. /// </summary>
  32. /// <param name="arguments"></param>
  33. /// <returns></returns>
  34. public ObjectInstance Construct(object[] arguments)
  35. {
  36. return Construct(arguments.Length > 0 ? TypeConverter.ToNumber(arguments[0]) : 0);
  37. }
  38. public NumberInstance Construct(double value)
  39. {
  40. var instance = new NumberInstance(Prototype);
  41. instance.PrimitiveValue = value;
  42. return instance;
  43. }
  44. }
  45. }