ExecutionContextStack.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System.Runtime.CompilerServices;
  2. using Jint.Collections;
  3. using Jint.Native.Generator;
  4. using Jint.Runtime.Environments;
  5. using Environment = Jint.Runtime.Environments.Environment;
  6. namespace Jint.Runtime
  7. {
  8. internal sealed class ExecutionContextStack
  9. {
  10. private readonly RefStack<ExecutionContext> _stack;
  11. public ExecutionContextStack(int capacity)
  12. {
  13. _stack = new RefStack<ExecutionContext>(capacity);
  14. }
  15. public void ReplaceTopLexicalEnvironment(Environment newEnv)
  16. {
  17. var array = _stack._array;
  18. var size = _stack._size;
  19. ref var executionContext = ref array[size - 1];
  20. executionContext = executionContext.UpdateLexicalEnvironment(newEnv);
  21. }
  22. public void ReplaceTopVariableEnvironment(Environment newEnv)
  23. {
  24. var array = _stack._array;
  25. var size = _stack._size;
  26. ref var executionContext = ref array[size - 1];
  27. executionContext = executionContext.UpdateVariableEnvironment(newEnv);
  28. }
  29. public void ReplaceTopPrivateEnvironment(PrivateEnvironment? newEnv)
  30. {
  31. var array = _stack._array;
  32. var size = _stack._size;
  33. ref var executionContext = ref array[size - 1];
  34. executionContext = executionContext.UpdatePrivateEnvironment(newEnv);
  35. }
  36. public ref readonly ExecutionContext ReplaceTopGenerator(GeneratorInstance newEnv)
  37. {
  38. var array = _stack._array;
  39. var size = _stack._size;
  40. ref var executionContext = ref array[size - 1];
  41. executionContext = executionContext.UpdateGenerator(newEnv);
  42. return ref executionContext;
  43. }
  44. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  45. public ref readonly ExecutionContext Peek() => ref _stack.Peek();
  46. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  47. public ref readonly ExecutionContext Peek(int fromTop) => ref _stack.Peek(fromTop);
  48. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  49. public void Push(in ExecutionContext context) => _stack.Push(in context);
  50. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  51. public ref readonly ExecutionContext Pop() => ref _stack.Pop();
  52. public IScriptOrModule? GetActiveScriptOrModule()
  53. {
  54. var array = _stack._array;
  55. var size = _stack._size;
  56. for (var i = size - 1; i > -1; --i)
  57. {
  58. ref readonly var context = ref array[i];
  59. if (context.ScriptOrModule is not null)
  60. {
  61. return context.ScriptOrModule;
  62. }
  63. }
  64. return null;
  65. }
  66. public ParserOptions? GetActiveParserOptions()
  67. {
  68. var array = _stack._array;
  69. var size = _stack._size;
  70. for (var i = size - 1; i > -1; --i)
  71. {
  72. ref readonly var context = ref array[i];
  73. if (context.ParserOptions is not null)
  74. {
  75. return context.ParserOptions;
  76. }
  77. }
  78. return null;
  79. }
  80. }
  81. }