From: John L. <jr...@us...> - 2007-12-08 00:25:28
|
Update of /cvsroot/wxlua/wxLua/modules/wxlua/src In directory sc8-pr-cvs9.sourceforge.net:/tmp/cvs-serv14401/wxLua/modules/wxlua/src Modified Files: wxlbind.cpp wxlstate.cpp Log Message: Move the wxLuaState s_wxHashMapLuaState to the wxLuaState since that's what it gets Cleanup the wxlua_CallOverloadedFunction a little Index: wxlstate.cpp =================================================================== RCS file: /cvsroot/wxlua/wxLua/modules/wxlua/src/wxlstate.cpp,v retrieving revision 1.147 retrieving revision 1.148 diff -C2 -d -r1.147 -r1.148 *** wxlstate.cpp 7 Dec 2007 06:44:46 -0000 1.147 --- wxlstate.cpp 8 Dec 2007 00:25:24 -0000 1.148 *************** *** 108,111 **** --- 108,112 ---- if (hookData->m_debug_hook_break) { + // It's ok that we get the wxLuaState here since we're stopping anyway. wxLuaState wxlState(L); *************** *** 116,120 **** } ! if (hookData->m_lua_debug_hook_send_evt) { wxLuaState wxlState(L); --- 117,123 ---- } ! // Assume they've set the wxEvtHandler, ok if not, but it wouldn't make sense ! // We use wxLuaState::SendEvent() because it sets wxEvent::SetEventObject() for us. ! if (hookData->m_lua_debug_hook_send_evt) { wxLuaState wxlState(L); *************** *** 291,296 **** lua_rawget(L, -2); // get t[key] = value; pop key push value ! if (lua_isnumber(L, -1)) ! tag = (int)lua_tonumber(L, -1); lua_pop(L, 2); // pop object we pushed and the ref table --- 294,300 ---- lua_rawget(L, -2); // get t[key] = value; pop key push value ! tag = (int)lua_tonumber(L, -1); ! if ((tag == 0) && !lua_isnumber(L, -1)) // if !isnumber it returns 0 ! tag = LUA_NOREF; lua_pop(L, 2); // pop object we pushed and the ref table *************** *** 429,432 **** --- 433,451 ---- } + bool wxluaT_isuserdatatype(lua_State* L, int stack_idx, int tag) + { + if (wxlua_iswxuserdata(L, stack_idx)) + { + int stack_tag = wxluaT_gettag(L, stack_idx); + if ((g_wxluatag_NULL == stack_tag) || // FIXME, how to check when NULL is valid or not? + (g_wxluatag_wxLuaFunction == stack_tag) || + ((tag == WXLUAARG_String) && (wxlua_isderivedclass(L, stack_tag, g_wxluatag_wxString) >= 0)) || + (wxlua_isderivedclass(L, stack_tag, tag) >= 0)) + return true; + } + + return false; + } + void* LUACALL wxluaT_getuserdatatype(lua_State* L, int stack_idx, int tag) { *************** *** 862,880 **** } - bool wxlua_isuserdatatype(lua_State* L, int stack_idx, int tag) - { - if (wxlua_iswxuserdata(L, stack_idx)) - { - int stack_tag = wxluaT_gettag(L, stack_idx); - if ((g_wxluatag_NULL == stack_tag) || // FIXME, how to check when NULL is valid or not? - (g_wxluatag_wxLuaFunction == stack_tag) || - ((tag == WXLUAARG_String) && (wxlua_isderivedclass(L, stack_tag, g_wxluatag_wxString) >= 0)) || - (wxlua_isderivedclass(L, stack_tag, tag) >= 0)) - return true; - } - - return false; - } - wxString wxlua_getwxluatypename(int wxluaarg_tag) { --- 881,884 ---- *************** *** 1174,1178 **** int arrstr_tag = wxluaT_gettag(L, "wxArrayString"); ! if (wxlua_isuserdatatype(L, stack_idx, arrstr_tag)) { wxArrayString *arrStr = (wxArrayString *)wxluaT_getuserdatatype(L, stack_idx, arrstr_tag); --- 1178,1182 ---- int arrstr_tag = wxluaT_gettag(L, "wxArrayString"); ! if (wxluaT_isuserdatatype(L, stack_idx, arrstr_tag)) { wxArrayString *arrStr = (wxArrayString *)wxluaT_getuserdatatype(L, stack_idx, arrstr_tag); *************** *** 1210,1214 **** int arrstr_tag = wxluaT_gettag(L, "wxArrayString"); ! if (wxlua_isuserdatatype(L, stack_idx, arrstr_tag)) { wxSortedArrayString *arrStr = (wxSortedArrayString *)wxluaT_getuserdatatype(L, stack_idx, arrstr_tag); --- 1214,1218 ---- int arrstr_tag = wxluaT_gettag(L, "wxArrayString"); ! if (wxluaT_isuserdatatype(L, stack_idx, arrstr_tag)) { wxSortedArrayString *arrStr = (wxSortedArrayString *)wxluaT_getuserdatatype(L, stack_idx, arrstr_tag); *************** *** 1269,1273 **** int arrint_tag = wxluaT_gettag(L, "wxArrayInt"); ! if (wxlua_isuserdatatype(L, stack_idx, arrint_tag)) { wxArrayInt *arrInt = (wxArrayInt *)wxluaT_getuserdatatype(L, stack_idx, arrint_tag); --- 1273,1277 ---- int arrint_tag = wxluaT_gettag(L, "wxArrayInt"); ! if (wxluaT_isuserdatatype(L, stack_idx, arrint_tag)) { wxArrayInt *arrInt = (wxArrayInt *)wxluaT_getuserdatatype(L, stack_idx, arrint_tag); *************** *** 1495,1499 **** wxLuaDebugHookData* data = (wxLuaDebugHookData*)lua_touserdata(L, -1); ! lua_pop(L, 1); // pop number return data; --- 1499,1503 ---- wxLuaDebugHookData* data = (wxLuaDebugHookData*)lua_touserdata(L, -1); ! lua_pop(L, 1); // pop udata return data; *************** *** 1684,1689 **** //---------------------------------------------------------------------------- - wxHashMapLuaState wxLuaStateRefData::s_wxHashMapLuaState; - wxLuaStateRefData::wxLuaStateRefData(bool create_data) :m_lua_State(NULL), --- 1688,1691 ---- *************** *** 1818,1828 **** // The wxLuaState we pushed into the reg table is a light userdata so // it didn't get deleted. ! wxHashMapLuaState::iterator it = s_wxHashMapLuaState.find(m_lua_State); ! if (it != s_wxHashMapLuaState.end()) { wxLuaState* wxlState = it->second; wxlState->SetRefData(NULL); delete wxlState; ! s_wxHashMapLuaState.erase(m_lua_State); } --- 1820,1830 ---- // The wxLuaState we pushed into the reg table is a light userdata so // it didn't get deleted. ! wxHashMapLuaState::iterator it = wxLuaState::s_wxHashMapLuaState.find(m_lua_State); ! if (it != wxLuaState::s_wxHashMapLuaState.end()) { wxLuaState* wxlState = it->second; wxlState->SetRefData(NULL); delete wxlState; ! wxLuaState::s_wxHashMapLuaState.erase(m_lua_State); } *************** *** 1883,1932 **** } - wxLuaState wxLuaStateRefData::GetLuaState(lua_State* L) - { - // try our hashtable for faster lookup - wxHashMapLuaState::iterator it = s_wxHashMapLuaState.find(L); - if (it != s_wxHashMapLuaState.end()) - { - return wxLuaState(*it->second); - } - - // else it's a coroutine? look up the state data from Lua - wxLuaState* wxlState = NULL; - - // try to get the state we've stored - lua_pushlightuserdata(L, &wxlua_lreg_wxluastate_key); - lua_rawget( L, LUA_REGISTRYINDEX ); - - // if nothing was returned or it wasn't a ptr, abort - if ( lua_islightuserdata(L, -1) ) - wxlState = (wxLuaState*)lua_touserdata( L, -1 ); - - lua_pop(L, 1); // pop the wxLuaStateRefData or nil on failure - - if (wxlState && (wxlState->GetLuaState() != L)) - { - // Create a new wxLuaState for the coroutine and set the wxLuaStateData - // to the original wxLuaState's data - wxLuaStateRefData* refData = new wxLuaStateRefData(false); - refData->m_lua_State = L; - refData->m_lua_State_static = true; - refData->m_lua_State_coroutine = true; - - refData->m_wxlStateData = wxlState->GetLuaStateData(); - refData->m_own_stateData = false; - - wxLuaState wxlState2(false); - wxlState2.SetRefData(refData); - return wxlState2; - } - else if (wxlState) - { - return wxLuaState(*wxlState); // Ref it - } - - return wxNullLuaState; - } - //---------------------------------------------------------------------------- // wxLuaState --- 1885,1888 ---- *************** *** 1935,1938 **** --- 1891,1896 ---- IMPLEMENT_DYNAMIC_CLASS(wxLuaState, wxObject) + wxHashMapLuaState wxLuaState::s_wxHashMapLuaState; + #define M_WXLSTATEDATA ((wxLuaStateRefData*)m_refData) *************** *** 1985,1989 **** { // returns an invalid, wxNullLuaState on failure ! Ref(wxLuaStateRefData::GetLuaState(L)); } else // state_type == wxLUASTATE_USESTATE or wxLUASTATE_SETSTATE --- 1943,1947 ---- { // returns an invalid, wxNullLuaState on failure ! Ref(wxLuaState::GetwxLuaState(L)); } else // state_type == wxLUASTATE_USESTATE or wxLUASTATE_SETSTATE *************** *** 1998,2002 **** wxLuaState* hashState = new wxLuaState(false); hashState->SetRefData(m_refData); ! wxLuaStateRefData::s_wxHashMapLuaState[L] = hashState; // Stick us into the Lua registry table - push key, value --- 1956,1960 ---- wxLuaState* hashState = new wxLuaState(false); hashState->SetRefData(m_refData); ! wxLuaState::s_wxHashMapLuaState[L] = hashState; // Stick us into the Lua registry table - push key, value *************** *** 2086,2089 **** --- 2044,2049 ---- } + // -------------------------------------------------------------------------- + bool wxLuaState::Ok() const { *************** *** 2091,2094 **** --- 2051,2056 ---- } + // -------------------------------------------------------------------------- + void wxLuaState::Destroy() { *************** *** 2114,2117 **** --- 2076,2081 ---- } + // -------------------------------------------------------------------------- + lua_State* wxLuaState::GetLuaState() const { *************** *** 2126,2129 **** --- 2090,2141 ---- } + wxLuaState wxLuaState::GetwxLuaState(lua_State* L) + { + // try our hashtable for faster lookup + wxHashMapLuaState::iterator it = s_wxHashMapLuaState.find(L); + if (it != s_wxHashMapLuaState.end()) + { + return wxLuaState(*it->second); + } + + // else it's a coroutine? look up the state data from Lua + wxLuaState* wxlState = NULL; + + // try to get the state we've stored + lua_pushlightuserdata(L, &wxlua_lreg_wxluastate_key); + lua_rawget( L, LUA_REGISTRYINDEX ); + + // if nothing was returned or it wasn't a ptr, abort + if ( lua_islightuserdata(L, -1) ) + wxlState = (wxLuaState*)lua_touserdata( L, -1 ); + + lua_pop(L, 1); // pop the wxLuaStateRefData or nil on failure + + if (wxlState && (wxlState->GetLuaState() != L)) + { + // Create a new wxLuaState for the coroutine and set the wxLuaStateData + // to the original wxLuaState's data + wxLuaStateRefData* refData = new wxLuaStateRefData(false); + refData->m_lua_State = L; + refData->m_lua_State_static = true; + refData->m_lua_State_coroutine = true; + + refData->m_wxlStateData = wxlState->GetLuaStateData(); + refData->m_own_stateData = false; + + wxLuaState wxlState2(false); + wxlState2.SetRefData(refData); + return wxlState2; + } + else if (wxlState) + { + return wxLuaState(*wxlState); // Ref it + } + + return wxNullLuaState; + } + + // -------------------------------------------------------------------------- + void wxLuaState::SetEventHandler(wxEvtHandler *evtHandler) { *************** *** 2913,2917 **** { wxCHECK_MSG(Ok(), false, wxT("Invalid wxLuaState")); ! return wxlua_isuserdatatype(M_WXLSTATEDATA->m_lua_State, stack_idx, tag); } --- 2925,2929 ---- { wxCHECK_MSG(Ok(), false, wxT("Invalid wxLuaState")); ! return wxluaT_isuserdatatype(M_WXLSTATEDATA->m_lua_State, stack_idx, tag); } *************** *** 3053,3058 **** wxHashMapLuaState::iterator it; ! for (it = wxLuaStateRefData::s_wxHashMapLuaState.begin(); ! it != wxLuaStateRefData::s_wxHashMapLuaState.end(); ++it) { wxLuaState wxlState((wxLuaState*)it->second); --- 3065,3070 ---- wxHashMapLuaState::iterator it; ! for (it = wxLuaState::s_wxHashMapLuaState.begin(); ! it != wxLuaState::s_wxHashMapLuaState.end(); ++it) { wxLuaState wxlState((wxLuaState*)it->second); *************** *** 3782,3798 **** } - - wxArrayString wxLuaState::CreateMethodArgTagsMsg(struct wxLuaBindMethod* wxlMethod) - { - wxCHECK_MSG(Ok() && wxlMethod, wxArrayString(), wxT("Invalid wxLuaState or overloaded method table")); - return wxlua_CreateMethodArgTagsMsg(M_WXLSTATEDATA->m_lua_State, wxlMethod); - } - - int LUACALL wxLuaState::CallOverloadedFunction(struct wxLuaBindMethod* wxlMethod) - { - wxCHECK_MSG(Ok() && wxlMethod, 0, wxT("Invalid wxLuaState or overloaded method table")); - return wxlua_CallOverloadedFunction(M_WXLSTATEDATA->m_lua_State, wxlMethod); - } - //----------------------------------------------------------------------------- // wxLuaEvent --- 3794,3797 ---- Index: wxlbind.cpp =================================================================== RCS file: /cvsroot/wxlua/wxLua/modules/wxlua/src/wxlbind.cpp,v retrieving revision 1.101 retrieving revision 1.102 diff -C2 -d -r1.101 -r1.102 *** wxlbind.cpp 7 Dec 2007 02:24:56 -0000 1.101 --- wxlbind.cpp 8 Dec 2007 00:25:24 -0000 1.102 *************** *** 58,65 **** if (m_wxlMethod->basemethod) ! { ! wxLuaState wxlState(L); ! return wxlState.CallOverloadedFunction(m_wxlMethod); ! } return (*m_wxlMethod->funcs[0].func)(L); --- 58,62 ---- if (m_wxlMethod->basemethod) ! return wxlua_CallOverloadedFunction(L, m_wxlMethod); return (*m_wxlMethod->funcs[0].func)(L); *************** *** 96,112 **** { int i, arg; - int invalidArg = 0; // get number of arguments called from Lua ! int argCount = lua_gettop(L); ! int lua_argStart = 0; // don't remove the table, but do skip past it when counting args ! bool remove_table = wxluabind_removetableforcall(L, true); ! ! if (remove_table) { ! argCount--; ! lua_argStart++; } --- 93,106 ---- { int i, arg; // get number of arguments called from Lua ! int arg_count = lua_gettop(L); ! int arg_start = 0; // don't remove the table, but do skip past it when counting args ! if (wxluabind_removetableforcall(L, true)) { ! arg_count--; ! arg_start++; } *************** *** 119,124 **** { if (!WXLUA_HASBIT(method->funcs[i].type, WXLUAMETHOD_OVERLOAD) && ! (argCount >= method->funcs[i].minargs) && ! (argCount <= method->funcs[i].maxargs)) { funcArray.Add(&method->funcs[i]); --- 113,118 ---- { if (!WXLUA_HASBIT(method->funcs[i].type, WXLUAMETHOD_OVERLOAD) && ! (arg_count >= method->funcs[i].minargs) && ! (arg_count <= method->funcs[i].maxargs)) { funcArray.Add(&method->funcs[i]); *************** *** 130,142 **** } ! wxLuaBindCFunc* bestFunc = NULL; // store the last function that worked. // Look at the available functions in parallel, per arg ! for (arg = 0; (arg < argCount) && (funcArray.GetCount() != 0); arg++) { ! int arg_lua = arg+1+lua_argStart; // arg N in Lua int ltype = lua_type(L, arg_lua); ! for (i = 0; i < (int)funcArray.GetCount(); i++) { wxLuaBindCFunc* func = (wxLuaBindCFunc*)funcArray[i]; --- 124,138 ---- } ! wxLuaBindCFunc* bestFunc = NULL; // stores the last function that worked. ! int invalidArg = 0; // arg that failed ! int func_count = funcArray.GetCount(); // Look at the available functions in parallel, per arg ! for (arg = 0; (arg < arg_count) && (func_count != 0); ++arg) { ! int arg_lua = arg+1+arg_start; // arg N on the Lua stack int ltype = lua_type(L, arg_lua); ! for (i = 0; i < func_count; ++i) { wxLuaBindCFunc* func = (wxLuaBindCFunc*)funcArray[i]; *************** *** 144,153 **** invalidArg = arg; - int arg_wxlua = arg; // arg N in wxLuaArgTag - // does this method have any more arguments? ! if (!func->argtags[arg_wxlua]) { funcArray.RemoveAt(i); i--; continue; --- 140,148 ---- invalidArg = arg; // does this method have any more arguments? ! if (!func->argtags[arg]) { funcArray.RemoveAt(i); + func_count--; i--; continue; *************** *** 155,161 **** // get argument tag id ! int tag = (int)*(func->argtags[arg_wxlua]); ! //wxPrintf(wxT("ARG '%s' type %d argCount %d arg %d arg_lua %d arg_wxlua %d ltype %d wxtype %d func_count %d/%d\n"), lua2wx(wxlMethod->name).c_str(), func->type, argCount, arg, arg_lua, arg_wxlua, ltype, tag, i, funcArray.GetCount()); // Does the Lua type match the wxlua arg tag type --- 150,156 ---- // get argument tag id ! int tag = (int)*(func->argtags[arg]); ! //wxPrintf(wxT("ARG '%s' type %d arg_count %d arg %d arg_lua %d ltype %d wxtype %d func_count %d/%d\n"), lua2wx(wxlMethod->name).c_str(), func->type, arg_count, arg, arg_lua, ltype, tag, i, funcArray.GetCount()); // Does the Lua type match the wxlua arg tag type *************** *** 165,170 **** if ((is_ok == -1) || ((is_ok == 0) && (tag == WXLUAARG_String))) { ! is_ok = (wxlua_isuserdatatype(L, arg_lua, tag) || ! (tag == g_wxluatag_NULL)) ? 1 : 0; } --- 160,165 ---- if ((is_ok == -1) || ((is_ok == 0) && (tag == WXLUAARG_String))) { ! is_ok = (wxluaT_isuserdatatype(L, arg_lua, tag) || ! (tag == g_wxluatag_NULL)) ? 1 : 0; } *************** *** 173,176 **** --- 168,172 ---- { funcArray.RemoveAt(i); + func_count--; i--; continue; *************** *** 182,186 **** // Note that the top function is the one that is highest in the // derived functions from any baseclasses and should be the best choice. ! if (funcArray.GetCount() > 0) { lua_CFunction func = ((wxLuaBindCFunc*)funcArray[0])->func; --- 178,182 ---- // Note that the top function is the one that is highest in the // derived functions from any baseclasses and should be the best choice. ! if (funcArray.GetCount() == 1) { lua_CFunction func = ((wxLuaBindCFunc*)funcArray[0])->func; *************** *** 197,206 **** // Build an error message wxString fnCall = name + wxT("("); ! for (arg = 0; arg < argCount; arg++) { ! if (arg > 0) ! fnCall += wxT(", "); ! int ltype = lua_type(L, arg+1+lua_argStart); if (ltype != LUA_TUSERDATA) --- 193,201 ---- // Build an error message wxString fnCall = name + wxT("("); ! for (arg = 0; arg < arg_count; arg++) { ! if (arg > 0) fnCall += wxT(", "); ! int ltype = lua_type(L, arg+1+arg_start); if (ltype != LUA_TUSERDATA) *************** *** 210,214 **** else { ! int tag = wxluaT_gettag(L, arg+1+lua_argStart); fnCall += wxluaT_gettagname(L, tag); } --- 205,209 ---- else { ! int tag = wxluaT_gettag(L, arg+1+arg_start); fnCall += wxluaT_gettagname(L, tag); } |