ExecutionContextStack.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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(EnvironmentRecord 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(EnvironmentRecord 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. public IScriptOrModule? GetActiveScriptOrModule()
  33. {
  34. var array = _stack._array;
  35. var size = _stack._size;
  36. for (var i = size - 1; i > -1; --i)
  37. {
  38. var context = array[i];
  39. if (context.ScriptOrModule is not null)
  40. {
  41. return context.ScriptOrModule;
  42. }
  43. }
  44. return null;
  45. }
  46. }
  47. }