BooleanPrototype.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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<object, string>(Engine, ToBooleanString), true, false, true);
  25. FastAddProperty("valueOf", new ClrFunctionInstance<object, bool>(Engine, ValueOf), true, false, true);
  26. }
  27. private bool ValueOf(object thisObj, object[] arguments)
  28. {
  29. var B = thisObj;
  30. object b;
  31. if (TypeConverter.GetType(B) == Types.Boolean)
  32. {
  33. b = B;
  34. }
  35. else
  36. {
  37. var o = B as BooleanInstance;
  38. if (o != null)
  39. {
  40. return o.PrimitiveValue;
  41. }
  42. else
  43. {
  44. throw new JavaScriptException(Engine.TypeError);
  45. }
  46. }
  47. return (bool)b;
  48. }
  49. private string ToBooleanString(object thisObj, object[] arguments)
  50. {
  51. var b = ValueOf(thisObj, Arguments.Empty);
  52. return b ? "true" : "false";
  53. }
  54. }
  55. }