BooleanPrototype.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. public 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)
  20. {
  21. _prototype = objectPrototype;
  22. PrimitiveValue = JsBoolean.False;
  23. _realm = realm;
  24. _constructor = constructor;
  25. }
  26. protected override void Initialize()
  27. {
  28. var properties = new PropertyDictionary(3, checkExistingKeys: false)
  29. {
  30. ["constructor"] = new PropertyDescriptor(_constructor, PropertyFlag.NonEnumerable),
  31. ["toString"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "toString", ToBooleanString, 0, PropertyFlag.Configurable), true, false, true),
  32. ["valueOf"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "valueOf", ValueOf, 0, PropertyFlag.Configurable), true, false, true)
  33. };
  34. SetProperties(properties);
  35. }
  36. private JsValue ValueOf(JsValue thisObj, JsValue[] arguments)
  37. {
  38. if (thisObj._type == InternalTypes.Boolean)
  39. {
  40. return thisObj;
  41. }
  42. if (thisObj is BooleanInstance bi)
  43. {
  44. return bi.PrimitiveValue;
  45. }
  46. ExceptionHelper.ThrowTypeError(_realm);
  47. return Undefined;
  48. }
  49. private JsValue ToBooleanString(JsValue thisObj, JsValue[] arguments)
  50. {
  51. var b = ValueOf(thisObj, Arguments.Empty);
  52. return ((JsBoolean) b)._value ? JsString.TrueString : JsString.FalseString;
  53. }
  54. }
  55. }