ClrAccessDescriptor.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. internal sealed class ClrAccessDescriptor : PropertyDescriptor
  7. {
  8. private readonly DeclarativeEnvironment _env;
  9. private readonly Engine _engine;
  10. private readonly Environment.BindingName _name;
  11. private GetterFunction? _get;
  12. private SetterFunction? _set;
  13. public ClrAccessDescriptor(
  14. DeclarativeEnvironment env,
  15. Engine engine,
  16. string name)
  17. : base(value: null, PropertyFlag.Configurable)
  18. {
  19. _flags |= PropertyFlag.NonData;
  20. _env = env;
  21. _engine = engine;
  22. _name = new Environment.BindingName(name);
  23. }
  24. public override JsValue Get => _get ??= new GetterFunction(_engine, DoGet);
  25. public override JsValue Set => _set ??= new SetterFunction(_engine, DoSet);
  26. private JsValue DoGet(JsValue n)
  27. {
  28. return _env.TryGetBinding(_name, strict: true, out var value)
  29. ? value
  30. : JsValue.Undefined;
  31. }
  32. private void DoSet(JsValue n, JsValue o)
  33. {
  34. _env.SetMutableBinding(_name.Key, o, strict: true);
  35. }
  36. }