BooleanConstructor.cs 2.0 KB

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