ConstantValueAccessor.cs 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. using System;
  2. using Jint.Native;
  3. using Jint.Runtime.Descriptors;
  4. namespace Jint.Runtime.Interop.Reflection
  5. {
  6. internal sealed class ConstantValueAccessor : ReflectionAccessor
  7. {
  8. public static readonly ConstantValueAccessor NullAccessor = new(null);
  9. public ConstantValueAccessor(JsValue value) : base(null, null)
  10. {
  11. ConstantValue = value;
  12. }
  13. public override bool Writable => false;
  14. protected override JsValue ConstantValue { get; }
  15. protected override object DoGetValue(object target)
  16. {
  17. return ConstantValue;
  18. }
  19. protected override void DoSetValue(object target, object value)
  20. {
  21. throw new InvalidOperationException();
  22. }
  23. public override PropertyDescriptor CreatePropertyDescriptor(Engine engine, object target)
  24. {
  25. return ConstantValue is null
  26. ? PropertyDescriptor.Undefined
  27. : new(ConstantValue, PropertyFlag.AllForbidden);
  28. }
  29. }
  30. }