BooleanPrototype.cs 2.0 KB

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