ClrAccessDescriptor.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using Jint.Native;
  2. using Jint.Runtime.Environments;
  3. using Jint.Runtime.Interop;
  4. using Environment = Jint.Runtime.Environments.Environment;
  5. namespace Jint.Runtime.Descriptors.Specialized
  6. {
  7. internal sealed class ClrAccessDescriptor : PropertyDescriptor
  8. {
  9. private readonly DeclarativeEnvironment _env;
  10. private readonly Engine _engine;
  11. private readonly Environment.BindingName _name;
  12. private GetterFunctionInstance? _get;
  13. private SetterFunctionInstance? _set;
  14. public ClrAccessDescriptor(
  15. DeclarativeEnvironment env,
  16. Engine engine,
  17. string name)
  18. : base(value: null, PropertyFlag.Configurable)
  19. {
  20. _flags |= PropertyFlag.NonData;
  21. _env = env;
  22. _engine = engine;
  23. _name = new Environment.BindingName(name);
  24. }
  25. public override JsValue Get => _get ??= new GetterFunctionInstance(_engine, DoGet);
  26. public override JsValue Set => _set ??= new SetterFunctionInstance(_engine, DoSet);
  27. private JsValue DoGet(JsValue n)
  28. {
  29. return _env.TryGetBinding(_name, false, out var binding, out _)
  30. ? binding.Value
  31. : JsValue.Undefined;
  32. }
  33. private void DoSet(JsValue n, JsValue o)
  34. {
  35. _env.SetMutableBinding(_name.Key.Name, o, true);
  36. }
  37. }
  38. }