NumberConstructor.cs 2.8 KB

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