BooleanPrototype.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using Jint.Collections;
  2. using Jint.Runtime;
  3. using Jint.Runtime.Descriptors;
  4. using Jint.Runtime.Interop;
  5. namespace Jint.Native.Boolean
  6. {
  7. /// <summary>
  8. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.6.4
  9. /// </summary>
  10. public sealed class BooleanPrototype : BooleanInstance
  11. {
  12. private BooleanConstructor _booleanConstructor;
  13. private BooleanPrototype(Engine engine) : base(engine)
  14. {
  15. }
  16. public static BooleanPrototype CreatePrototypeObject(Engine engine, BooleanConstructor booleanConstructor)
  17. {
  18. var obj = new BooleanPrototype(engine)
  19. {
  20. Prototype = engine.Object.PrototypeObject,
  21. PrimitiveValue = false,
  22. Extensible = true,
  23. _booleanConstructor = booleanConstructor
  24. };
  25. return obj;
  26. }
  27. protected override void Initialize()
  28. {
  29. _properties = new StringDictionarySlim<PropertyDescriptor>(3)
  30. {
  31. ["constructor"] = new PropertyDescriptor(_booleanConstructor, PropertyFlag.NonEnumerable),
  32. ["toString"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "toString", ToBooleanString, 0, PropertyFlag.Configurable), true, false, true),
  33. ["valueOf"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "valueOf", ValueOf, 0, PropertyFlag.Configurable), true, false, true)
  34. };
  35. }
  36. private JsValue ValueOf(JsValue thisObj, JsValue[] arguments)
  37. {
  38. if (thisObj._type == Types.Boolean)
  39. {
  40. return thisObj;
  41. }
  42. if (thisObj is BooleanInstance bi)
  43. {
  44. return bi.PrimitiveValue;
  45. }
  46. return ExceptionHelper.ThrowTypeError<JsValue>(Engine);
  47. }
  48. private JsValue ToBooleanString(JsValue thisObj, JsValue[] arguments)
  49. {
  50. var b = ValueOf(thisObj, Arguments.Empty);
  51. return ((JsBoolean) b)._value ? "true" : "false";
  52. }
  53. }
  54. }