//----------------------------------------------------------------// int MOAILuaObject::_tostring ( lua_State* L ) { MOAILuaState state ( L ); MOAILuaObject* data = ( MOAILuaObject* )state.GetPtrUserData ( 1 ); if ( data ) { STLString str; lua_getfield ( state, 1, "getClassName" ); if ( state.IsType ( -1, LUA_TFUNCTION )) { lua_pushvalue ( state, 1 ); state.DebugCall ( 1, 1 ); cc8* classname = state.GetValue < cc8* >( -1, "" ); str.write ( "%p <%s>", data, classname ); state.Push ( str ); return 1; } str.write ( "%p <%s>", data, data->TypeName ()); state.Push ( str ); return 1; } return 0; }
//----------------------------------------------------------------// STLString MOAILuaState::GetStackTrace ( int level ) { int firstpart = 1; /* still before eventual `...' */ lua_Debug ar; lua_State* L = this->mState; STLString out; out.append ( "stack traceback:" ); while ( lua_getstack ( L, level++, &ar )) { if ( level > LEVELS1 && firstpart ) { if ( !lua_getstack ( L, level + LEVELS2, &ar )) { level--; } else { // too many levels out.append ( "\n\t..." ); /* too many levels */ // find last levels */ while ( lua_getstack ( L, level + LEVELS2, &ar )) level++; } firstpart = 0; continue; } out.append ( "\n\t" ); lua_getinfo ( L, "Snl", &ar ); out.append ( ar.short_src ); if ( ar.currentline > 0 ) { out.write ( ":%d", ar.currentline ); } if ( *ar.namewhat != '\0' ) { out.write ( " in function '%s'", ar.name ); } else { if ( *ar.what == 'm' ) { out.write ( " in main chunk" ); } else if ( *ar.what == 'C' || *ar.what == 't' ) { out.write ( " ?" ); } else { out.write ( " in function <%s:%d>", ar.short_src, ar.linedefined ); } } } out.append ( "\n" ); return out; }
//----------------------------------------------------------------// void ZLAdapterInfo::SetNameFromMACAddress ( u8* address, u32 length ) { STLString result; for ( u32 i = 0; i < length; i++ ) { if ( i ) { result.write ( "-" ); } result.write ( "%.2X", ( u32 )address [ i ]); } this->mName = result; }
//----------------------------------------------------------------// STLString USMemStream::ToString ( u32 size ) { if ( size == 0 ) return 0; if (( this->mCursor + size ) > this->mLength ) { size = this->mLength - this->mCursor; } STLString str; char buffer [ DEFAULT_CHUNK_SIZE + 1 ]; u32 readSize = DEFAULT_CHUNK_SIZE; while ( size > 0 ) { if ( size < readSize ) { readSize = size; } this->ReadBytes ( buffer, readSize ); buffer [ readSize ] = 0; str.write ( "%s", buffer ); size -= readSize; } return str; }
//----------------------------------------------------------------// int MOAILuaObject::_tostring ( lua_State* L ) { MOAILuaState state ( L ); MOAILuaObject* data = ( MOAILuaObject* )state.GetPtrUserData ( 1 ); if ( data ) { STLString str; str.write ( "0x%p <%s>", data, data->TypeName ()); // TODO: 64-bit state.Push ( str ); return 1; } return 0; }
//----------------------------------------------------------------// // This beast will walk through all tables and functions accessible in the // current lua state and print a reference line for each one found to help // track who is pointing to it. void MOAILuaRuntime::FindAndPrintLuaRefs ( int idx, cc8* prefix, FILE *f, const LeakPtrList& objects ) { lua_State* L = this->mMainState; // Convert to absolute index if ( idx < 0 ) { idx = lua_gettop(L) + idx + 1; } // Check if the item at the top of the stack has been traversed yet. lua_pushvalue ( L, -1 ); lua_gettable ( L, idx ); if( lua_type ( L, -1 ) != LUA_TNIL ) { // It has, let's bail. lua_pop ( L, 1 ); // Clean our 'true' return; } lua_pop(L, 1); // Remove the nil int tt = lua_type ( L, -1 ); if( tt == LUA_TTABLE ) { // printf("finding refs in: %s\n", prefix); // It hasn't been visited, so mark it in our traversal set lua_pushvalue ( L, -1 ); // Push table as key lua_pushboolean ( L, true ); lua_settable ( L, idx ); lua_pushnil ( L ); // first key while ( lua_next ( L, -2 ) != 0 ) { // use the 'key' (at index -2) and 'value' (at index -1) STLString key; if ( lua_type ( L, -2) == LUA_TSTRING ) { if ( MOAILuaRuntime::IsLuaIdentifier ( lua_tostring ( L, -2 ))) { key.write ( "%s.%s", prefix, lua_tostring ( L, -2 )); } else { // TODO: escape '\"' key.write ( "%s[\"%s\"]", prefix, lua_tostring ( L, -2 )); } } else { // stringify key lua_getglobal ( L, "tostring" ); lua_pushvalue ( L, -3 ); lua_call ( L, 1, 1 ); key.write ( "%s[%s]", prefix, lua_tostring ( L, -1 )); // Pop stringified key lua_pop ( L, 1 ); } this->FindAndPrintLuaRefs ( idx, key.c_str (), f, objects ); // removes 'value'; keeps 'key' for next iteration lua_pop ( L, 1 ); } // Check its metatable (if it has one) if ( lua_getmetatable ( L, -1 )) { STLString key; key.write ( "%s~mt", prefix ); this->FindAndPrintLuaRefs ( idx, key.c_str(), f, objects ); lua_pop ( L, 1 ); // Pop metatable } } else if ( tt == LUA_TFUNCTION ) { // printf("finding refs in: %s\n", prefix); // It hasn't been visited, so mark it in our tarversal set lua_pushvalue ( L, -1 ); // Push table as key lua_pushboolean ( L, true ); lua_settable ( L, idx ); const char *upname; for ( int i = 1; ( upname = lua_getupvalue ( L, -1, i )) != NULL; ++i ) { STLString key; key.write ( "%s(%s)", prefix, upname ); this->FindAndPrintLuaRefs ( idx, key.c_str(), f, objects ); // Pop the upvalue lua_pop ( L, 1 ); } } else if ( tt == LUA_TUSERDATA ) { // It hasn't been visited, so mark it in our traversal set lua_pushvalue ( L, -1 ); // Push table as key lua_pushboolean ( L, true ); lua_settable ( L, idx ); MOAILuaState state ( L ); void *ud = state.GetPtrUserData ( -1 ); for ( LeakPtrList::const_iterator i = objects.begin (); i != objects.end (); ++i ) { if( *i == ud ) { fprintf ( f, "\tLua Ref: %s = %s <%p>\n", prefix, ( *i )->TypeName (), ud ); // if ( strcmp((*i)->TypeName(), "MOAICoroutine") == 0 ) { // MOAICoroutine *t = (MOAICoroutine*)ud; // } } } // Check its metatable (if it has one) if ( lua_getmetatable ( L, -1 )) { STLString key; key.write ( "%s~mt", prefix ); this->FindAndPrintLuaRefs ( idx, key.c_str (), f, objects ); lua_pop ( L, 1 ); // Pop metatable } } }