///////////////////////////////////////////////////////////////////// For debugger. /////////////////////////////////////////////////////////////////////
| 274 | // For debugger. |
| 275 | ////////////////////////////////////////////////////////////////////////// |
| 276 | IScriptTable* CScriptSystem::GetLocalVariables(int nLevel, bool bRecursive) |
| 277 | { |
| 278 | lua_Debug ar; |
| 279 | const char* name; |
| 280 | IScriptTable* pObj = CreateTable(true); |
| 281 | pObj->AddRef(); |
| 282 | |
| 283 | // Attach a new table |
| 284 | const int checkStack = lua_gettop(L); |
| 285 | lua_newtable(L); |
| 286 | lua_pushvalue(L, -1); |
| 287 | AttachTable(pObj); |
| 288 | |
| 289 | // Get the stack frame |
| 290 | while (lua_getstack(L, nLevel, &ar) != 0) |
| 291 | { |
| 292 | // Push a sub-table for this frame (recursive only) |
| 293 | if (bRecursive) |
| 294 | { |
| 295 | assert(lua_istable(L, -1) && "The result table should be on the top of the stack"); |
| 296 | lua_pushinteger(L, nLevel); |
| 297 | lua_newtable(L); |
| 298 | lua_pushvalue(L, -1); |
| 299 | lua_insert(L, -3); |
| 300 | lua_rawset(L, -4); |
| 301 | assert(lua_istable(L, -1) && lua_istable(L, -2) && lua_gettop(L) == checkStack + 2 && "There should now be two tables on the top of the stack"); |
| 302 | } |
| 303 | |
| 304 | // Assign variable names and values for the current frame to the table on top of the stack |
| 305 | int i = 1; |
| 306 | const int checkInner = lua_gettop(L); |
| 307 | assert(checkInner == checkStack + 1 + bRecursive && "Too much stack space used"); |
| 308 | assert(lua_istable(L, -1) && "The target table must be on the top of the stack"); |
| 309 | while ((name = lua_getlocal(L, &ar, i++)) != NULL) |
| 310 | { |
| 311 | if (strcmp(name, "(*temporary)") == 0) |
| 312 | { |
| 313 | // Not interested in temporaries |
| 314 | lua_pop(L, 1); |
| 315 | } |
| 316 | else |
| 317 | { |
| 318 | // Push the name, swap the top two items, and set in the table |
| 319 | lua_pushstring(L, name); |
| 320 | lua_insert(L, -2); |
| 321 | assert(lua_gettop(L) == checkInner + 2 && "There should be a key-value pair on top of the stack"); |
| 322 | lua_rawset(L, -3); |
| 323 | } |
| 324 | assert(lua_gettop(L) == checkInner && "Unbalanced algorithm problem"); |
| 325 | assert(lua_istable(L, -1) && "The target table should be on the top of the stack"); |
| 326 | } |
| 327 | |
| 328 | // Pop the sub-table (recursive only) |
| 329 | if (bRecursive) |
| 330 | { |
| 331 | assert(lua_istable(L, -1) && lua_istable(L, -2) && "There should now be two tables on the top of the stack"); |
| 332 | lua_pop(L, 1); |
| 333 | } |
no test coverage detected