From: John L. <jr...@us...> - 2008-01-15 01:04:09
|
Update of /cvsroot/wxlua/wxLua/modules/wxlua/src In directory sc8-pr-cvs9.sourceforge.net:/tmp/cvs-serv3556/wxLua/modules/wxlua/src Modified Files: wxlbind.cpp wxlcallb.cpp wxlstate.cpp Log Message: * Changed wxLuaState_Type enum for wxLuaState(lua_State*, wxLuaState_Type) Removed wxLUASTATE_USESTATE and you now | together wxLUASTATE_SETSTATE with wxLUASTATE_OPENBINDINGS if you want the bindings opened. Cleans up the creation of the wxLuaState so a precreated lua_State should be able to be used easier. - Remove poorly named wxLuaState::LuaError() and CheckRunError() and replaced them with the C functions wxlua_errorinfo() and wxlua_LUA_ERR_msg() respectively. - Added wxlua_pushargs(wxChar**, int) for a standard way to push args into Lua. - Copy Lua's print() function to print_lua() instead of simply overwriting it in case someone really wants to use it. Index: wxlstate.cpp =================================================================== RCS file: /cvsroot/wxlua/wxLua/modules/wxlua/src/wxlstate.cpp,v retrieving revision 1.161 retrieving revision 1.162 diff -C2 -d -r1.161 -r1.162 *** wxlstate.cpp 8 Jan 2008 00:55:35 -0000 1.161 --- wxlstate.cpp 15 Jan 2008 01:04:04 -0000 1.162 *************** *** 207,210 **** --- 207,282 ---- // ---------------------------------------------------------------------------- + wxString wxlua_LUA_ERR_msg(int LUA_ERRx) + { + switch (LUA_ERRx) + { + case 0 : return wxEmptyString; + case LUA_YIELD : return wxT("Lua: Thread is suspended"); + case LUA_ERRRUN : return wxT("Lua: Error while running chunk"); + case LUA_ERRSYNTAX : return wxT("Lua: Syntax error during pre-compilation"); + case LUA_ERRMEM : return wxT("Lua: Memory allocation error"); + case LUA_ERRERR : return wxT("Lua: Generic error or an error occurred while running the error handler"); + case LUA_ERRFILE : return wxT("Lua: Error occurred while opening file"); + } + + return wxT("Lua: Unknown LUA_ERRx error value"); + } + + bool wxlua_errorinfo(lua_State* L, int status, int top, wxString* errorMsg_, int* line_num_) + { + if (status == 0) + return false; + + int newtop = lua_gettop(L); + + wxString errorMsg = wxlua_LUA_ERR_msg(status); + + switch(status) + { + case LUA_ERRMEM: + case LUA_ERRERR: + { + if (newtop > top) + errorMsg += wxT("\n"); + break; + } + case LUA_ERRRUN: + case LUA_ERRFILE: + case LUA_ERRSYNTAX: + default: + { + if (newtop > top) + errorMsg += wxT("\n") + lua2wx(lua_tostring(L, -1)); + break; + } + } + + errorMsg += wxT("\n"); + + // Why can't I fill a lua_Debug here? Try to get the line number + // by parsing the error message that looks like this, 3 is linenumber + // [string "a = 1("]:3: unexpected symbol near `<eof>' + wxString lineStr = errorMsg; + long line_num = -1; + while(!lineStr.IsEmpty()) + { + // search through the str to find ']:LONG:' pattern + lineStr = lineStr.AfterFirst(wxT(']')); + if ((lineStr.Length() > 0) && (lineStr.GetChar(0) == wxT(':'))) + { + lineStr = lineStr.AfterFirst(wxT(':')); + if (lineStr.IsEmpty() || lineStr.BeforeFirst(wxT(':')).ToLong(&line_num)) + break; + } + } + + lua_settop(L, top); // pops the message if any + + if (errorMsg_) *errorMsg_ = errorMsg; + if (line_num_) *line_num_ = (int)line_num; + + return false; + } + void LUACALL wxlua_error(lua_State *L, const char *errorMsg) { *************** *** 1574,1577 **** --- 1646,1672 ---- } + int wxlua_pushargs(lua_State* L, wxChar **argv, int argc, int start_n) + { + if (argc == 0) return 0; + + int i = 0; + int narg = argc - (start_n + 1); // number of arguments to the script + luaL_checkstack(L, narg + 3, "too many arguments to script"); + for (i = start_n+1; i < argc; i++) + lua_pushstring(L, wx2lua(argv[i])); + + lua_createtable(L, narg, start_n + 1); + + for (i = 0; i < argc; i++) + { + lua_pushstring(L, wx2lua(argv[i])); + lua_rawseti(L, -2, i - start_n); + } + + lua_setglobal(L, "arg"); + + return narg; + } + //---------------------------------------------------------------------------- // Derived class member functions for classes in wxLua *************** *** 2046,2053 **** wxlua_lreg_createtable(m_lua_State, &wxlua_lreg_refs_key); wxlua_lreg_createtable(m_lua_State, &wxlua_lreg_debug_refs_key); ! ! //lua_pushlightuserdata(m_lua_State, &wxlua_lreg_derivedmethods_key); // gc will delete them ! //lua_pushnil(m_lua_State); ! //lua_rawset(m_lua_State, LUA_REGISTRYINDEX); lua_gc(m_lua_State, LUA_GCCOLLECT, 0); // round up dead refs --- 2141,2145 ---- wxlua_lreg_createtable(m_lua_State, &wxlua_lreg_refs_key); wxlua_lreg_createtable(m_lua_State, &wxlua_lreg_debug_refs_key); ! //wxlua_lreg_createtable(m_lua_State, &wxlua_lreg_derivedmethods_key); // gc will delete them lua_gc(m_lua_State, LUA_GCCOLLECT, 0); // round up dead refs *************** *** 2152,2161 **** luaL_openlibs(L); ! // load the bit lib, this is the accepted way, see luaL_openlibs(L) ! lua_pushcfunction(L, luaopen_bit); ! lua_pushstring(L, "bit"); ! lua_call(L, 1, 0); ! ! bool ok = Create(L, wxLUASTATE_USESTATE); M_WXLSTATEDATA->m_wxlStateData->m_evtHandler = handler; --- 2244,2248 ---- luaL_openlibs(L); ! bool ok = Create(L, wxLUASTATE_SETSTATE|wxLUASTATE_OPENBINDINGS); M_WXLSTATEDATA->m_wxlStateData->m_evtHandler = handler; *************** *** 2179,2183 **** Ref(wxLuaState::GetwxLuaState(L)); } ! else // state_type == wxLUASTATE_USESTATE or wxLUASTATE_SETSTATE { m_refData = new wxLuaStateRefData(); --- 2266,2270 ---- Ref(wxLuaState::GetwxLuaState(L)); } ! else if (WXLUA_HASBIT(state_type, wxLUASTATE_SETSTATE)) { m_refData = new wxLuaStateRefData(); *************** *** 2254,2264 **** wxlua_lreg_createtable(L, &wxlua_lreg_topwindows_key); ! // register print handler to send events RegisterFunction(wxlua_printFunction, "print"); // now register bindings ! if (WXLUA_HASBIT(state_type, wxLUASTATE_USESTATE)) RegisterBindings(); } return Ok(); --- 2341,2368 ---- wxlua_lreg_createtable(L, &wxlua_lreg_topwindows_key); ! // copy Lua's print function in case someone really wants to use it ! lua_pushlstring(L, "print", 5); ! lua_rawget( L, LUA_GLOBALSINDEX ); // pop key, push print function ! lua_pushlstring(L, "print_lua", 9); ! lua_pushvalue(L, -2); // copy print function ! lua_rawset(L, LUA_GLOBALSINDEX); // set t[key] = value, pops key and value ! lua_pop(L, 1); // pop the print function ! ! // register wxLua's print handler to send events, replaces Lua's print function RegisterFunction(wxlua_printFunction, "print"); // now register bindings ! if (WXLUA_HASBIT(state_type, wxLUASTATE_OPENBINDINGS)) ! { ! // load the bit lib, this is the accepted way, see luaL_openlibs(L) ! lua_pushcfunction(L, luaopen_bit); ! lua_pushstring(L, "bit"); ! lua_call(L, 1, 0); ! RegisterBindings(); + } } + else + wxFAIL_MSG(wxT("Unknown state_type for wxLuaState::Create()")); return Ok(); *************** *** 2293,2297 **** bool wxLuaState::IsClosing() const { ! wxCHECK_MSG(Ok(), false, wxT("Invalid wxLuaState")); return M_WXLSTATEDATA->m_wxlStateData->m_is_closing; } --- 2397,2401 ---- bool wxLuaState::IsClosing() const { ! wxCHECK_MSG(m_refData && M_WXLSTATEDATA->m_wxlStateData, false, wxT("Invalid wxLuaState")); return M_WXLSTATEDATA->m_wxlStateData->m_is_closing; } *************** *** 2311,2315 **** } ! wxLuaState wxLuaState::GetwxLuaState(lua_State* L) { // try our hashtable for faster lookup --- 2415,2419 ---- } ! wxLuaState wxLuaState::GetwxLuaState(lua_State* L) // static function { // try our hashtable for faster lookup *************** *** 2361,2370 **** void wxLuaState::SetEventHandler(wxEvtHandler *evtHandler) { ! wxCHECK_RET(Ok(), wxT("Invalid wxLuaState")); M_WXLSTATEDATA->m_wxlStateData->m_evtHandler = evtHandler; } wxEvtHandler *wxLuaState::GetEventHandler() const { ! wxCHECK_MSG(Ok(), NULL, wxT("Invalid wxLuaState")); return M_WXLSTATEDATA->m_wxlStateData->m_evtHandler; } --- 2465,2474 ---- void wxLuaState::SetEventHandler(wxEvtHandler *evtHandler) { ! wxCHECK_RET(m_refData && M_WXLSTATEDATA->m_wxlStateData, wxT("Invalid wxLuaState")); M_WXLSTATEDATA->m_wxlStateData->m_evtHandler = evtHandler; } wxEvtHandler *wxLuaState::GetEventHandler() const { ! wxCHECK_MSG(m_refData && M_WXLSTATEDATA->m_wxlStateData, NULL, wxT("Invalid wxLuaState")); return M_WXLSTATEDATA->m_wxlStateData->m_evtHandler; } *************** *** 2372,2381 **** void wxLuaState::SetId(wxWindowID id) { ! wxCHECK_RET(Ok(), wxT("Invalid wxLuaState")); M_WXLSTATEDATA->m_wxlStateData->m_id = id; } wxWindowID wxLuaState::GetId() const { ! wxCHECK_MSG(Ok(), wxID_ANY, wxT("Invalid wxLuaState")); return M_WXLSTATEDATA->m_wxlStateData->m_id; } --- 2476,2485 ---- void wxLuaState::SetId(wxWindowID id) { ! wxCHECK_RET(m_refData && M_WXLSTATEDATA->m_wxlStateData, wxT("Invalid wxLuaState")); M_WXLSTATEDATA->m_wxlStateData->m_id = id; } wxWindowID wxLuaState::GetId() const { ! wxCHECK_MSG(m_refData && M_WXLSTATEDATA->m_wxlStateData, wxID_ANY, wxT("Invalid wxLuaState")); return M_WXLSTATEDATA->m_wxlStateData->m_id; } *************** *** 2383,2387 **** bool wxLuaState::SendEvent( wxLuaEvent &event ) const { ! wxCHECK_MSG(Ok(), false, wxT("Invalid wxLuaState")); if (M_WXLSTATEDATA->m_wxlStateData->m_evtHandler) --- 2487,2491 ---- bool wxLuaState::SendEvent( wxLuaEvent &event ) const { ! wxCHECK_MSG(m_refData && M_WXLSTATEDATA->m_wxlStateData, false, wxT("Invalid wxLuaState")); if (M_WXLSTATEDATA->m_wxlStateData->m_evtHandler) *************** *** 2407,2417 **** int status = luaL_LoadFile(wx2lua(filename)); if (status == 0) - { status = LuaPCall(0, 0); // no args and no results - } else ! { ! status = LuaError(status, top); // compilation error ! } M_WXLSTATEDATA->m_wxlStateData->m_debugHookData.m_debug_hook_break = false; --- 2511,2519 ---- int status = luaL_LoadFile(wx2lua(filename)); if (status == 0) status = LuaPCall(0, 0); // no args and no results else ! SendLuaErrorEvent(status, top); // compilation error ! ! lua_SetTop(top); // restore original top (removes err msg) M_WXLSTATEDATA->m_wxlStateData->m_debugHookData.m_debug_hook_break = false; *************** *** 2438,2448 **** int status = luaL_LoadBuffer((const char*)buf, size, wx2lua(name)); if (status == 0) - { status = LuaPCall(0, 0); // no args and no results - } else ! { ! status = LuaError(status, top); // compilation error ! } M_WXLSTATEDATA->m_wxlStateData->m_debugHookData.m_debug_hook_break = false; --- 2540,2548 ---- int status = luaL_LoadBuffer((const char*)buf, size, wx2lua(name)); if (status == 0) status = LuaPCall(0, 0); // no args and no results else ! SendLuaErrorEvent(status, top); // compilation error ! ! lua_SetTop(top); // restore original top (removes err msg) M_WXLSTATEDATA->m_wxlStateData->m_debugHookData.m_debug_hook_break = false; *************** *** 2458,2478 **** } - int wxLuaState::CompileString(const wxString &script, const wxString& name, wxString* errMsg_, int* line_num_) - { - wxLuaCharBuffer buf(script); - return CompileBuffer((unsigned char*)buf.GetData(), buf.Length(), name, errMsg_, line_num_); - } - int wxLuaState::CompileBuffer(const unsigned char buf[], size_t size, const wxString &name, wxString* errMsg_, int* line_num_) - { - // create a new lua_State so we don't mess up our own - lua_State *L = lua_open(); - luaL_openlibs(L); // load some useful libraries, loads all of them - int top = lua_gettop(L); - int status = luaL_loadbuffer(L, (const char*)buf, size, wx2lua(name)); - status = LuaError(status, top, L, errMsg_, line_num_); - lua_close(L); - return status; - } - // this function taken from lua.c, the lua executable static int LUACALL wxlua_traceback (lua_State *L) { --- 2558,2561 ---- *************** *** 2480,2484 **** if (!lua_istable(L, -1)) { lua_pop(L, 1); - return 1; } lua_getfield(L, -1, "traceback"); --- 2563,2566 ---- *************** *** 2508,2611 **** lua_remove(L, base); // remove traceback function ! status = LuaError(status, top - (narg + 1)); ! ! return status; ! } ! ! int wxLuaState::LuaError(int status, int top, lua_State* L, wxString* errorMsg_, int* line_num) ! { ! if (L == NULL) ! { ! wxCHECK_MSG(Ok(), status, wxT("Invalid wxLuaState")); ! L = M_WXLSTATEDATA->m_lua_State; ! } ! ! int newtop = lua_gettop(L); ! wxString errorMsg; ! ! if (!CheckRunError(status, &errorMsg)) ! { ! switch(status) ! { ! case LUA_ERRMEM: ! case LUA_ERRERR: ! if (newtop > top) ! errorMsg += wxT("\n"); ! break; ! case LUA_ERRRUN: ! case LUA_ERRFILE: ! case LUA_ERRSYNTAX: ! default: ! if (newtop > top) ! errorMsg += wxT("\n") + lua2wx(lua_tostring(L, -1)); ! break; ! } ! ! errorMsg += wxT("\n"); ! ! // Why can't I fill a lua_Debug here? Try to get the line number ! // by parsing the error message that looks like this, 3 is linenumber ! // [string "a = 1("]:3: unexpected symbol near `<eof>' ! wxString lineStr = errorMsg; ! long line = -1; ! while(!lineStr.IsEmpty()) ! { ! // search through the str to find ']:LONG:' pattern ! lineStr = lineStr.AfterFirst(wxT(']')); ! if ((lineStr.Length() > 0) && (lineStr.GetChar(0) == wxT(':'))) ! { ! lineStr = lineStr.AfterFirst(wxT(':')); ! if (lineStr.IsEmpty() || lineStr.BeforeFirst(wxT(':')).ToLong(&line)) ! break; ! } ! } ! ! wxLuaEvent event(wxEVT_LUA_ERROR, GetId(), *this); ! event.SetString(errorMsg); ! event.SetInt((int)line); ! SendEvent(event); ! lua_settop(L, top); // pops the message if any ! if (errorMsg_) *errorMsg_ = errorMsg; ! if (line_num) *line_num = (int)line; ! } return status; } ! bool wxLuaState::CheckRunError(int status, wxString *msg_) { ! wxString msg; ! ! switch (status) ! { ! case 0 : ! return true; ! case LUA_YIELD: ! msg = wxT("Lua: Thread is suspended"); ! break; ! case LUA_ERRRUN: ! msg = wxT("Lua: Error while running chunk"); ! break; ! case LUA_ERRSYNTAX: ! msg = wxT("Lua: Syntax error during pre-compilation"); ! break; ! case LUA_ERRMEM: ! msg = wxT("Lua: Memory allocation error"); ! break; ! case LUA_ERRERR: ! msg = wxT("Lua: Generic error or an error occurred while running the error handler"); ! break; ! case LUA_ERRFILE: ! msg = wxT("Lua: Error occurred while opening file"); ! break; ! default : ! msg = wxT("Lua: Unknown error"); ! break; ! } ! if (msg_) *msg_ = msg; ! return false; } --- 2590,2613 ---- lua_remove(L, base); // remove traceback function ! if (status != 0) ! SendLuaErrorEvent(status, top - (narg + 1)); ! lua_SetTop(top); // restore original top (removes err msg) return status; } ! bool wxLuaState::SendLuaErrorEvent(int status, int top) { ! wxCHECK_MSG(Ok(), false, wxT("Invalid wxLuaState")); ! wxString errorMsg; ! int line_num = -1; ! wxlua_errorinfo(GetLuaState(), status, top, &errorMsg, &line_num); ! wxLuaEvent event(wxEVT_LUA_ERROR, GetId(), *this); ! event.SetString(errorMsg); ! event.SetInt(line_num); ! return SendEvent(event); } *************** *** 2622,2625 **** --- 2624,2645 ---- } + int wxLuaState::CompileString(const wxString &script, const wxString& name, wxString* errMsg_, int* line_num_) + { + wxLuaCharBuffer buf(script); + return CompileBuffer((unsigned char*)buf.GetData(), buf.Length(), name, errMsg_, line_num_); + } + int wxLuaState::CompileBuffer(const unsigned char buf[], size_t size, const wxString &name, wxString* errMsg_, int* line_num_) + { + // create a new lua_State so we don't mess up our own + lua_State *L = lua_open(); + luaL_openlibs(L); // load some useful libraries, loads all of them + int top = lua_gettop(L); + int status = luaL_loadbuffer(L, (const char*)buf, size, wx2lua(name)); + status = wxlua_errorinfo(L, status, top, errMsg_, line_num_); + lua_close(L); + return 1; + return status; + } + void wxLuaState::DebugHookBreak(const wxString &msg) { Index: wxlbind.cpp =================================================================== RCS file: /cvsroot/wxlua/wxLua/modules/wxlua/src/wxlbind.cpp,v retrieving revision 1.114 retrieving revision 1.115 diff -C2 -d -r1.114 -r1.115 *** wxlbind.cpp 8 Jan 2008 16:11:15 -0000 1.114 --- wxlbind.cpp 15 Jan 2008 01:04:04 -0000 1.115 *************** *** 1140,1144 **** lua_pushstring(L, wxlMethod->name); lua_pushlightuserdata(L, wxlMethod); ! lua_pushcclosure(L, wxlMethod->wxluacfuncs[0].lua_cfunc, 1); lua_rawset(L, -3); } --- 1140,1148 ---- lua_pushstring(L, wxlMethod->name); lua_pushlightuserdata(L, wxlMethod); ! if (wxlMethod->wxluacfuncs_n > 1) ! lua_pushcclosure(L, wxlua_callOverloadedFunction, 1); ! else ! lua_pushcclosure(L, wxlMethod->wxluacfuncs[0].lua_cfunc, 1); ! lua_rawset(L, -3); } Index: wxlcallb.cpp =================================================================== RCS file: /cvsroot/wxlua/wxLua/modules/wxlua/src/wxlcallb.cpp,v retrieving revision 1.56 retrieving revision 1.57 diff -C2 -d -r1.56 -r1.57 *** wxlcallb.cpp 8 Jan 2008 06:19:53 -0000 1.56 --- wxlcallb.cpp 15 Jan 2008 01:04:04 -0000 1.57 *************** *** 175,179 **** wxlState.wxlua_Error("wxLua: wxEvtHandler::Connect() in wxLuaEventCallback::OnEvent(), function to call is not refed."); ! wxlState.lua_SetTop(oldTop); // pop function from the stack (if it's there) } --- 175,179 ---- wxlState.wxlua_Error("wxLua: wxEvtHandler::Connect() in wxLuaEventCallback::OnEvent(), function to call is not refed."); ! wxlState.lua_SetTop(oldTop); // pop function and error message from the stack (if they're there) } |