BooleanPrototype.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. _booleanConstructor = booleanConstructor
  23. };
  24. return obj;
  25. }
  26. protected override void Initialize()
  27. {
  28. _properties = new StringDictionarySlim<PropertyDescriptor>(3)
  29. {
  30. ["constructor"] = new PropertyDescriptor(_booleanConstructor, 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. }
  35. private JsValue ValueOf(JsValue thisObj, JsValue[] arguments)
  36. {
  37. if (thisObj._type == InternalTypes.Boolean)
  38. {
  39. return thisObj;
  40. }
  41. if (thisObj is BooleanInstance bi)
  42. {
  43. return bi.PrimitiveValue;
  44. }
  45. return ExceptionHelper.ThrowTypeError<JsValue>(Engine);
  46. }
  47. private JsValue ToBooleanString(JsValue thisObj, JsValue[] arguments)
  48. {
  49. var b = ValueOf(thisObj, Arguments.Empty);
  50. return ((JsBoolean) b)._value ? "true" : "false";
  51. }
  52. }
  53. }