BooleanConstructor.cs 2.0 KB

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