From: John L. <jr...@us...> - 2007-06-06 03:53:45
|
Update of /cvsroot/wxlua/wxLua/modules/wxlua/src In directory sc8-pr-cvs9.sourceforge.net:/tmp/cvs-serv25933/modules/wxlua/src Modified Files: wxlbind.cpp wxlstate.cpp Log Message: Switch s_wxluaarg_Enumeration to s_wxluaarg_Enum Add the rest of the LUA_TXXX types as s_wxluaarg_XXX and create #defines as well as functions to verify them with the result of lua_type() Unify all of the wxlua_isXXXtype functions to all call the same function Get rid of wxLuaStringToLongHashMap in wxlstate.h and don't use it anymore in wxLuaCheckStack since we can just use a wxSortedArrayString Remove wxLuaState::GetBaseLuaClass since it wasn't used anywhere Cleanup wxLuaState::CallOverloadedFunction to use new check function Index: wxlstate.cpp =================================================================== RCS file: /cvsroot/wxlua/wxLua/modules/wxlua/src/wxlstate.cpp,v retrieving revision 1.99 retrieving revision 1.100 diff -C2 -d -r1.99 -r1.100 *** wxlstate.cpp 5 Jun 2007 21:07:25 -0000 1.99 --- wxlstate.cpp 6 Jun 2007 03:53:40 -0000 1.100 *************** *** 283,287 **** } ! lua_pushvalue(L, stack_idx); // push a copy of value to store to top int nTop = lua_gettop(L); // this is where the value is --- 283,287 ---- } ! lua_pushvalue(L, stack_idx); // push a copy of value to store to top int nTop = lua_gettop(L); // this is where the value is *************** *** 370,389 **** int LUACALL wxlua_ttag(lua_State *L, int stack_idx) { - //wxPrintf(wxT("wxlua_ttag %d"), stack_idx); - int tag = TLUA_NOTAG; if (lua_getmetatable(L, stack_idx) != 0) // see tnewtag { ! //wxPrintf(wxT(" has metatable")); ! lua_pushliteral(L, "tag"); // get t["tag"] lua_rawget(L, -2); ! if (lua_isnumber(L, -1)) // FIXME this is really an error is this ever happens { tag = (int)lua_tonumber(L, -1); - //wxPrintf(wxT(" and is number %d"), tag); } ! lua_pop(L, 2); } - //wxPrintf(wxT("\n")); return tag; } --- 370,384 ---- int LUACALL wxlua_ttag(lua_State *L, int stack_idx) { int tag = TLUA_NOTAG; if (lua_getmetatable(L, stack_idx) != 0) // see tnewtag { ! lua_pushliteral(L, "tag"); // get t["tag"] lua_rawget(L, -2); ! if (lua_isnumber(L, -1)) // FIXME this is really an error if this isn't true { tag = (int)lua_tonumber(L, -1); } ! lua_pop(L, 2); // pop metatable and tag number } return tag; } *************** *** 489,555 **** } ! bool LUACALL wxlua_isstringtype(lua_State* L, int stack_idx) { ! int ltype = lua_type(L, stack_idx); ! switch (ltype) { ! //case LUA_TNIL: too easy to have a variable typo, use (str or "") ! case LUA_TSTRING: ! case LUA_TNUMBER: // can convert easily, always works ! return true; ! default: break; ! } ! ! return false; ! } ! bool LUACALL wxlua_isbooleantype(lua_State* L, int stack_idx) ! { ! int ltype = lua_type(L, stack_idx); ! ! switch (ltype) ! { ! case LUA_TNIL: // evaluates to false ! case LUA_TNUMBER: // as in C 0 == false ! case LUA_TBOOLEAN: ! return true; ! default: break; ! } ! ! return false; ! } ! bool LUACALL wxlua_isenumerationtype(lua_State* L, int stack_idx) ! { ! int ltype = lua_type(L, stack_idx); ! ! switch (ltype) ! { ! //case LUA_TNIL: // evaluates to 0 so wx.ENUM_typo = 0 ! case LUA_TNUMBER: // make sure it's only a number for an enum ! return true; ! default: break; } ! return false; } - bool LUACALL wxlua_isnumbertype(lua_State* L, int stack_idx) - { - int ltype = lua_type(L, stack_idx); ! switch (ltype) { ! //case LUA_TNIL: evaluates to 0, too easy to have a typo ! //case LUA_TSTRING: will be 0 unless really a number "2" ! case LUA_TNUMBER: ! case LUA_TBOOLEAN: // can't do (bool_val or 1) ! return true; ! default: ! break; ! } ! return false; } --- 484,557 ---- } ! int LUACALL wxlua_iswxluatype(int luatype, int wxluaarg_type) { ! int ret = -1; // unknown wxlua arg type ! switch (wxluaarg_type) { ! case WXLUAARG_None : ! ret = (luatype == LUA_TNONE) ? 1 : 0; break; ! case WXLUAARG_Nil : ! ret = (luatype == LUA_TNIL) ? 1 : 0; break; ! case WXLUAARG_Boolean : ! // LUA_TNIL: nil == false ! // LUA_TNUMBER: 0 == false as in C ! ret = ((luatype == LUA_TBOOLEAN) || (luatype == LUA_TNUMBER) || (luatype == LUA_TNIL)) ? 1 : 0; ! break; ! case WXLUAARG_LightUserData : ! ret = (luatype == LUA_TLIGHTUSERDATA) ? 1 : 0; ! break; ! case WXLUAARG_Number : ! // LUA_TNIL: evaluates to 0, too easy to have a typo ! // LUA_TSTRING: will be 0 unless really a number "2" ! // LUA_TBOOLEAN: can't do (bool_val or 1) ! ret = ((luatype == LUA_TNUMBER) || (luatype == LUA_TBOOLEAN)) ? 1 : 0; ! break; ! case WXLUAARG_String : ! // LUA_TNIL: too easy to have a variable typo, use (str or "") ! // LUA_TNUMBER: can convert easily, always works, but breaks overload bindings ! ret = (luatype == LUA_TSTRING) ? 1 : 0; ! break; ! case WXLUAARG_LuaTable : ! ret = (luatype == LUA_TTABLE) ? 1 : 0; ! break; ! case WXLUAARG_LuaFunction : ! ret = (luatype == LUA_TFUNCTION) ? 1 : 0; ! break; ! case WXLUAARG_UserData : ! ret = (luatype == LUA_TUSERDATA) ? 1 : 0; ! break; ! case WXLUAARG_LuaThread : ! ret = (luatype == LUA_TTHREAD) ? 1 : 0; ! break; ! case WXLUAARG_Enum : ! // LUA_TNIL: evaluates to 0 so wx.ENUM_typo = 0 ! ret = (luatype == LUA_TNUMBER) ? 1 : 0; break; } ! return ret; } ! wxString wxlua_getwxluatypename(int wxluaarg_type) ! { ! switch (wxluaarg_type) { ! case WXLUAARG_None : return wxT("none"); ! case WXLUAARG_Nil : return wxT("nil"); ! case WXLUAARG_Boolean : return wxT("boolean"); ! case WXLUAARG_LightUserData : return wxT("lightuserdata"); ! case WXLUAARG_Number : return wxT("number"); ! case WXLUAARG_String : return wxT("string"); ! case WXLUAARG_LuaTable : return wxT("table"); ! case WXLUAARG_LuaFunction : return wxT("function"); ! case WXLUAARG_UserData : return wxT("userdata"); ! case WXLUAARG_LuaThread : return wxT("thread"); ! case WXLUAARG_Enum : return wxT("enum"); ! } ! return wxT("unknown"); } *************** *** 593,597 **** } ! return (long)lua_tonumber(L, stack_idx); } double LUACALL wxlua_getnumbertype(lua_State *L, int stack_idx) --- 595,608 ---- } ! double value = lua_tonumber(L, stack_idx); ! ! if (value != (long)value) ! { ! wxString msg(wxString::Format(_("wxLua: Expected an enumeration for parameter %d, but got a floating point number '%lf'."), ! stack_idx, value)); ! wxlua_terror(L, wx2lua(msg)); ! } ! ! return (long)value; } double LUACALL wxlua_getnumbertype(lua_State *L, int stack_idx) *************** *** 1878,1892 **** } - const WXLUACLASS* wxLuaState::GetBaseLuaClass(int iClassTag) const - { - wxCHECK_MSG(GetRefData() != NULL, NULL, wxT("Invalid wxLuaState")); - - const WXLUACLASS *pLuaClass = GetLuaClass(iClassTag); - if (pLuaClass) - return pLuaClass->baseclass; - - return NULL; - } - bool wxLuaState::IsDerivedClass(int iClassTag, int iBaseClassTag) const { --- 1889,1892 ---- *************** *** 3269,3278 **** ! wxString wxLuaState::CreateMethodArgTagsMsg(struct WXLUAMETHOD* wxlMethod) { ! wxCHECK_MSG(wxlMethod, wxEmptyString, wxT("Invalid method table")); - wxString fnOverloadList; WXLUAMETHOD* method = wxlMethod; while (method) --- 3269,3279 ---- ! wxArrayString wxLuaState::CreateMethodArgTagsMsg(struct WXLUAMETHOD* wxlMethod) { ! wxArrayString overloadMethodArray; ! wxCHECK_MSG(wxlMethod, overloadMethodArray, wxT("Invalid method table")); WXLUAMETHOD* method = wxlMethod; + int i_func = 0; while (method) *************** *** 3280,3360 **** WXLUAMETHOD_CFUNC* funcs = method->funcs; int i, arg, funcs_count = wxlMethod->funcs_n; ! int i_func = 0; for (i = 0; i < funcs_count; i++) { - // skip over the overload function - if (WXLUA_HASBIT(funcs[i].type, WXLUAMETHOD_OVERLOAD)) - continue; - i_func++; // only count the non overload functions ! wxString fnOverload = wxString::Format(wxT("%02d. %s("), i_func, lua2wx(method->name).c_str()); ! for (arg = 0; arg < funcs[i].maxargs; arg++) { ! // optional args? ! if ((funcs[i].minargs < funcs[i].maxargs) && (arg == funcs[i].minargs)) { ! fnOverload += wxT("["); ! } ! if (arg > 0) ! fnOverload += wxT(", "); ! int tag = (int)*(funcs[i].argtags[arg]); ! if (tag == s_wxluaarg_String) ! { ! fnOverload += wxT("string"); ! } ! else if (tag == s_wxluaarg_Boolean) ! { ! fnOverload += wxT("boolean"); ! } ! else if (tag == s_wxluaarg_Enumeration) ! { ! fnOverload += wxT("enum"); ! } ! else if (tag == s_wxluaarg_Number) ! { ! fnOverload += wxT("number"); ! } ! else if (tag == s_wxluaarg_LightUserData) ! { ! fnOverload += wxT("lightuserdata"); ! } ! else if (tag == s_wxluaarg_UserData) ! { ! fnOverload += wxT("userdata"); ! } ! else if (tag == s_wxluaarg_LuaTable) ! { ! fnOverload += wxT("luatable"); ! } ! else if (tag == s_wxluaarg_LuaFunction) ! { ! fnOverload += wxT("luafunction"); ! } ! else ! { ! fnOverload += GetLuaTagName(tag); ! if ((arg == 0) && !WXLUA_HASBIT(funcs[i].type, WXLUAMETHOD_STATIC)) ! fnOverload += wxT("(self)"); } - } ! // close optional args ! if (funcs[i].minargs < funcs[i].maxargs) ! { ! fnOverload += wxT("]"); ! } ! fnOverload += wxT(")"); ! if (WXLUA_HASBIT(funcs[i].type, WXLUAMETHOD_STATIC)) ! fnOverload += wxT(" - static"); ! fnOverloadList += fnOverload + wxT("\n"); } --- 3281,3342 ---- WXLUAMETHOD_CFUNC* funcs = method->funcs; int i, arg, funcs_count = wxlMethod->funcs_n; ! wxString className; ! ! const WXLUACLASS* wxlClass = GetLuaClass(method); ! if (wxlClass) ! className = lua2wx(wxlClass->name) + wxT("::"); for (i = 0; i < funcs_count; i++) { i_func++; // only count the non overload functions ! wxString fnOverload = wxString::Format(wxT("%02d. %s%s("), i_func, className.c_str(), lua2wx(method->name).c_str()); ! // overloaded function has invalid tags ! if (WXLUA_HASBIT(funcs[i].type, WXLUAMETHOD_OVERLOAD)) { ! // we do print that there is an overload so that in CallOverloadedFunction ! // we can find what function we were closest too. ! fnOverload += wxT(" ... ) - overloaded function"); ! } ! else ! { ! for (arg = 0; arg < funcs[i].maxargs; arg++) { ! // optional args? ! if ((funcs[i].minargs < funcs[i].maxargs) && (arg == funcs[i].minargs)) ! fnOverload += wxT("["); ! if (arg > 0) ! fnOverload += wxT(", "); ! int tag = (int)*(funcs[i].argtags[arg]); ! if (tag < 0) ! { ! fnOverload += wxlua_getwxluatypename(tag); ! } ! else ! { ! fnOverload += GetLuaTagName(tag); ! if ((arg == 0) && ! !WXLUA_HASBIT(funcs[i].type, WXLUAMETHOD_STATIC) && ! !WXLUA_HASBIT(funcs[i].type, WXLUAMETHOD_CONSTRUCTOR) && ! !WXLUA_HASBIT(funcs[i].type, WXLUAMETHOD_CFUNCTION)) ! fnOverload += wxT("(self)"); ! } } ! // close optional args ! if (funcs[i].minargs < funcs[i].maxargs) ! fnOverload += wxT("]"); ! fnOverload += wxT(")"); ! if (WXLUA_HASBIT(funcs[i].type, WXLUAMETHOD_STATIC)) ! fnOverload += wxT(" - static"); ! } ! overloadMethodArray.Add(fnOverload); } *************** *** 3362,3366 **** } ! return fnOverloadList; } --- 3344,3348 ---- } ! return overloadMethodArray; } *************** *** 3468,3472 **** { // this one won't work, try the next ! funcArray.RemoveAt(i); i--; continue; } --- 3450,3455 ---- { // this one won't work, try the next ! funcArray.RemoveAt(i); ! i--; continue; } *************** *** 3477,3548 **** //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()); ! if (tag == s_wxluaarg_String) ! { ! if ((ltype != LUA_TNIL) && (ltype != LUA_TSTRING)) ! { ! funcArray.RemoveAt(i); i--; ! continue; ! } ! } ! else if (tag == s_wxluaarg_Boolean) ! { ! if ((ltype != LUA_TNIL) && (ltype != LUA_TBOOLEAN)) ! { ! funcArray.RemoveAt(i); i--; ! continue; ! } ! } ! else if (tag == s_wxluaarg_Enumeration) ! { ! if (ltype != LUA_TNUMBER) ! { ! funcArray.RemoveAt(i); i--; ! continue; ! } ! } ! else if (tag == s_wxluaarg_Number) ! { ! if ((ltype != LUA_TNIL) && (ltype != LUA_TNUMBER)) ! { ! funcArray.RemoveAt(i); i--; ! continue; ! } ! } ! else if (tag == s_wxluaarg_LightUserData) ! { ! if (!lua_IsLightUserData(arg_lua)) ! { ! funcArray.RemoveAt(i); i--; ! continue; ! } ! } ! else if (tag == s_wxluaarg_UserData) ! { ! if (!lua_IsUserData(arg_lua)) ! { ! funcArray.RemoveAt(i); i--; ! continue; ! } ! } ! else if (tag == s_wxluaarg_LuaTable) ! { ! if (!lua_IsTable(arg_lua)) ! { ! funcArray.RemoveAt(i); i--; ! continue; ! } ! } ! else if (tag == s_wxluaarg_LuaFunction) { ! if (!lua_IsFunction(arg_lua)) ! { ! funcArray.RemoveAt(i); i--; ! continue; ! } } ! else if (!IsUserDataType(arg_lua, tag) && ! (tag != M_WXLSTATEDATA->m_wxlStateData->m_wxluatag_NULL)) // FIXME! { ! funcArray.RemoveAt(i); i--; continue; } --- 3460,3477 ---- //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 ! int is_ok = wxlua_iswxluatype(ltype, tag); ! // unknown standard wxlua arg type, check binding tag ! if (is_ok == -1) { ! is_ok = (IsUserDataType(arg_lua, tag) || ! (tag == M_WXLSTATEDATA->m_wxlStateData->m_wxluatag_NULL)) ? 1 : 0; } ! ! // this arg is not a match, remove this function as a possibility ! if (is_ok == 0) { ! funcArray.RemoveAt(i); ! i--; continue; } *************** *** 3550,3554 **** } ! if (funcArray.GetCount() != 0) { lua_CFunction func = ((WXLUAMETHOD_CFUNC*)funcArray[0])->func; --- 3479,3484 ---- } ! // If there was a function that matched run it, if > 1 ??? ! if (funcArray.GetCount() == 1) { lua_CFunction func = ((WXLUAMETHOD_CFUNC*)funcArray[0])->func; *************** *** 3618,3634 **** wxString fnOverloadList = wxT("wxLua Function Overload Table:\n"); ! fnOverloadList += CreateMethodArgTagsMsg(wxlMethod); wxString errmsg; ! if (1) //bestFunc == NULL) FIXME errmsg = wxString::Format(wxT("wxLua overloaded function %s has invalid argument\n%s"), fnCall.c_str(), fnOverloadList.c_str()); else { method = wxlMethod; while (method) { for (i = 0; i < method->funcs_n; ++i) { if (&method->funcs[i] == bestFunc) break; --- 3548,3574 ---- wxString fnOverloadList = wxT("wxLua Function Overload Table:\n"); ! wxArrayString overloadMethodArray = CreateMethodArgTagsMsg(wxlMethod); ! for (i = 0; i < (int)overloadMethodArray.GetCount(); i++) ! fnOverloadList += overloadMethodArray[i] + wxT("\n"); ! wxString errmsg; ! if (funcArray.GetCount() > 1) ! { ! errmsg = wxT("wxLua Overloaded function call is ambiguous.\nTry coercing values to proper types using tostring/number as appropriate.\n"); ! } ! ! if (bestFunc == NULL) errmsg = wxString::Format(wxT("wxLua overloaded function %s has invalid argument\n%s"), fnCall.c_str(), fnOverloadList.c_str()); else { method = wxlMethod; + int i_func = 0; while (method) { for (i = 0; i < method->funcs_n; ++i) { + i_func++; if (&method->funcs[i] == bestFunc) break; *************** *** 3638,3642 **** } ! errmsg = wxString::Format(wxT("wxLua overloaded function %s has invalid argument %d on method %02d\n%s"), fnCall.c_str(), (invalidArg), (i+1), fnOverloadList.c_str()); } --- 3578,3582 ---- } ! errmsg = wxString::Format(wxT("wxLua overloaded function %s has invalid argument %d on method %02d\n%s"), fnCall.c_str(), (invalidArg), i_func, fnOverloadList.c_str()); } Index: wxlbind.cpp =================================================================== RCS file: /cvsroot/wxlua/wxLua/modules/wxlua/src/wxlbind.cpp,v retrieving revision 1.62 retrieving revision 1.63 diff -C2 -d -r1.62 -r1.63 *** wxlbind.cpp 5 Jun 2007 21:07:25 -0000 1.62 --- wxlbind.cpp 6 Jun 2007 03:53:40 -0000 1.63 *************** *** 35,47 **** WXLUADEFINE s_wxluadefineArray_None[1] = {{0, 0}}; ! int s_wxluaarg_None = 0; ! int s_wxluaarg_String = -2; ! int s_wxluaarg_Boolean = -3; ! int s_wxluaarg_Enumeration = -4; ! int s_wxluaarg_Number = -5; ! int s_wxluaarg_LightUserData = -6; // raw data ! int s_wxluaarg_UserData = -7; // raw data ! int s_wxluaarg_LuaTable = -8; ! int s_wxluaarg_LuaFunction = -9; //----------------------------------------------------------------------------- --- 35,49 ---- WXLUADEFINE s_wxluadefineArray_None[1] = {{0, 0}}; ! int s_wxluaarg_None = WXLUAARG_None; ! int s_wxluaarg_Nil = WXLUAARG_Nil; ! int s_wxluaarg_Boolean = WXLUAARG_Boolean; ! int s_wxluaarg_LightUserData = WXLUAARG_LightUserData; // raw data ! int s_wxluaarg_Number = WXLUAARG_Number; ! int s_wxluaarg_String = WXLUAARG_String; ! int s_wxluaarg_LuaTable = WXLUAARG_LuaTable; ! int s_wxluaarg_LuaFunction = WXLUAARG_LuaFunction; ! int s_wxluaarg_UserData = WXLUAARG_UserData; // raw data ! int s_wxluaarg_LuaThread = WXLUAARG_LuaThread; ! int s_wxluaarg_Enum = WXLUAARG_Enum; //----------------------------------------------------------------------------- *************** *** 985,991 **** for (i_method = 0; i_method < method_count; ++i_method) { ! for (int i = 0; i < wxlClass[i_method].methods_n; ++i) { ! if (&wxlClass[i_method].methods[i] == wxlMethod) return wxlClass; } --- 987,993 ---- for (i_method = 0; i_method < method_count; ++i_method) { ! for (int i = 0; i < wxlClass->methods_n; ++i) { ! if (&wxlClass->methods[i] == wxlMethod) return wxlClass; } *************** *** 1006,1014 **** for (i_method = 0; i_method < method_count; ++i_method) { ! for (int i = 0; i < wxlClass[i_method].methods_n; ++i) { ! for (int j = 0; j < wxlClass[i_method].methods[i].funcs_n; ++j) { ! if (&wxlClass[i_method].methods[i].funcs[j] == wxlMethod_cfunc) return wxlClass; } --- 1008,1016 ---- for (i_method = 0; i_method < method_count; ++i_method) { ! for (int i = 0; i < wxlClass->methods_n; ++i) { ! for (int j = 0; j < wxlClass->methods[i].funcs_n; ++j) { ! if (&wxlClass->methods[i].funcs[j] == wxlMethod_cfunc) return wxlClass; } |