BooleanConstructor.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using Jint.Native.Function;
  2. using Jint.Native.Object;
  3. using Jint.Runtime;
  4. using Jint.Runtime.Descriptors;
  5. namespace Jint.Native.Boolean
  6. {
  7. public sealed class BooleanConstructor : FunctionInstance, IConstructor
  8. {
  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.SetOwnProperty("length", new PropertyDescriptor(1, PropertyFlag.AllForbidden));
  20. // The initial value of Boolean.prototype is the Boolean prototype object
  21. obj.SetOwnProperty("prototype", new PropertyDescriptor(obj.PrototypeObject, PropertyFlag.AllForbidden));
  22. return obj;
  23. }
  24. public void Configure()
  25. {
  26. }
  27. public override JsValue Call(JsValue thisObject, JsValue[] 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(JsValue[] arguments)
  41. {
  42. return Construct(TypeConverter.ToBoolean(arguments.At(0)));
  43. }
  44. public BooleanPrototype PrototypeObject { get; private set; }
  45. public BooleanInstance Construct(bool value)
  46. {
  47. return Construct(value ? JsBoolean.True : JsBoolean.False);
  48. }
  49. public BooleanInstance Construct(JsBoolean value)
  50. {
  51. var instance = new BooleanInstance(Engine)
  52. {
  53. Prototype = PrototypeObject,
  54. PrimitiveValue = value,
  55. Extensible = true
  56. };
  57. return instance;
  58. }
  59. }
  60. }