ObjectTraverseStack.cs 928 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using System.Collections.Generic;
  2. using Jint.Native;
  3. using Jint.Runtime;
  4. namespace Jint.Collections
  5. {
  6. /// <summary>
  7. /// Helps traversing objects and checks for cyclic references.
  8. /// </summary>
  9. internal sealed class ObjectTraverseStack
  10. {
  11. private readonly Engine _engine;
  12. private readonly Stack<object> _stack = new();
  13. public ObjectTraverseStack(Engine engine)
  14. {
  15. _engine = engine;
  16. }
  17. public void Enter(JsValue value)
  18. {
  19. if (value is null)
  20. {
  21. ExceptionHelper.ThrowArgumentNullException(nameof(value));
  22. }
  23. if (_stack.Contains(value))
  24. {
  25. ExceptionHelper.ThrowTypeError(_engine.Realm, "Cyclic reference detected.");
  26. }
  27. _stack.Push(value);
  28. }
  29. public void Exit()
  30. {
  31. _stack.Pop();
  32. }
  33. }
  34. }