From: John L. <jr...@us...> - 2007-12-19 06:16:42
|
Update of /cvsroot/wxlua/wxLua/modules/wxlua/src In directory sc8-pr-cvs9.sourceforge.net:/tmp/cvs-serv10699/wxLua/modules/wxlua/src Modified Files: wxlbind.cpp wxlstate.cpp Log Message: Give better error messages using the upvalue wxLuaBindMethod we push for function calls. Index: wxlstate.cpp =================================================================== RCS file: /cvsroot/wxlua/wxLua/modules/wxlua/src/wxlstate.cpp,v retrieving revision 1.153 retrieving revision 1.154 diff -C2 -d -r1.153 -r1.154 *** wxlstate.cpp 19 Dec 2007 00:42:11 -0000 1.153 --- wxlstate.cpp 19 Dec 2007 06:16:36 -0000 1.154 *************** *** 213,216 **** --- 213,255 ---- } + void LUACALL wxlua_argerror(lua_State *L, int stack_idx, const wxString& expectedType) + { + wxString argType = wxlua_gettypename(L, stack_idx); + + wxString funcArgs; + + if (lua_type(L, stack_idx) == LUA_TUSERDATA) + { + int wxl_tag = wxluaT_gettag(L, stack_idx); + + if (wxl_tag != WXLUA_NOTAG) + argType = wxluaT_gettagname(L, wxl_tag); + } + + int start_arg = wxluabind_removetableforcall(L, true) ? 2 : 1; + + wxString argMsg = wxlua_getLuaArgsMsg(L, start_arg, lua_gettop(L)); + + wxLuaBindMethod* wxlMethod = (wxLuaBindMethod *)lua_touserdata(L, lua_upvalueindex(1)); // lightuserdata + if (wxlMethod != NULL) + { + // guarantee that this is a wxLuaBindMethod of ours so we don't crash + const wxLuaBindClass* wxlClass = wxLuaBinding::GetBindClass(wxlMethod, NULL); + if (wxlClass != NULL) + { + wxArrayString a = wxlua_getBindMethodArgsMsg(L, wxlMethod); + for (size_t n = 0; n < a.GetCount(); ++n) + funcArgs += a[n] + wxT("\n"); + } + } + + wxString msg(wxString::Format(_("wxLua: Expected %s for parameter %d, but got a '%s'."), + expectedType.c_str(), stack_idx, argType.c_str())); + + msg += wxT("\nFunction called: '") + argMsg + wxT("'\n") + funcArgs; + + wxlua_error(L, msg); + } + void* LUACALL wxlua_touserdata(lua_State *L, int stack_idx, bool null_ptr /*= false*/) { *************** *** 441,456 **** } ! wxString LUACALL wxluaT_gettagname(lua_State* L, int class_tag) { // This is a predefined WXLUAARG_XXX type ! if (class_tag <= 0) ! return wxlua_getwxluatypename(class_tag); ! if (class_tag == g_wxluatag_NULL) return wxT("NULL"); ! if (class_tag == g_wxluatag_wxLuaFunction) return wxT("wxLuaFunction"); ! const wxLuaBindClass* wxlClass = wxluaT_getclass(L, class_tag); if (wxlClass) return lua2wx(wxlClass->name); --- 480,495 ---- } ! wxString LUACALL wxluaT_gettagname(lua_State* L, int wxl_tag) { // This is a predefined WXLUAARG_XXX type ! if (wxl_tag <= 0) ! return wxlua_getwxluatypename(wxl_tag); ! if (wxl_tag == g_wxluatag_NULL) return wxT("NULL"); ! if (wxl_tag == g_wxluatag_wxLuaFunction) return wxT("wxLuaFunction"); ! const wxLuaBindClass* wxlClass = wxluaT_getclass(L, wxl_tag); if (wxlClass) return lua2wx(wxlClass->name); *************** *** 501,506 **** } ! wxlua_error(L, wxString::Format(_("wxLua: Expected user defined data type '%s' for parameter %d, but got '%s'."), ! wxluaT_gettagname(L, wxl_tag).c_str(), stack_idx, wxlua_gettypename(L, stack_idx).c_str())); return NULL; --- 540,544 ---- } ! wxlua_argerror(L, stack_idx, wxT("a '") + wxluaT_gettagname(L, wxl_tag) + wxT("'")); return NULL; *************** *** 567,571 **** { // Connect the wxWindow to wxEVT_DESTROY callback so if Lua has ! // a copy(s) of it we can clear the metatable when we get the // event so we don't segfault if we try to access it by accident. wxLuaState wxlState(L); --- 605,609 ---- { // Connect the wxWindow to wxEVT_DESTROY callback so if Lua has ! // a copy(s) of it we can clear the metatable when we get the // event so we don't segfault if we try to access it by accident. wxLuaState wxlState(L); *************** *** 767,771 **** { // check for dupes since that's what we're trying to avoid ! lua_pushnumber(L, tag); lua_rawget(L, -2); // this must never happen --- 805,809 ---- { // check for dupes since that's what we're trying to avoid ! lua_pushnumber(L, tag); lua_rawget(L, -2); // this must never happen *************** *** 1178,1186 **** { if (!wxlua_isstringtype(L, stack_idx)) ! { ! wxString msg(wxString::Format(_("wxLua: Expected a string for parameter %d, but got a '%s'."), ! stack_idx, wxlua_gettypename(L, stack_idx).c_str())); ! wxlua_error(L, msg); ! } return lua_tostring(L, stack_idx); --- 1216,1220 ---- { if (!wxlua_isstringtype(L, stack_idx)) ! wxlua_argerror(L, stack_idx, wxT("a 'string'")); return lua_tostring(L, stack_idx); *************** *** 1202,1208 **** } ! wxString msg(wxString::Format(_("wxLua: Expected a string or wxString for parameter %d, but got '%s'."), ! stack_idx, wxlua_gettypename(L, stack_idx).c_str())); ! wxlua_error(L, msg); return wxEmptyString; --- 1236,1240 ---- } ! wxlua_argerror(L, stack_idx, wxT("a 'string' or 'wxString'")); return wxEmptyString; *************** *** 1214,1222 **** if (!wxlua_iswxluatype(l_type, WXLUAARG_Boolean)) ! { ! wxString msg(wxString::Format(_("wxLua: Expected a boolean for parameter %d, but got a '%s'."), ! stack_idx, wxlua_gettypename(L, stack_idx).c_str())); ! wxlua_error(L, msg); ! } int num = 0; --- 1246,1250 ---- if (!wxlua_iswxluatype(l_type, WXLUAARG_Boolean)) ! wxlua_argerror(L, stack_idx, wxT("a 'boolean'")); int num = 0; *************** *** 1234,1242 **** if (!wxlua_iswxluatype(l_type, WXLUAARG_Integer)) ! { ! wxString msg(wxString::Format(_("wxLua: Expected an integer for parameter %d, but got a '%s'."), ! stack_idx, wxlua_gettypename(L, stack_idx).c_str())); ! wxlua_error(L, msg); ! } double value = 0; --- 1262,1266 ---- if (!wxlua_iswxluatype(l_type, WXLUAARG_Integer)) ! wxlua_argerror(L, stack_idx, wxT("an 'integer'")); double value = 0; *************** *** 1248,1256 **** if (value != (long)value) ! { ! wxString msg(wxString::Format(_("wxLua: Expected an integer for parameter %d, but got a floating point number '%lf'."), ! stack_idx, value)); ! wxlua_error(L, msg); ! } return (long)value; --- 1272,1276 ---- if (value != (long)value) ! wxlua_argerror(L, stack_idx, wxT("an 'integer'")); return (long)value; *************** *** 1261,1269 **** if (!wxlua_iswxluatype(l_type, WXLUAARG_Number)) ! { ! wxString msg(wxString::Format(_("wxLua: Expected a number for parameter %d, but got a '%s'."), ! stack_idx, wxlua_gettypename(L, stack_idx).c_str())); ! wxlua_error(L, msg); ! } double value = 0; --- 1281,1285 ---- if (!wxlua_iswxluatype(l_type, WXLUAARG_Number)) ! wxlua_argerror(L, stack_idx, wxT("a 'number'")); double value = 0; *************** *** 1302,1310 **** } else ! { ! wxString msg(wxString::Format(_("wxLua: Expected a table of strings for parameter %d, but got a '%s'."), ! stack_idx, wxlua_gettypename(L, stack_idx).c_str())); ! wxlua_error(L, msg); ! } return arrChar; --- 1318,1322 ---- } else ! wxlua_argerror(L, stack_idx, wxT("a 'table' array of strings")); return arrChar; *************** *** 1366,1372 **** else { ! wxString msg(wxString::Format(_("wxLua: Expected a wxArrayString or table of strings for parameter %d, but table index %d is '%s'."), ! stack_idx, count+1, wxlua_gettypename(L, -1).c_str())); ! wxlua_error(L, msg); return arr; } --- 1378,1382 ---- else { ! wxlua_argerror(L, stack_idx, wxT("a 'wxArrayString' or table array of strings")); return arr; } *************** *** 1389,1397 **** if (count < 0) ! { ! wxString msg(wxString::Format(_("wxLua: Expected a wxArrayString or table of strings for parameter %d, but got '%s'."), ! stack_idx, wxlua_gettypename(L, stack_idx).c_str())); ! wxlua_error(L, msg); ! } return arr; --- 1399,1403 ---- if (count < 0) ! wxlua_argerror(L, stack_idx, wxT("a 'wxArrayString' or table array of strings")); return arr; *************** *** 1425,1433 **** if (count < 0) ! { ! wxString msg(wxString::Format(_("wxLua: Expected a wxArrayString or table of strings for parameter %d, but got '%s'."), ! stack_idx, wxlua_gettypename(L, stack_idx).c_str())); ! wxlua_error(L, msg); ! } return arr; --- 1431,1435 ---- if (count < 0) ! wxlua_argerror(L, stack_idx, wxT("a 'wxArrayString' or table array of strings")); return arr; *************** *** 1461,1467 **** else { ! wxString msg(wxString::Format(_("wxLua: Expected a wxArrayInt or a table of integers for parameter %d, but table index %d is '%s'."), ! stack_idx, count+1, wxlua_gettypename(L, -1).c_str())); ! wxlua_error(L, msg); return arr; } --- 1463,1467 ---- else { ! wxlua_argerror(L, stack_idx, wxT("a 'wxArrayInt' or a table array of integers")); return arr; } *************** *** 1484,1492 **** if (count < 0) ! { ! wxString msg(wxString::Format(_("wxLua: Expected a wxArrayInt or a table of integers for parameter %d, but got '%s'."), ! stack_idx, wxlua_gettypename(L, stack_idx).c_str())); ! wxlua_error(L, msg); ! } return arr; --- 1484,1488 ---- if (count < 0) ! wxlua_argerror(L, stack_idx, wxT("a 'wxArrayInt' or a table array of integers")); return arr; Index: wxlbind.cpp =================================================================== RCS file: /cvsroot/wxlua/wxLua/modules/wxlua/src/wxlbind.cpp,v retrieving revision 1.107 retrieving revision 1.108 diff -C2 -d -r1.107 -r1.108 *** wxlbind.cpp 19 Dec 2007 00:42:11 -0000 1.107 --- wxlbind.cpp 19 Dec 2007 06:16:36 -0000 1.108 *************** *** 101,106 **** } - wxString wxlua_CreateArgsMsg(lua_State* L, int start_stack_idx, int end_stack_idx); - int LUACALL wxlua_CallOverloadedFunction(lua_State* L, struct wxLuaBindMethod* wxlMethod) { --- 101,104 ---- *************** *** 204,211 **** // Did not find a suitable function to call, post error ! wxString fnCall = wxlua_CreateArgsMsg(L, arg_lua_start, arg_lua_start+arg_lua_count-1); ! wxString fnOverloadList = wxT("wxLua Function Overload Table:\n"); ! wxArrayString overloadMethodArray = wxlua_CreateMethodArgTagsMsg(L, wxlMethod); for (i = 0; i < (int)overloadMethodArray.GetCount(); ++i) fnOverloadList += overloadMethodArray[i] + wxT("\n"); --- 202,209 ---- // Did not find a suitable function to call, post error ! wxString fnCall = wxlua_getLuaArgsMsg(L, arg_lua_start, arg_lua_start+arg_lua_count-1); ! wxString fnOverloadList = wxString::Format(wxT("Function called: '%s'\n"), fnCall.c_str()); ! wxArrayString overloadMethodArray = wxlua_getBindMethodArgsMsg(L, wxlMethod); for (i = 0; i < (int)overloadMethodArray.GetCount(); ++i) fnOverloadList += overloadMethodArray[i] + wxT("\n"); *************** *** 219,223 **** if (bestFunc == NULL) ! errmsg += wxString::Format(wxT("wxLua: Function call '%s' has invalid arguments.\n%s"), fnCall.c_str(), fnOverloadList.c_str()); else { --- 217,221 ---- if (bestFunc == NULL) ! errmsg += wxString::Format(wxT("wxLua: Function call has invalid arguments.\n%s"), fnOverloadList.c_str()); else { *************** *** 242,246 **** } ! errmsg += wxString::Format(wxT("wxLua: Function call '%s' has invalid argument %d on method %02d.\n%s"), fnCall.c_str(), invalid_lua_arg, i_func, fnOverloadList.c_str()); } --- 240,244 ---- } ! errmsg += wxString::Format(wxT("wxLua: Function call has invalid argument %d on method %02d.\n%s"), invalid_lua_arg, i_func, fnOverloadList.c_str()); } *************** *** 250,254 **** } ! wxString wxlua_CreateArgsMsg(lua_State* L, int start_stack_idx, int end_stack_idx) { lua_Debug ar; --- 248,252 ---- } ! wxString wxlua_getLuaArgsMsg(lua_State* L, int start_stack_idx, int end_stack_idx) { lua_Debug ar; *************** *** 257,261 **** wxString funcName = lua2wx(ar.name); - // Build an error message wxString funcCall = funcName + wxT("("); --- 255,258 ---- *************** *** 281,285 **** } ! wxArrayString wxlua_CreateMethodArgTagsMsg(lua_State* L, struct wxLuaBindMethod* wxlMethod) { wxArrayString overloadMethodArray; --- 278,282 ---- } ! wxArrayString wxlua_getBindMethodArgsMsg(lua_State* L, struct wxLuaBindMethod* wxlMethod) { wxArrayString overloadMethodArray; *************** *** 948,952 **** bool wxLuaBinding::sm_bindingList_initialized = false; ! wxLuaBinding::wxLuaBinding() :m_classCount(0), m_classArray(NULL), m_defineCount(0), m_defineArray(NULL), --- 945,949 ---- bool wxLuaBinding::sm_bindingList_initialized = false; ! wxLuaBinding::wxLuaBinding() :m_classCount(0), m_classArray(NULL), m_defineCount(0), m_defineArray(NULL), |