NumberConstructor.cs 6.1 KB

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