evalState.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. 
  2. #include "evalState.h"
  3. void ExprEvalState::pushFrame(StringTableEntry frameName, Namespace *ns, S32 registerCount)
  4. {
  5. #ifdef DEBUG_SPEW
  6. validate();
  7. Platform::outputDebugString("[ConsoleInternal] Pushing new frame for '%s' at %i",
  8. frameName, mStackDepth);
  9. #endif
  10. if (mStackDepth + 1 > stack.size())
  11. {
  12. #ifdef DEBUG_SPEW
  13. Platform::outputDebugString("[ConsoleInternal] Growing stack by one frame");
  14. #endif
  15. stack.push_back(new Dictionary);
  16. }
  17. Dictionary& newFrame = *(stack[mStackDepth]);
  18. newFrame.setState();
  19. newFrame.scopeName = frameName;
  20. newFrame.scopeNamespace = ns;
  21. Con::pushStackFrame(stack[mStackDepth]);
  22. mStackDepth++;
  23. currentVariable = NULL;
  24. AssertFatal(!newFrame.getCount(), "ExprEvalState::pushFrame - Dictionary not empty!");
  25. ConsoleValue* consoleValArray = new ConsoleValue[registerCount]();
  26. localStack.push_back(ConsoleValueFrame(consoleValArray, false));
  27. currentRegisterArray = &localStack.last();
  28. AssertFatal(mStackDepth == localStack.size(), avar("Stack sizes do not match. mStackDepth = %d, localStack = %d", mStackDepth, localStack.size()));
  29. #ifdef DEBUG_SPEW
  30. validate();
  31. #endif
  32. }
  33. void ExprEvalState::popFrame()
  34. {
  35. AssertFatal(mStackDepth > 0, "ExprEvalState::popFrame - Stack Underflow!");
  36. #ifdef DEBUG_SPEW
  37. validate();
  38. Platform::outputDebugString("[ConsoleInternal] Popping %sframe at %i",
  39. getCurrentFrame().isOwner() ? "" : "shared ", mStackDepth - 1);
  40. #endif
  41. Con::popStackFrame();
  42. mStackDepth--;
  43. stack[mStackDepth]->reset();
  44. currentVariable = NULL;
  45. const ConsoleValueFrame& frame = localStack.last();
  46. localStack.pop_back();
  47. if (!frame.isReference)
  48. delete[] frame.values;
  49. currentRegisterArray = localStack.size() ? &localStack.last() : NULL;
  50. AssertFatal(mStackDepth == localStack.size(), avar("Stack sizes do not match. mStackDepth = %d, localStack = %d", mStackDepth, localStack.size()));
  51. #ifdef DEBUG_SPEW
  52. validate();
  53. #endif
  54. }
  55. void ExprEvalState::pushFrameRef(S32 stackIndex)
  56. {
  57. AssertFatal(stackIndex >= 0 && stackIndex < mStackDepth, "You must be asking for a valid frame!");
  58. #ifdef DEBUG_SPEW
  59. validate();
  60. Platform::outputDebugString("[ConsoleInternal] Cloning frame from %i to %i",
  61. stackIndex, mStackDepth);
  62. #endif
  63. if (mStackDepth + 1 > stack.size())
  64. {
  65. #ifdef DEBUG_SPEW
  66. Platform::outputDebugString("[ConsoleInternal] Growing stack by one frame");
  67. #endif
  68. stack.push_back(new Dictionary);
  69. }
  70. Dictionary& newFrame = *(stack[mStackDepth]);
  71. newFrame.setState(stack[stackIndex]);
  72. Con::pushStackFrame(stack[mStackDepth]);
  73. mStackDepth++;
  74. currentVariable = NULL;
  75. ConsoleValue* values = localStack[stackIndex].values;
  76. localStack.push_back(ConsoleValueFrame(values, true));
  77. currentRegisterArray = &localStack.last();
  78. AssertFatal(mStackDepth == localStack.size(), avar("Stack sizes do not match. mStackDepth = %d, localStack = %d", mStackDepth, localStack.size()));
  79. #ifdef DEBUG_SPEW
  80. validate();
  81. #endif
  82. }
  83. void ExprEvalState::pushDebugFrame(S32 stackIndex)
  84. {
  85. pushFrameRef(stackIndex);
  86. Dictionary& newFrame = *(stack[mStackDepth - 1]);
  87. // debugger needs to know this info...
  88. newFrame.scopeName = stack[stackIndex]->scopeName;
  89. newFrame.scopeNamespace = stack[stackIndex]->scopeNamespace;
  90. newFrame.module = stack[stackIndex]->module;
  91. newFrame.ip = stack[stackIndex]->ip;
  92. }
  93. ExprEvalState::ExprEvalState()
  94. {
  95. VECTOR_SET_ASSOCIATION(stack);
  96. currentVariable = NULL;
  97. mStackDepth = 0;
  98. stack.reserve(64);
  99. mShouldReset = false;
  100. mResetLocked = false;
  101. copyVariable = NULL;
  102. currentRegisterArray = NULL;
  103. }
  104. ExprEvalState::~ExprEvalState()
  105. {
  106. // Delete callframes.
  107. while (!stack.empty())
  108. {
  109. delete stack.last();
  110. stack.decrement();
  111. }
  112. }
  113. void ExprEvalState::validate()
  114. {
  115. AssertFatal(mStackDepth <= stack.size(),
  116. "ExprEvalState::validate() - Stack depth pointing beyond last stack frame!");
  117. for (U32 i = 0; i < stack.size(); ++i)
  118. stack[i]->validate();
  119. }