BooleanConstructor.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 override object Call(object thisObject, object[] arguments)
  25. {
  26. if (arguments.Length == 0)
  27. {
  28. return false;
  29. }
  30. return TypeConverter.ToBoolean(arguments[0]);
  31. }
  32. /// <summary>
  33. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.7.2.1
  34. /// </summary>
  35. /// <param name="arguments"></param>
  36. /// <returns></returns>
  37. public ObjectInstance Construct(object[] arguments)
  38. {
  39. return Construct(TypeConverter.ToBoolean(arguments[0]));
  40. }
  41. public ObjectInstance PrototypeObject { get; private set; }
  42. public BooleanInstance Construct(bool value)
  43. {
  44. var instance = new BooleanInstance(_engine);
  45. instance.Prototype = PrototypeObject;
  46. instance.PrimitiveValue = value;
  47. instance.Extensible = true;
  48. return instance;
  49. }
  50. }
  51. }