BooleanConstructor.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. var instance = new BooleanInstance(Engine);
  48. instance.Prototype = PrototypeObject;
  49. instance.PrimitiveValue = value;
  50. instance.Extensible = true;
  51. return instance;
  52. }
  53. }
  54. }