BooleanPrototype.cs 1.8 KB

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