NumberConstructor.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using Jint.Native.Function;
  2. using Jint.Native.Object;
  3. using Jint.Runtime;
  4. using Jint.Runtime.Descriptors;
  5. using Jint.Runtime.Interop;
  6. namespace Jint.Native.Number
  7. {
  8. public sealed class NumberConstructor : FunctionInstance, IConstructor
  9. {
  10. private const long MinSafeInteger = -9007199254740991;
  11. internal const long MaxSafeInteger = 9007199254740991;
  12. public NumberConstructor(Engine engine)
  13. : base(engine, "Number", null, null, false)
  14. {
  15. }
  16. public static NumberConstructor CreateNumberConstructor(Engine engine)
  17. {
  18. var obj = new NumberConstructor(engine);
  19. obj.Extensible = true;
  20. // The value of the [[Prototype]] internal property of the Number constructor is the Function prototype object
  21. obj.Prototype = engine.Function.PrototypeObject;
  22. obj.PrototypeObject = NumberPrototype.CreatePrototypeObject(engine, obj);
  23. obj.SetOwnProperty("length", new PropertyDescriptor(1, PropertyFlag.AllForbidden));
  24. // The initial value of Number.prototype is the Number prototype object
  25. obj.SetOwnProperty("prototype", new PropertyDescriptor(obj.PrototypeObject, PropertyFlag.AllForbidden));
  26. return obj;
  27. }
  28. public void Configure()
  29. {
  30. SetOwnProperty("MAX_VALUE", new PropertyDescriptor(double.MaxValue, PropertyFlag.AllForbidden));
  31. SetOwnProperty("MIN_VALUE", new PropertyDescriptor(double.Epsilon, PropertyFlag.AllForbidden));
  32. SetOwnProperty("NaN", new PropertyDescriptor(double.NaN, PropertyFlag.AllForbidden));
  33. SetOwnProperty("NEGATIVE_INFINITY", new PropertyDescriptor(double.NegativeInfinity, PropertyFlag.AllForbidden));
  34. SetOwnProperty("POSITIVE_INFINITY", new PropertyDescriptor(double.PositiveInfinity, PropertyFlag.AllForbidden));
  35. SetOwnProperty("EPSILON", new PropertyDescriptor(JsNumber.JavaScriptEpsilon, PropertyFlag.AllForbidden));
  36. SetOwnProperty("MIN_SAFE_INTEGER", new PropertyDescriptor(MinSafeInteger, PropertyFlag.AllForbidden));
  37. SetOwnProperty("MAX_SAFE_INTEGER", new PropertyDescriptor(MaxSafeInteger, PropertyFlag.AllForbidden));
  38. FastAddProperty("isFinite", new ClrFunctionInstance(Engine, "isFinite", IsFinite, 1, PropertyFlag.Configurable), true, false, true);
  39. FastAddProperty("isInteger", new ClrFunctionInstance(Engine, "isInteger", IsInteger, 1, PropertyFlag.Configurable), true, false, true);
  40. FastAddProperty("isNaN", new ClrFunctionInstance(Engine, "isNaN", IsNaN, 1, PropertyFlag.Configurable), true, false, true);
  41. FastAddProperty("isSafeInteger", new ClrFunctionInstance(Engine, "isSafeInteger", IsSafeInteger, 1, PropertyFlag.Configurable), true, false, true);
  42. FastAddProperty("parseFloat", new ClrFunctionInstance(Engine, "parseFloat", _engine.Global.ParseFloat, 0, PropertyFlag.Configurable), true, false, true);
  43. FastAddProperty("parseInt", new ClrFunctionInstance(Engine, "parseInt", _engine.Global.ParseInt, 0, PropertyFlag.Configurable), true, false, true);
  44. }
  45. private JsValue IsFinite(JsValue thisObj, JsValue[] arguments)
  46. {
  47. if (!(arguments.At(0) is JsNumber num))
  48. {
  49. return false;
  50. }
  51. return double.IsInfinity(num._value) || double.IsNaN(num._value) ? JsBoolean.False : JsBoolean.True;
  52. }
  53. private JsValue IsInteger(JsValue thisObj, JsValue[] arguments)
  54. {
  55. if (!(arguments.At(0) is JsNumber num))
  56. {
  57. return false;
  58. }
  59. if (double.IsInfinity(num._value) || double.IsNaN(num._value))
  60. {
  61. return JsBoolean.False;
  62. }
  63. var integer = TypeConverter.ToInteger(num);
  64. return integer == num._value;
  65. }
  66. private JsValue IsNaN(JsValue thisObj, JsValue[] arguments)
  67. {
  68. if (!(arguments.At(0) is JsNumber num))
  69. {
  70. return false;
  71. }
  72. return double.IsNaN(num._value);
  73. }
  74. private JsValue IsSafeInteger(JsValue thisObj, JsValue[] arguments)
  75. {
  76. if (!(arguments.At(0) is JsNumber num))
  77. {
  78. return false;
  79. }
  80. if (double.IsInfinity(num._value) || double.IsNaN(num._value))
  81. {
  82. return JsBoolean.False;
  83. }
  84. var integer = TypeConverter.ToInteger(num);
  85. if (integer != num._value)
  86. {
  87. return false;
  88. }
  89. return System.Math.Abs(integer) <= MaxSafeInteger;
  90. }
  91. public override JsValue Call(JsValue thisObject, JsValue[] arguments)
  92. {
  93. if (arguments.Length == 0)
  94. {
  95. return 0d;
  96. }
  97. return TypeConverter.ToNumber(arguments[0]);
  98. }
  99. /// <summary>
  100. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.7.2.1
  101. /// </summary>
  102. /// <param name="arguments"></param>
  103. /// <returns></returns>
  104. public ObjectInstance Construct(JsValue[] arguments)
  105. {
  106. return Construct(arguments.Length > 0 ? TypeConverter.ToNumber(arguments[0]) : 0);
  107. }
  108. public NumberPrototype PrototypeObject { get; private set; }
  109. public NumberInstance Construct(double value)
  110. {
  111. return Construct(JsNumber.Create(value));
  112. }
  113. public NumberInstance Construct(JsNumber value)
  114. {
  115. var instance = new NumberInstance(Engine)
  116. {
  117. Prototype = PrototypeObject,
  118. NumberData = value,
  119. Extensible = true
  120. };
  121. return instance;
  122. }
  123. }
  124. }