ExecutionContextStack.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. public void ReplaceTopPrivateEnvironment(PrivateEnvironmentRecord newEnv)
  27. {
  28. var array = _stack._array;
  29. var size = _stack._size;
  30. array[size - 1] = array[size - 1].UpdatePrivateEnvironment(newEnv);
  31. }
  32. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  33. public ref readonly ExecutionContext Peek() => ref _stack.Peek();
  34. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  35. public ref readonly ExecutionContext Peek(int fromTop) => ref _stack.Peek(fromTop);
  36. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  37. public void Push(in ExecutionContext context) => _stack.Push(in context);
  38. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  39. public ref readonly ExecutionContext Pop() => ref _stack.Pop();
  40. public IScriptOrModule? GetActiveScriptOrModule()
  41. {
  42. var array = _stack._array;
  43. var size = _stack._size;
  44. for (var i = size - 1; i > -1; --i)
  45. {
  46. var context = array[i];
  47. if (context.ScriptOrModule is not null)
  48. {
  49. return context.ScriptOrModule;
  50. }
  51. }
  52. return null;
  53. }
  54. }
  55. }