From: John L. <jr...@us...> - 2008-12-05 21:15:17
|
Update of /cvsroot/wxlua/wxLua/modules/wxlua/src In directory 23jxhf1.ch3.sourceforge.com:/tmp/cvs-serv1652/wxLua/modules/wxlua/src Modified Files: wxlstate.cpp Log Message: Allow convertion of wxStrings to const char in wxlua_getstringtype() Make wxLua app allow for printing to stdout, msgdlg, and/or console as cmd line option Check for and don't crash if someone replaces tostring() with garbage Index: wxlstate.cpp =================================================================== RCS file: /cvsroot/wxlua/wxLua/modules/wxlua/src/wxlstate.cpp,v retrieving revision 1.175 retrieving revision 1.176 diff -C2 -d -r1.175 -r1.176 *** wxlstate.cpp 3 Dec 2008 05:26:03 -0000 1.175 --- wxlstate.cpp 5 Dec 2008 21:15:11 -0000 1.176 *************** *** 64,69 **** int i, n = lua_gettop(L); ! // figure out the types here in c++ land lua_getglobal(L, "tostring"); for (i = 1; i <= n; ++i) { --- 64,78 ---- int i, n = lua_gettop(L); ! // Use the Lua tostring() function to print them as Lua would lua_getglobal(L, "tostring"); + + if (!lua_isfunction(L, -1)) + { + // This code is also used in wxledit.cpp, wxLuaShell::RunString() + msg = wxT("wxLua ERROR: Unable to print() without the tostring() function. Did you remove it?"); + lua_pop(L, 1); // pop the nil or whatever replaced tostring() + n = 0; // don't let for loop run + } + for (i = 1; i <= n; ++i) { *************** *** 71,78 **** const char *s; ! lua_pushvalue(L, -1); /* function to be called */ ! lua_pushvalue(L, i); /* value to print */ lua_call(L, 1, 1); ! s = lua_tostring(L, -1); /* get result */ if (s == NULL) { --- 80,87 ---- const char *s; ! lua_pushvalue(L, -1); /* function to be called */ ! lua_pushvalue(L, i); /* value to print */ lua_call(L, 1, 1); ! s = lua_tostring(L, -1); /* get result */ if (s == NULL) { *************** *** 92,96 **** } else if (!msg.IsEmpty()) ! wxPrintf(wxT("%s\n"), msg.c_str()); return 0; // no items put onto stack --- 101,105 ---- } else if (!msg.IsEmpty()) ! wxPrintf(wxT("%s\n"), msg.c_str()); // Lua puts a \n too return 0; // no items put onto stack *************** *** 1344,1347 **** --- 1353,1359 ---- bool wxlua_iswxstringtype(lua_State* L, int stack_idx) { + // NOTE: If we ever allow numbers to be coerced to strings we must + // change how we handle lua_tostring() calls since it will change a number + // to a string on the stack. This could break people's code. if (wxlua_iswxluatype(lua_type(L, stack_idx), WXLUA_TSTRING) == 1) return true; *************** *** 1357,1364 **** const char* LUACALL wxlua_getstringtype(lua_State *L, int stack_idx) { ! if (!wxlua_isstringtype(L, stack_idx)) ! wxlua_argerror(L, stack_idx, wxT("a 'string'")); ! return lua_tostring(L, stack_idx); } --- 1369,1389 ---- const char* LUACALL wxlua_getstringtype(lua_State *L, int stack_idx) { ! if (wxlua_isstringtype(L, stack_idx)) ! return lua_tostring(L, stack_idx); ! else if (wxlua_iswxuserdata(L, stack_idx)) ! { ! int stack_type = wxluaT_type(L, stack_idx); ! if (wxluaT_isderivedtype(L, stack_type, *p_wxluatype_wxString) >= 0) ! { ! wxString* wxstr = (wxString*)wxlua_touserdata(L, stack_idx, false); ! wxCHECK_MSG(wxstr, NULL, wxT("Invalid userdata wxString")); ! return wx2lua(*wxstr); ! } ! } ! ! wxlua_argerror(L, stack_idx, wxT("a 'string' or 'wxString'")); ! ! return NULL; } |