NumberConstructor.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using Jint.Native.Function;
  2. using Jint.Native.Object;
  3. using Jint.Runtime;
  4. namespace Jint.Native.Number
  5. {
  6. public sealed class NumberConstructor : FunctionInstance, IConstructor
  7. {
  8. public NumberConstructor(Engine engine)
  9. : base(engine, null, null, false)
  10. {
  11. }
  12. public static NumberConstructor CreateNumberConstructor(Engine engine)
  13. {
  14. var obj = new NumberConstructor(engine);
  15. obj.Extensible = true;
  16. // The value of the [[Prototype]] internal property of the Number constructor is the Function prototype object
  17. obj.Prototype = engine.Function.PrototypeObject;
  18. obj.PrototypeObject = NumberPrototype.CreatePrototypeObject(engine, obj);
  19. obj.FastAddProperty("length", 1, false, false, false);
  20. // The initial value of Number.prototype is the Number prototype object
  21. obj.FastAddProperty("prototype", obj.PrototypeObject, false, false, false);
  22. return obj;
  23. }
  24. public void Configure()
  25. {
  26. FastAddProperty("MAX_VALUE", double.MaxValue, false, false, false);
  27. FastAddProperty("MIN_VALUE", double.Epsilon, false, false, false);
  28. FastAddProperty("NaN", double.NaN, false, false, false);
  29. FastAddProperty("NEGATIVE_INFINITY", double.NegativeInfinity, false, false, false);
  30. FastAddProperty("POSITIVE_INFINITY", double.PositiveInfinity, false, false, false);
  31. }
  32. public override JsValue Call(JsValue thisObject, JsValue[] arguments)
  33. {
  34. if (arguments.Length == 0)
  35. {
  36. return 0d;
  37. }
  38. return TypeConverter.ToNumber(arguments[0]);
  39. }
  40. /// <summary>
  41. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.7.2.1
  42. /// </summary>
  43. /// <param name="arguments"></param>
  44. /// <returns></returns>
  45. public ObjectInstance Construct(JsValue[] arguments)
  46. {
  47. return Construct(arguments.Length > 0 ? TypeConverter.ToNumber(arguments[0]) : 0);
  48. }
  49. public NumberPrototype PrototypeObject { get; private set; }
  50. public NumberInstance Construct(double value)
  51. {
  52. var instance = new NumberInstance(Engine);
  53. instance.Prototype = PrototypeObject;
  54. instance.PrimitiveValue = value;
  55. instance.Extensible = true;
  56. return instance;
  57. }
  58. }
  59. }