BooleanPrototype.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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, "toString", ToBooleanString, 0, PropertyFlag.Configurable), true, false, true);
  26. FastAddProperty("valueOf", new ClrFunctionInstance(Engine, "valueOf", ValueOf, 0, PropertyFlag.Configurable), true, false, true);
  27. }
  28. private JsValue ValueOf(JsValue thisObj, JsValue[] arguments)
  29. {
  30. if (thisObj._type == Types.Boolean)
  31. {
  32. return thisObj;
  33. }
  34. if (thisObj is BooleanInstance bi)
  35. {
  36. return bi.PrimitiveValue;
  37. }
  38. return ExceptionHelper.ThrowTypeError<JsValue>(Engine);
  39. }
  40. private JsValue ToBooleanString(JsValue thisObj, JsValue[] arguments)
  41. {
  42. var b = ValueOf(thisObj, Arguments.Empty);
  43. return ((JsBoolean) b)._value ? "true" : "false";
  44. }
  45. }
  46. }