NumberConstructor.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. public NumberConstructor(Engine engine)
  11. : base(engine, null, null, false)
  12. {
  13. }
  14. public static NumberConstructor CreateNumberConstructor(Engine engine)
  15. {
  16. var obj = new NumberConstructor(engine);
  17. obj.Extensible = true;
  18. // The value of the [[Prototype]] internal property of the Number constructor is the Function prototype object
  19. obj.Prototype = engine.Function.PrototypeObject;
  20. obj.PrototypeObject = NumberPrototype.CreatePrototypeObject(engine, obj);
  21. obj.FastAddProperty("length", 1, false, false, false);
  22. // The initial value of Number.prototype is the Number prototype object
  23. obj.FastAddProperty("prototype", obj.PrototypeObject, false, false, false);
  24. return obj;
  25. }
  26. public void Configure()
  27. {
  28. }
  29. public override object Call(object thisObject, object[] arguments)
  30. {
  31. if (arguments.Length == 0)
  32. {
  33. return 0d;
  34. }
  35. return TypeConverter.ToNumber(arguments[0]);
  36. }
  37. /// <summary>
  38. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.7.2.1
  39. /// </summary>
  40. /// <param name="arguments"></param>
  41. /// <returns></returns>
  42. public ObjectInstance Construct(object[] arguments)
  43. {
  44. return Construct(arguments.Length > 0 ? TypeConverter.ToNumber(arguments[0]) : 0);
  45. }
  46. public NumberPrototype PrototypeObject { get; private set; }
  47. public NumberInstance Construct(double value)
  48. {
  49. var instance = new NumberInstance(Engine);
  50. instance.Prototype = PrototypeObject;
  51. instance.PrimitiveValue = value;
  52. instance.Extensible = true;
  53. return instance;
  54. }
  55. }
  56. }