ConstantValueAccessor.cs 1.1 KB

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