//----------------------------------------------------------------// void MOAIHttpTaskNaCl::Prepare ( GetURLHandler *handler ) { // until we get a header indicating otherwise, assume we won't // know the final length of the stream, so default to use the // USMemStream which will grow dynamically this->mStream = &this->mMemStream; char buffer [ MAX_HEADER_LENGTH ]; int written = 0; // prepare the custom headers (if any) HeaderMapIt headerMapIt = this->mHeaderMap.begin (); for ( ; headerMapIt != this->mHeaderMap.end (); ++headerMapIt ) { STLString key = headerMapIt->first; STLString value = headerMapIt->second; assert (( written + ( key.size () + value.size () + 3 )) < MAX_HEADER_LENGTH ); if ( value.size ()) { written += sprintf ( buffer + written, "%s: %s\n", key.c_str (), value.c_str ()); } else { written += sprintf ( buffer + written, "%s:\n", key.c_str ()); } } //append headers handler->SetHeaders ( buffer ); }
//----------------------------------------------------------------// void MOAIHttpTaskCurl::Prepare () { // until we get a header indicating otherwise, assume we won't // know the final length of the stream, so default to use the // USMemStream which will grow dynamically if ( this->mUserStream ) { this->mStream = this->mUserStream->GetUSStream(); } else { this->mStream = &this->mMemStream; } char buffer [ MAX_HEADER_LENGTH ]; // prepare the custom headers (if any) HeaderMapIt headerMapIt = this->mHeaderMap.begin (); for ( ; headerMapIt != this->mHeaderMap.end (); ++headerMapIt ) { STLString key = headerMapIt->first; STLString value = headerMapIt->second; assert (( key.size () + value.size () + 3 ) < MAX_HEADER_LENGTH ); if ( value.size ()) { sprintf ( buffer, "%s: %s", key.c_str (), value.c_str ()); } else { sprintf ( buffer, "%s:", key.c_str ()); } this->mHeaderList = curl_slist_append ( this->mHeaderList, buffer ); } if ( this->mHeaderList ) { CURLcode result = curl_easy_setopt ( this->mEasyHandle, CURLOPT_HTTPHEADER, this->mHeaderList ); PrintError ( result ); } CURLcode result = curl_easy_setopt ( this->mEasyHandle, CURLOPT_CONNECTTIMEOUT, this->mDefaultTimeout ); // follow redirects based on settings in base class (default is to NOT follow redirects) result = curl_easy_setopt ( this->mEasyHandle, CURLOPT_FOLLOWLOCATION, this->mFollowRedirects ); // set the timeout for this task result = curl_easy_setopt ( this->mEasyHandle, CURLOPT_TIMEOUT, this->mTimeout ); PrintError ( result ); }
//----------------------------------------------------------------// u32 USLuaSerializer::WriteTableInitializer ( USStream& stream, USLuaState& state, int idx, cc8* prefix ) { u32 count = 0; u32 itr = state.PushTableItr ( idx ); while ( state.TableItrNext ( itr )) { switch ( lua_type ( state, -2 )) { case LUA_TSTRING: { stream.Print ( "\t%s [ \"%s\" ] = ", prefix, lua_tostring ( state, -2 )); break; } case LUA_TNUMBER: { stream.Print ( "\t%s [ %s ]\t= ", prefix, lua_tostring ( state, -2 )); break; } }; switch ( lua_type ( state, -1 )) { case LUA_TBOOLEAN: { int value = lua_toboolean ( state, -1 ); cc8* str = ( value ) ? "true": "false"; stream.Print ( "%s\n", str ); break; } case LUA_TTABLE: { uintptr tableID = ( uintptr )lua_topointer ( state, -1 ); if ( this->mTableMap.contains ( tableID )) { stream.Print ( "objects [ 0x%08X ]\n", tableID ); } break; } case LUA_TSTRING: { STLString str = _escapeString ( lua_tostring ( state, -1 )); stream.Print ( "\"%s\"\n", str.c_str ()); break; } case LUA_TNUMBER: { stream.Print ( "%s\n", lua_tostring ( state, -1 )); break; } case LUA_TUSERDATA: { USLuaObject* object = state.GetLuaObject < USLuaObject >( -1 ); u32 instanceID = this->GetID ( object ); stream.Print ( "objects [ 0x%08X ]\n", instanceID ); break; } case LUA_TLIGHTUSERDATA: { stream.Print ( "%p,\n", lua_touserdata ( state, -1 )); break; } }; ++count; } return count; }
//----------------------------------------------------------------// bool USLuaState::PrintErrors ( int status ) { if ( status != 0 ) { cc8* error = lua_tostring ( this->mState, -1 ); if ( error ) { STLString msg = lua_tostring ( this->mState, -1 ); USLog::Print ( "-- %s\n", msg.c_str ()); } lua_pop ( this->mState, 1 ); // pop error message return true; } return false; }
//----------------------------------------------------------------// bool MOAILuaState::PrintErrors ( FILE* file, int status ) { if ( status != 0 ) { cc8* error = lua_tostring ( this->mState, -1 ); if ( error ) { STLString msg = lua_tostring ( this->mState, -1 ); // TODO: Fix this on Android #ifndef MOAI_OS_ANDROID ZLLog::PrintFile ( file, "-- %s\n", msg.c_str ()); #endif } lua_pop ( this->mState, 1 ); // pop error message return true; } return false; }
//----------------------------------------------------------------// u32 USLuaSerializer::WriteTable ( USStream& stream, USLuaState& state, int idx, u32 tab ) { STLString indent; for ( u32 i = 0; i < tab; ++i ) { indent.append ( "\t" ); } u32 count = 0; u32 itr = state.PushTableItr ( idx ); while ( state.TableItrNext ( itr )) { if ( count == 0 ) { stream.Print ( "\n" ); } switch ( lua_type ( state, -2 )) { case LUA_TSTRING: { stream.Print ( "%s[ \"%s\" ] = ", indent.c_str (), lua_tostring ( state, -2 )); break; } case LUA_TNUMBER: { stream.Print ( "%s[ %s ]\t= ", indent.c_str (), lua_tostring ( state, -2 )); break; } }; switch ( lua_type ( state, -1 )) { case LUA_TBOOLEAN: { int value = lua_toboolean ( state, -1 ); cc8* str = ( value ) ? "true": "false"; stream.Print ( "%s,\n", str ); break; } case LUA_TTABLE: { uintptr tableID = ( uintptr )lua_topointer ( state, -1 ); if ( this->mTableMap.contains ( tableID )) { stream.Print ( "objects [ 0x%08X ],\n", tableID ); } else { stream.Print ( "{" ); if ( this->WriteTable ( stream, state, -1, tab + 1 )) { stream.Print ( "%s},\n", indent.c_str ()); } else { stream.Print ( "},\n" ); } } break; } case LUA_TSTRING: { STLString str = _escapeString ( lua_tostring ( state, -1 )); stream.Print ( "\"%s\",\n", str.c_str ()); break; } case LUA_TNUMBER: { stream.Print ( "%s,\n", lua_tostring ( state, -1 )); break; } case LUA_TLIGHTUSERDATA: { stream.Print ( "%p,\n", lua_touserdata ( state, -1 )); break; } }; ++count; } return count; }
//----------------------------------------------------------------// // 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 } } }