BooleanConstructor.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 static readonly JsString _functionName = new JsString("Boolean");
  10. private BooleanConstructor(Engine engine)
  11. : base(engine, _functionName)
  12. {
  13. }
  14. public static BooleanConstructor CreateBooleanConstructor(Engine engine)
  15. {
  16. var obj = new BooleanConstructor(engine);
  17. // The value of the [[Prototype]] internal property of the Boolean constructor is the Function prototype object
  18. obj._prototype = engine.Function.PrototypeObject;
  19. obj.PrototypeObject = BooleanPrototype.CreatePrototypeObject(engine, obj);
  20. obj._length = new PropertyDescriptor(JsNumber.One, PropertyFlag.Configurable);
  21. // The initial value of Boolean.prototype is the Boolean prototype object
  22. obj._prototypeDescriptor = new PropertyDescriptor(obj.PrototypeObject, PropertyFlag.AllForbidden);
  23. return obj;
  24. }
  25. public override JsValue Call(JsValue thisObject, JsValue[] arguments)
  26. {
  27. if (arguments.Length == 0)
  28. {
  29. return false;
  30. }
  31. return TypeConverter.ToBoolean(arguments[0]);
  32. }
  33. /// <summary>
  34. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.7.2.1
  35. /// </summary>
  36. public ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
  37. {
  38. return Construct(TypeConverter.ToBoolean(arguments.At(0)));
  39. }
  40. public BooleanPrototype PrototypeObject { get; private set; }
  41. public BooleanInstance Construct(bool value)
  42. {
  43. return Construct(value ? JsBoolean.True : JsBoolean.False);
  44. }
  45. public BooleanInstance Construct(JsBoolean value)
  46. {
  47. var instance = new BooleanInstance(Engine)
  48. {
  49. _prototype = PrototypeObject,
  50. PrimitiveValue = value,
  51. };
  52. return instance;
  53. }
  54. }
  55. }