RefStack.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using Jint.Runtime.Environments;
  4. namespace Jint.Runtime
  5. {
  6. internal sealed class ExecutionContextStack
  7. {
  8. private ExecutionContext[] _array;
  9. private int _size;
  10. private const int DefaultCapacity = 2;
  11. public ExecutionContextStack(int capacity)
  12. {
  13. _array = new ExecutionContext[capacity];
  14. _size = 0;
  15. }
  16. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  17. public ref readonly ExecutionContext Peek()
  18. {
  19. if (_size == 0)
  20. {
  21. ExceptionHelper.ThrowInvalidOperationException("stack is empty");
  22. }
  23. return ref _array[_size - 1];
  24. }
  25. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  26. public void Pop()
  27. {
  28. if (_size == 0)
  29. {
  30. ExceptionHelper.ThrowInvalidOperationException("stack is empty");
  31. }
  32. _size--;
  33. }
  34. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  35. public void Push(in ExecutionContext item)
  36. {
  37. if (_size == _array.Length)
  38. {
  39. EnsureCapacity(_size + 1);
  40. }
  41. _array[_size++] = item;
  42. }
  43. private void EnsureCapacity(int min)
  44. {
  45. if (_array.Length < min)
  46. {
  47. int newCapacity = _array.Length == 0
  48. ? DefaultCapacity
  49. : _array.Length * 2;
  50. if (newCapacity < min)
  51. {
  52. newCapacity = min;
  53. }
  54. Resize(newCapacity);
  55. }
  56. }
  57. private void Resize(int value)
  58. {
  59. if (value != _array.Length)
  60. {
  61. if (value > 0)
  62. {
  63. var newItems = new ExecutionContext[value];
  64. if (_size > 0)
  65. {
  66. Array.Copy(_array, 0, newItems, 0, _size);
  67. }
  68. _array = newItems;
  69. }
  70. else
  71. {
  72. _array = Array.Empty<ExecutionContext>();
  73. }
  74. }
  75. }
  76. public void ReplaceTopLexicalEnvironment(LexicalEnvironment newEnv)
  77. {
  78. _array[_size - 1] = _array[_size - 1].UpdateLexicalEnvironment(newEnv);
  79. }
  80. public void ReplaceTopVariableEnvironment(LexicalEnvironment newEnv)
  81. {
  82. _array[_size - 1] = _array[_size - 1].UpdateVariableEnvironment(newEnv);
  83. }
  84. }
  85. }