BooleanPrototype.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using Jint.Runtime;
  2. using Jint.Runtime.Descriptors;
  3. using Jint.Runtime.Interop;
  4. namespace Jint.Native.Boolean
  5. {
  6. /// <summary>
  7. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.6.4
  8. /// </summary>
  9. public sealed class BooleanPrototype : BooleanInstance
  10. {
  11. private BooleanPrototype(Engine engine) : base(engine)
  12. {
  13. }
  14. public static BooleanPrototype CreatePrototypeObject(Engine engine, BooleanConstructor booleanConstructor)
  15. {
  16. var obj = new BooleanPrototype(engine);
  17. obj.Prototype = engine.Object.PrototypeObject;
  18. obj.PrimitiveValue = false;
  19. obj.Extensible = true;
  20. obj.SetOwnProperty("constructor", new PropertyDescriptor(booleanConstructor, PropertyFlag.NonEnumerable));
  21. return obj;
  22. }
  23. public void Configure()
  24. {
  25. FastAddProperty("toString", new ClrFunctionInstance(Engine, ToBooleanString), true, false, true);
  26. FastAddProperty("valueOf", new ClrFunctionInstance(Engine, ValueOf), true, false, true);
  27. }
  28. private JsValue ValueOf(JsValue thisObj, JsValue[] arguments)
  29. {
  30. var B = thisObj;
  31. if (B.IsBoolean())
  32. {
  33. return B;
  34. }
  35. var o = B.TryCast<BooleanInstance>();
  36. if (!ReferenceEquals(o, null))
  37. {
  38. return o.PrimitiveValue;
  39. }
  40. throw new JavaScriptException(Engine.TypeError);
  41. }
  42. private JsValue ToBooleanString(JsValue thisObj, JsValue[] arguments)
  43. {
  44. var b = ValueOf(thisObj, Arguments.Empty);
  45. return ((JsBoolean) b)._value ? "true" : "false";
  46. }
  47. }
  48. }