2
0

ExecutionContextStack.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. 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(Environment newEnv)
  15. {
  16. var array = _stack._array;
  17. var size = _stack._size;
  18. ref var executionContext = ref array[size - 1];
  19. executionContext = executionContext.UpdateLexicalEnvironment(newEnv);
  20. }
  21. public void ReplaceTopVariableEnvironment(Environment newEnv)
  22. {
  23. var array = _stack._array;
  24. var size = _stack._size;
  25. ref var executionContext = ref array[size - 1];
  26. executionContext = executionContext.UpdateVariableEnvironment(newEnv);
  27. }
  28. public void ReplaceTopPrivateEnvironment(PrivateEnvironment? newEnv)
  29. {
  30. var array = _stack._array;
  31. var size = _stack._size;
  32. ref var executionContext = ref array[size - 1];
  33. executionContext = executionContext.UpdatePrivateEnvironment(newEnv);
  34. }
  35. public ref readonly ExecutionContext ReplaceTopGenerator(GeneratorInstance newEnv)
  36. {
  37. var array = _stack._array;
  38. var size = _stack._size;
  39. ref var executionContext = ref array[size - 1];
  40. executionContext = executionContext.UpdateGenerator(newEnv);
  41. return ref executionContext;
  42. }
  43. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  44. public ref readonly ExecutionContext Peek() => ref _stack.Peek();
  45. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  46. public ref readonly ExecutionContext Peek(int fromTop) => ref _stack.Peek(fromTop);
  47. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  48. public void Push(in ExecutionContext context) => _stack.Push(in context);
  49. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  50. public ref readonly ExecutionContext Pop() => ref _stack.Pop();
  51. public IScriptOrModule? GetActiveScriptOrModule()
  52. {
  53. var array = _stack._array;
  54. var size = _stack._size;
  55. for (var i = size - 1; i > -1; --i)
  56. {
  57. ref readonly var context = ref array[i];
  58. if (context.ScriptOrModule is not null)
  59. {
  60. return context.ScriptOrModule;
  61. }
  62. }
  63. return null;
  64. }
  65. public ParserOptions? GetActiveParserOptions()
  66. {
  67. var array = _stack._array;
  68. var size = _stack._size;
  69. for (var i = size - 1; i > -1; --i)
  70. {
  71. ref readonly var context = ref array[i];
  72. if (context.ParserOptions is not null)
  73. {
  74. return context.ParserOptions;
  75. }
  76. }
  77. return null;
  78. }
  79. }