2
0

BooleanPrototype.cs 1.8 KB

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