2
0

WeakSetInstance.cs 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System.Runtime.CompilerServices;
  2. using Jint.Native.Object;
  3. using Jint.Native.Symbol;
  4. using Jint.Runtime;
  5. namespace Jint.Native.WeakSet;
  6. internal sealed class WeakSetInstance : ObjectInstance
  7. {
  8. private readonly ConditionalWeakTable<JsValue, JsValue> _table;
  9. public WeakSetInstance(Engine engine) : base(engine)
  10. {
  11. _table = new ConditionalWeakTable<JsValue, JsValue>();
  12. }
  13. internal bool WeakSetHas(JsValue value)
  14. {
  15. return _table.TryGetValue(value, out _);
  16. }
  17. internal bool WeakSetDelete(JsValue value)
  18. {
  19. return _table.Remove(value);
  20. }
  21. internal void WeakSetAdd(JsValue value)
  22. {
  23. if (!value.CanBeHeldWeakly(_engine.GlobalSymbolRegistry))
  24. {
  25. ExceptionHelper.ThrowTypeError(_engine.Realm, "WeakSet value must be an object or symbol, got " + value);
  26. }
  27. #if NETSTANDARD2_1_OR_GREATER
  28. _table.AddOrUpdate(value, Undefined);
  29. #else
  30. _table.Remove(value);
  31. _table.Add(value, Undefined);
  32. #endif
  33. }
  34. }