BooleanPrototype.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using Jint.Collections;
  2. using Jint.Native.Object;
  3. using Jint.Runtime;
  4. using Jint.Runtime.Descriptors;
  5. using Jint.Runtime.Interop;
  6. namespace Jint.Native.Boolean;
  7. /// <summary>
  8. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.6.4
  9. /// </summary>
  10. internal sealed class BooleanPrototype : BooleanInstance
  11. {
  12. private readonly Realm _realm;
  13. private readonly BooleanConstructor _constructor;
  14. internal BooleanPrototype(
  15. Engine engine,
  16. Realm realm,
  17. BooleanConstructor constructor,
  18. ObjectPrototype objectPrototype) : base(engine, JsBoolean.False)
  19. {
  20. _prototype = objectPrototype;
  21. _realm = realm;
  22. _constructor = constructor;
  23. }
  24. protected override void Initialize()
  25. {
  26. var properties = new PropertyDictionary(3, checkExistingKeys: false)
  27. {
  28. ["constructor"] = new PropertyDescriptor(_constructor, PropertyFlag.NonEnumerable),
  29. ["toString"] = new PropertyDescriptor(new ClrFunction(Engine, "toString", ToBooleanString, 0, PropertyFlag.Configurable), true, false, true),
  30. ["valueOf"] = new PropertyDescriptor(new ClrFunction(Engine, "valueOf", ValueOf, 0, PropertyFlag.Configurable), true, false, true)
  31. };
  32. SetProperties(properties);
  33. }
  34. private JsValue ValueOf(JsValue thisObject, JsValue[] arguments)
  35. {
  36. if (thisObject._type == InternalTypes.Boolean)
  37. {
  38. return thisObject;
  39. }
  40. if (thisObject is BooleanInstance bi)
  41. {
  42. return bi.BooleanData;
  43. }
  44. ExceptionHelper.ThrowTypeError(_realm);
  45. return Undefined;
  46. }
  47. private JsString ToBooleanString(JsValue thisObject, JsValue[] arguments)
  48. {
  49. var b = ValueOf(thisObject, Arguments.Empty);
  50. return ((JsBoolean) b)._value ? JsString.TrueString : JsString.FalseString;
  51. }
  52. }