ExecutionContextStack.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #nullable enable
  2. using System.Runtime.CompilerServices;
  3. using Jint.Collections;
  4. using Jint.Runtime.Environments;
  5. namespace Jint.Runtime
  6. {
  7. internal sealed class ExecutionContextStack
  8. {
  9. private readonly RefStack<ExecutionContext> _stack;
  10. public ExecutionContextStack(int capacity)
  11. {
  12. _stack = new RefStack<ExecutionContext>(capacity);
  13. }
  14. public void ReplaceTopLexicalEnvironment(LexicalEnvironment newEnv)
  15. {
  16. var array = _stack._array;
  17. var size = _stack._size;
  18. array[size - 1] = array[size - 1].UpdateLexicalEnvironment(newEnv);
  19. }
  20. public void ReplaceTopVariableEnvironment(LexicalEnvironment newEnv)
  21. {
  22. var array = _stack._array;
  23. var size = _stack._size;
  24. array[size - 1] = array[size - 1].UpdateVariableEnvironment(newEnv);
  25. }
  26. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  27. public ref readonly ExecutionContext Peek() => ref _stack.Peek();
  28. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  29. public void Push(in ExecutionContext context) => _stack.Push(in context);
  30. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  31. public ref readonly ExecutionContext Pop() => ref _stack.Pop();
  32. }
  33. }