From: John L. <jr...@us...> - 2007-12-19 00:42:15
|
Update of /cvsroot/wxlua/wxLua/modules/wxlua/src In directory sc8-pr-cvs9.sourceforge.net:/tmp/cvs-serv1877/wxLua/modules/wxlua/src Modified Files: wxlbind.cpp wxlstate.cpp Log Message: Add wxLuaState::lua_TowxString and make lua_ToString return const char* Cleanup the overload function code and give better messages. Set the wxLuaBindMethod struct as an upvalue for C function calls TODO: give better error messages using it. Index: wxlstate.cpp =================================================================== RCS file: /cvsroot/wxlua/wxLua/modules/wxlua/src/wxlstate.cpp,v retrieving revision 1.152 retrieving revision 1.153 diff -C2 -d -r1.152 -r1.153 *** wxlstate.cpp 15 Dec 2007 16:56:41 -0000 1.152 --- wxlstate.cpp 19 Dec 2007 00:42:11 -0000 1.153 *************** *** 2785,2797 **** // memory tracking functions ! void wxLuaState::AddGCObject(wxObject *pObject) { ! AddGCObject(pObject, pObject); } ! void wxLuaState::AddGCObject(const void* obj_ptr, wxObject *pObject) { ! wxCHECK_RET(Ok() && pObject, wxT("Invalid wxLuaState or wxObject to track")); ! wxluaO_addgcobject(M_WXLSTATEDATA->m_lua_State, obj_ptr, pObject); } --- 2785,2797 ---- // memory tracking functions ! void wxLuaState::AddGCObject(wxObject *wxobj) { ! AddGCObject(wxobj, wxobj); } ! void wxLuaState::AddGCObject(const void* obj_ptr, wxObject *wxobj) { ! wxCHECK_RET(Ok() && wxobj, wxT("Invalid wxLuaState or wxObject to track")); ! wxluaO_addgcobject(M_WXLSTATEDATA->m_lua_State, obj_ptr, wxobj); } *************** *** 3183,3187 **** } ! wxLuaState wxLuaState::GetDerivedMethodState(void *obj_ptr, const char *method) { wxCHECK_MSG(obj_ptr, wxNullLuaState, wxT("Invalid object to wxLuaState::GetDerivedMethod")); --- 3183,3187 ---- } ! wxLuaState wxLuaState::GetDerivedMethodState(void *obj_ptr, const char *method_name) { wxCHECK_MSG(obj_ptr, wxNullLuaState, wxT("Invalid object to wxLuaState::GetDerivedMethod")); *************** *** 3192,3196 **** { wxLuaState wxlState((wxLuaState*)it->second); ! if (wxlState.HasDerivedMethod(obj_ptr, method, false)) return wxlState; } --- 3192,3196 ---- { wxLuaState wxlState((wxLuaState*)it->second); ! if (wxlState.HasDerivedMethod(obj_ptr, method_name, false)) return wxlState; } *************** *** 3313,3317 **** return lua_toboolean(M_WXLSTATEDATA->m_lua_State, index); } ! wxString wxLuaState::lua_ToString(int index) const { wxCHECK_MSG(Ok(), wxEmptyString, wxT("Invalid wxLuaState")); --- 3313,3322 ---- return lua_toboolean(M_WXLSTATEDATA->m_lua_State, index); } ! const char* wxLuaState::lua_ToString(int index) const ! { ! wxCHECK_MSG(Ok(), NULL, wxT("Invalid wxLuaState")); ! return lua_tostring(M_WXLSTATEDATA->m_lua_State, index); ! } ! wxString wxLuaState::lua_TowxString(int index) const { wxCHECK_MSG(Ok(), wxEmptyString, wxT("Invalid wxLuaState")); *************** *** 3869,3873 **** wxCHECK_MSG(Ok(), wxEmptyString, wxT("Invalid wxLuaState")); lua_GetGlobal(LUA_PATH); ! wxString path = lua_ToString(-1); lua_Pop(1); --- 3874,3878 ---- wxCHECK_MSG(Ok(), wxEmptyString, wxT("Invalid wxLuaState")); lua_GetGlobal(LUA_PATH); ! wxString path = lua_TowxString(-1); lua_Pop(1); Index: wxlbind.cpp =================================================================== RCS file: /cvsroot/wxlua/wxLua/modules/wxlua/src/wxlbind.cpp,v retrieving revision 1.106 retrieving revision 1.107 diff -C2 -d -r1.106 -r1.107 *** wxlbind.cpp 18 Dec 2007 05:56:15 -0000 1.106 --- wxlbind.cpp 19 Dec 2007 00:42:11 -0000 1.107 *************** *** 101,104 **** --- 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) { *************** *** 106,117 **** // 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++; } --- 108,119 ---- // get number of arguments called from Lua ! int arg_lua_count = lua_gettop(L); ! int arg_lua_start = 1; // don't remove the table, but do skip past it when counting args if (wxluabind_removetableforcall(L, true)) { ! arg_lua_count--; ! arg_lua_start++; } *************** *** 121,131 **** while (method) { ! for (i = 0; i < method->funcs_n; ++i) { ! 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]); } --- 123,135 ---- while (method) { ! wxLuaBindCFunc* bindFunc = method->funcs; ! ! for (i = 0; i < method->funcs_n; ++i, ++bindFunc) { ! if (!WXLUA_HASBIT(bindFunc->type, WXLUAMETHOD_OVERLOAD) && ! (arg_lua_count >= bindFunc->minargs) && ! (arg_lua_count <= bindFunc->maxargs)) { ! funcArray.Add(bindFunc); } *************** *** 136,156 **** 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]; ! bestFunc = func; ! invalidArg = arg; // does this method have any more arguments? ! if (!func->argtags[arg]) { funcArray.RemoveAt(i); --- 140,160 ---- wxLuaBindCFunc* bestFunc = NULL; // stores the last function that worked. ! int invalid_lua_arg = 1; // arg that failed int func_count = funcArray.GetCount(); // Look at the available functions in parallel, per arg ! for (arg = 0; (arg < arg_lua_count) && (func_count != 0); ++arg) { ! int arg_lua = arg+arg_lua_start; // arg N on the Lua stack int ltype = lua_type(L, arg_lua); for (i = 0; i < func_count; ++i) { ! wxLuaBindCFunc* bindFunc = (wxLuaBindCFunc*)funcArray[i]; ! bestFunc = bindFunc; ! invalid_lua_arg = arg_lua+1-arg_lua_start; // does this method have any more arguments? ! if (!bindFunc->argtags[arg]) { funcArray.RemoveAt(i); *************** *** 161,165 **** // 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()); --- 165,169 ---- // get argument tag id ! int tag = (int)*(bindFunc->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()); *************** *** 189,193 **** // derived functions from any baseclasses and should be the best choice. // Example is wxBookCtrlBaseEvent::GetSelection() and wxCommandEvent::GetSelection() ! if (funcArray.GetCount() > 0) { lua_CFunction func = ((wxLuaBindCFunc*)funcArray[0])->func; --- 193,197 ---- // derived functions from any baseclasses and should be the best choice. // Example is wxBookCtrlBaseEvent::GetSelection() and wxCommandEvent::GetSelection() ! if (func_count > 0) { lua_CFunction func = ((wxLuaBindCFunc*)funcArray[0])->func; *************** *** 197,225 **** } ! lua_Debug ar; ! lua_getstack(L, 0, &ar); ! lua_getinfo(L, "n", &ar); ! wxString name = lua2wx(ar.name); ! ! // 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) ! { ! fnCall += lua2wx(lua_typename(L, ltype)); ! } ! else ! { ! int tag = wxluaT_gettag(L, arg+1+arg_start); ! fnCall += wxluaT_gettagname(L, tag); ! } ! } ! fnCall += wxT(")"); wxString fnOverloadList = wxT("wxLua Function Overload Table:\n"); --- 201,208 ---- } ! // ---------------------------------------------------------------------- ! // 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"); *************** *** 230,240 **** 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 { --- 213,223 ---- wxString errmsg; ! if (func_count > 1) // Note: we actually allow this.. for now { ! errmsg = wxT("wxLua: Function call is ambiguous.\nTry coercing values to proper types using tostring/number as appropriate.\n"); } if (bestFunc == NULL) ! errmsg += wxString::Format(wxT("wxLua: Function call '%s' has invalid arguments.\n%s"), fnCall.c_str(), fnOverloadList.c_str()); else { *************** *** 259,263 **** } ! 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()); } --- 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()); } *************** *** 267,270 **** --- 250,284 ---- } + wxString wxlua_CreateArgsMsg(lua_State* L, int start_stack_idx, int end_stack_idx) + { + lua_Debug ar; + lua_getstack(L, 0, &ar); + lua_getinfo(L, "n", &ar); + wxString funcName = lua2wx(ar.name); + + // Build an error message + wxString funcCall = funcName + wxT("("); + + for (int arg = start_stack_idx; arg <= end_stack_idx; ++arg) + { + if (arg > start_stack_idx) funcCall += wxT(", "); + + int ltype = lua_type(L, arg); + + if (ltype != LUA_TUSERDATA) + { + funcCall += lua2wx(lua_typename(L, ltype)); + } + else + { + int tag = wxluaT_gettag(L, arg); + funcCall += wxluaT_gettagname(L, tag); + } + } + funcCall += wxT(")"); + + return funcCall; + } + wxArrayString wxlua_CreateMethodArgTagsMsg(lua_State* L, struct wxLuaBindMethod* wxlMethod) { *************** *** 292,296 **** i_func++; ! wxString fnOverload = wxString::Format(wxT("%02d. %s%s("), i_func, className.c_str(), lua2wx(method->name).c_str()); // overloaded function has invalid tags --- 306,310 ---- i_func++; ! wxString funcStr = wxString::Format(wxT("%02d. %s%s("), i_func, className.c_str(), lua2wx(method->name).c_str()); // overloaded function has invalid tags *************** *** 299,303 **** // However, 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 --- 313,317 ---- // However, we do print that there is an overload so that in CallOverloadedFunction // we can find what function we were closest too. ! funcStr += wxT(" ... ) - overloaded function"); } else *************** *** 307,318 **** // 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]); ! fnOverload += wxluaT_gettagname(L, tag); if ((arg == 0) && --- 321,332 ---- // optional args? if ((funcs[i].minargs < funcs[i].maxargs) && (arg == funcs[i].minargs)) ! funcStr += wxT("["); if (arg > 0) ! funcStr += wxT(", "); int tag = (int)*(funcs[i].argtags[arg]); ! funcStr += wxluaT_gettagname(L, tag); if ((arg == 0) && *************** *** 320,337 **** !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); } --- 334,351 ---- !WXLUA_HASBIT(funcs[i].type, WXLUAMETHOD_CONSTRUCTOR) && !WXLUA_HASBIT(funcs[i].type, WXLUAMETHOD_CFUNCTION)) ! funcStr += wxT("(self)"); } // close optional args if (funcs[i].minargs < funcs[i].maxargs) ! funcStr += wxT("]"); ! funcStr += wxT(")"); if (WXLUA_HASBIT(funcs[i].type, WXLUAMETHOD_STATIC)) ! funcStr += wxT(" - static"); } ! overloadMethodArray.Add(funcStr); } *************** *** 401,405 **** return true; } ! else if (m_wxlState->wxluaR_GetRef(m_reference, &wxlua_lreg_refs_key)) return true; --- 415,419 ---- return true; } ! else if (wxluaR_getref(L, m_reference, &wxlua_lreg_refs_key)) return true; *************** *** 420,428 **** { wxCHECK_MSG(m_wxlState->Ok(), 0, wxT("Invalid wxLuaState")); ! wxCHECK_MSG(m_alloc_flag == wxLUAOBJECT_NONE, 0, wxT("wxLuaObject already initialized")); if ((m_reference != LUA_NOREF) && GetObject()) { ! m_bool = (lua_toboolean(m_wxlState->GetLuaState(), -1) != 0); m_alloc_flag = wxLUAOBJECT_BOOL; m_wxlState->lua_Pop(1); --- 434,442 ---- { wxCHECK_MSG(m_wxlState->Ok(), 0, wxT("Invalid wxLuaState")); ! wxCHECK_MSG(m_alloc_flag == wxLUAOBJECT_NONE, 0, wxT("wxLuaObject already initialized in wxLuaObject::GetBoolPtr")); if ((m_reference != LUA_NOREF) && GetObject()) { ! m_bool = (m_wxlState->lua_ToBoolean(-1) != 0); m_alloc_flag = wxLUAOBJECT_BOOL; m_wxlState->lua_Pop(1); *************** *** 434,442 **** { wxCHECK_MSG(m_wxlState->Ok(), false, wxT("Invalid wxLuaState")); ! wxCHECK_MSG(m_alloc_flag == wxLUAOBJECT_NONE, 0, wxT("wxLuaObject already initialized")); if ((m_reference != LUA_NOREF) && GetObject()) { ! m_int = (int)lua_tonumber(m_wxlState->GetLuaState(), -1); m_alloc_flag = wxLUAOBJECT_INT; m_wxlState->lua_Pop(1); --- 448,456 ---- { wxCHECK_MSG(m_wxlState->Ok(), false, wxT("Invalid wxLuaState")); ! wxCHECK_MSG(m_alloc_flag == wxLUAOBJECT_NONE, 0, wxT("wxLuaObject already initialized in wxLuaObject::GetIntPtr")); if ((m_reference != LUA_NOREF) && GetObject()) { ! m_int = (int)m_wxlState->lua_ToNumber(-1); m_alloc_flag = wxLUAOBJECT_INT; m_wxlState->lua_Pop(1); *************** *** 448,452 **** { wxCHECK_MSG(m_wxlState->Ok(), false, wxT("Invalid wxLuaState")); ! wxCHECK_MSG(m_alloc_flag == wxLUAOBJECT_NONE, 0, wxT("wxLuaObject already initialized")); m_string = new wxString(); // create valid string for return --- 462,466 ---- { wxCHECK_MSG(m_wxlState->Ok(), false, wxT("Invalid wxLuaState")); ! wxCHECK_MSG(m_alloc_flag == wxLUAOBJECT_NONE, 0, wxT("wxLuaObject already initialized in wxLuaObject::GetStringPtr")); m_string = new wxString(); // create valid string for return *************** *** 454,458 **** if ((m_reference != LUA_NOREF) && GetObject()) { ! *m_string = lua2wx(lua_tostring(m_wxlState->GetLuaState(), -1)); m_alloc_flag = wxLUAOBJECT_STRING; m_wxlState->lua_Pop(1); --- 468,472 ---- if ((m_reference != LUA_NOREF) && GetObject()) { ! *m_string = m_wxlState->lua_TowxString(-1); m_alloc_flag = wxLUAOBJECT_STRING; m_wxlState->lua_Pop(1); *************** *** 465,469 **** { wxCHECK_MSG(m_wxlState->Ok(), false, wxT("Invalid wxLuaState")); ! wxCHECK_MSG(m_alloc_flag == wxLUAOBJECT_NONE, 0, wxT("wxLuaObject already initialized")); m_arrayInt = new wxArrayInt(); // create valid array for return --- 479,483 ---- { wxCHECK_MSG(m_wxlState->Ok(), false, wxT("Invalid wxLuaState")); ! wxCHECK_MSG(m_alloc_flag == wxLUAOBJECT_NONE, 0, wxT("wxLuaObject already initialized in wxLuaObject::GetArrayPtr")); m_arrayInt = new wxArrayInt(); // create valid array for return *************** *** 471,475 **** if ((m_reference != LUA_NOREF) && GetObject()) { ! *m_arrayInt = (wxArrayInt&)m_wxlState->GetwxArrayInt(-1); m_alloc_flag = wxLUAOBJECT_ARRAYINT; m_wxlState->lua_Pop(1); --- 485,489 ---- if ((m_reference != LUA_NOREF) && GetObject()) { ! *m_arrayInt = (wxArrayInt&)m_wxlState->GetwxArrayInt(-1); // coerce wxLuaSmartwxArrayInt m_alloc_flag = wxLUAOBJECT_ARRAYINT; m_wxlState->lua_Pop(1); *************** *** 579,583 **** void* udata = lua_touserdata(L, 1); void* obj_ptr = wxlua_touserdata(L, 1, false); ! // if removed from tracked mem list, reset the tag so that gc() is not called on this object. if ((obj_ptr != NULL) && wxluaO_deletegcobject(L, udata, obj_ptr, WXLUA_DELETE_OBJECT_ALL)) { --- 593,598 ---- void* udata = lua_touserdata(L, 1); void* obj_ptr = wxlua_touserdata(L, 1, false); ! ! // if removed from tracked mem list, remove the metatable so that __gc is not called on this object. if ((obj_ptr != NULL) && wxluaO_deletegcobject(L, udata, obj_ptr, WXLUA_DELETE_OBJECT_ALL)) { *************** *** 606,610 **** { void* udata = lua_touserdata(L, 1); ! void* obj_ptr = wxlua_touserdata(L, 1, true); // clean up the rest of this, this won't error if the key doesn't exist --- 621,625 ---- { void* udata = lua_touserdata(L, 1); ! void* obj_ptr = wxlua_touserdata(L, 1, true); // clear lua userdata's ptr // clean up the rest of this, this won't error if the key doesn't exist *************** *** 635,641 **** wxlua_setcallbaseclassfunction(L, false); ! bool callbase = false; ! bool found = false; ! int result = 0; wxLuaBindClass *wxlClass = (wxLuaBindClass *)lua_touserdata(L, lua_upvalueindex(1)); --- 650,656 ---- wxlua_setcallbaseclassfunction(L, false); ! bool callbase = false; ! bool found = false; ! int result = 0; wxLuaBindClass *wxlClass = (wxLuaBindClass *)lua_touserdata(L, lua_upvalueindex(1)); *************** *** 658,661 **** --- 673,677 ---- // } + void *obj_ptr = wxlua_touserdata(L, 1, false); const char *name = lua_tostring(L, 2); // name of the __index method called in Lua *************** *** 668,674 **** else if ((wxlClass != NULL) && wxlua_iswxuserdata(L, 1) && (wxluaT_gettag(L, 1) == *wxlClass->class_tag)) { - void *pObject = wxlua_touserdata(L, 1, false); - - // check if we're to call the baseclass function or if it's a Lua derived function if (!found) --- 684,687 ---- *************** *** 681,688 **** { // if there's a derived method, push it onto the stack to be run ! if (wxlua_hasderivedmethod(L, pObject, name, true)) { found = true; ! result = 1; // the function for Lua to call } } --- 694,701 ---- { // if there's a derived method, push it onto the stack to be run ! if (wxlua_hasderivedmethod(L, obj_ptr, name, true)) { found = true; ! result = 1; // the function for Lua to call is on the stack } } *************** *** 708,721 **** #if 1 // experimental function calls w/o using the wxLuaFunction if (wxlMethod->basemethod) - { - lua_pushlightuserdata(L, wxlMethod); lua_pushcclosure(L, wxlua_CallOverloadedFunction, 1); - } else ! lua_pushcclosure(L, wxlMethod->funcs[0].func, 0); #else ! wxLuaFunction *wxlFunc = new wxLuaFunction(wxlMethod, pObject); // Don't track the wxLuaFunction for speed wxluaT_pushusertag(L, wxlFunc, g_wxluatag_wxLuaFunction, false); --- 721,733 ---- #if 1 + lua_pushlightuserdata(L, wxlMethod); + // experimental function calls w/o using the wxLuaFunction if (wxlMethod->basemethod) lua_pushcclosure(L, wxlua_CallOverloadedFunction, 1); else ! lua_pushcclosure(L, wxlMethod->funcs[0].func, 1); #else ! wxLuaFunction *wxlFunc = new wxLuaFunction(wxlMethod, obj_ptr); // Don't track the wxLuaFunction for speed wxluaT_pushusertag(L, wxlFunc, g_wxluatag_wxLuaFunction, false); *************** *** 857,861 **** int wxluabind_removetableforcall(lua_State* L, bool only_check) { ! void* p = (void *)lua_touserdata(L, lua_upvalueindex(1)); // lightuserdata if (!only_check && (p == &wxluabind_checkremovetable)) lua_remove(L, 1); --- 869,873 ---- int wxluabind_removetableforcall(lua_State* L, bool only_check) { ! void* p = (void *)lua_touserdata(L, lua_upvalueindex(2)); // lightuserdata if (!only_check && (p == &wxluabind_checkremovetable)) lua_remove(L, 1); *************** *** 936,950 **** bool wxLuaBinding::sm_bindingList_initialized = false; ! wxLuaBinding::wxLuaBinding() : wxObject(), ! m_classCount(0), m_classArray(NULL), ! m_defineCount(0), m_defineArray(NULL), ! m_stringCount(0), m_stringArray(NULL), ! m_eventCount(0), m_eventArray(NULL), ! m_objectCount(0), m_objectArray(NULL), ! m_functionCount(0), m_functionArray(NULL), ! m_bindings_registered(false), ! m_start_tag(0), ! m_last_tag(0), ! m_luaTable_ref(-1) { } --- 948,962 ---- bool wxLuaBinding::sm_bindingList_initialized = false; ! wxLuaBinding::wxLuaBinding() ! :m_classCount(0), m_classArray(NULL), ! m_defineCount(0), m_defineArray(NULL), ! m_stringCount(0), m_stringArray(NULL), ! m_eventCount(0), m_eventArray(NULL), ! m_objectCount(0), m_objectArray(NULL), ! m_functionCount(0), m_functionArray(NULL), ! m_bindings_registered(false), ! m_start_tag(0), ! m_last_tag(0), ! m_luaTable_ref(-1) { } *************** *** 984,992 **** } ! bool wxLuaBinding::RegisterBinding(const wxLuaState& wxlState_) { ! wxCHECK_MSG(wxlState_.Ok(), false, wxT("Invalid wxLuaState")); ! wxLuaState wxlState(wxlState_); // unconst the state ! lua_State *L = wxlState.GetLuaState(); --- 996,1002 ---- } ! bool wxLuaBinding::RegisterBinding(const wxLuaState& wxlState) { ! wxCHECK_MSG(wxlState.Ok(), false, wxT("Invalid wxLuaState")); lua_State *L = wxlState.GetLuaState(); *************** *** 996,1000 **** s.Printf(wxT("wxLua: Bindings '%s' in Lua namespace '%s' are already registered."), m_bindingName.c_str(), m_nameSpace.c_str()); ! wxlState.wxlua_Error(wx2lua(s)); return false; } --- 1006,1010 ---- s.Printf(wxT("wxLua: Bindings '%s' in Lua namespace '%s' are already registered."), m_bindingName.c_str(), m_nameSpace.c_str()); ! wxlua_error(L, s); return false; } *************** *** 1045,1049 **** { // create a ref for the wxLua table we're filling ! m_luaTable_ref = wxlState.wxluaR_Ref(-1, &wxlua_lreg_refs_key); } --- 1055,1059 ---- { // create a ref for the wxLua table we're filling ! m_luaTable_ref = wxluaR_ref(L, -1, &wxlua_lreg_refs_key); } *************** *** 1060,1064 **** } ! void wxLuaBinding::DoRegisterBinding(const wxLuaState& wxlState_, int tableOffset) { // Replace the metatable functions for the classes we push into Lua --- 1070,1074 ---- } ! void wxLuaBinding::DoRegisterBinding(const wxLuaState& wxlState, int tableOffset) { // Replace the metatable functions for the classes we push into Lua *************** *** 1072,1080 **** static const size_t s_funcCount = sizeof(s_funcTable)/sizeof(s_funcTable[0]); ! wxCHECK_RET(wxlState_.Ok(), wxT("Invalid wxLuaState")); ! wxLuaState wxlState(wxlState_); lua_State *L = wxlState.GetLuaState(); ! int iTag = m_start_tag; // install the classes, functions and methods, creating new tags --- 1082,1089 ---- static const size_t s_funcCount = sizeof(s_funcTable)/sizeof(s_funcTable[0]); ! wxCHECK_RET(wxlState.Ok(), wxT("Invalid wxLuaState")); lua_State *L = wxlState.GetLuaState(); ! int wxl_tag = m_start_tag; // install the classes, functions and methods, creating new tags *************** *** 1086,1107 **** // Add to the lookup table for "class name" to wxLuaBindClass struct lua_pushlightuserdata(L, &wxlua_lreg_classes_key); ! lua_rawget(L, LUA_REGISTRYINDEX); // pop key, push result (the classes table) ! lua_pushstring(L, wxlClass->name); ! lua_pushlightuserdata(L, (void *)wxlClass); ! lua_rawset(L, -3); // t[name] = tag ! lua_remove(L, -1); // remove wxLua's registry wxLuaClasses table // ------------------------------------------------------------------ // Create a new metatable for this class with a numerical tag index ! iTag = wxlState.wxluaT_NewTag(); ! *wxlClass->class_tag = iTag; if (i_class == 0) ! m_start_tag = iTag; // store a lookup in the class metatable to the wxLuaBindClass struct ! lua_pushlightuserdata(L, &wxlua_metatable_wxluabindclass_key); ! lua_pushlightuserdata(L, (void *)wxlClass); ! lua_rawset(L, -3); // t[name] = tag // set the functions for the class in the metatable --- 1095,1116 ---- // Add to the lookup table for "class name" to wxLuaBindClass struct lua_pushlightuserdata(L, &wxlua_lreg_classes_key); ! lua_rawget(L, LUA_REGISTRYINDEX); // pop key, push result (the classes table) ! lua_pushstring(L, wxlClass->name); // push key ! lua_pushlightuserdata(L, (void *)wxlClass); // push value ! lua_rawset(L, -3); // set t[key] = value, pops key and value ! lua_pop(L, 1); // pop wxlua_lreg_classes_key table // ------------------------------------------------------------------ // Create a new metatable for this class with a numerical tag index ! wxl_tag = wxluaT_newtag(L); // create metatable, is on top of stack ! *wxlClass->class_tag = wxl_tag; if (i_class == 0) ! m_start_tag = wxl_tag; // store a lookup in the class metatable to the wxLuaBindClass struct ! lua_pushlightuserdata(L, &wxlua_metatable_wxluabindclass_key); // push key ! lua_pushlightuserdata(L, (void *)wxlClass); // push value ! lua_rawset(L, -3); // set t[key] = value, pops key and value // set the functions for the class in the metatable *************** *** 1114,1124 **** } ! lua_remove(L, -1); // remove metatable we got from wxluaR_getref() // ------------------------------------------------------------------ ! // install the table for the class ! lua_pushstring(L, wxlClass->name); ! lua_newtable(L); // Install the member enums for the classname table --- 1123,1133 ---- } ! lua_pop(L, 1); // pop metatable from wxluaT_newtag() // ------------------------------------------------------------------ ! // Create and install the table for the class ! lua_pushstring(L, wxlClass->name); // push key ! lua_newtable(L); // push value, the table we use as the class // Install the member enums for the classname table *************** *** 1139,1148 **** { lua_pushstring(L, wxlMethod->name); ! lua_pushcfunction(L, wxlMethod->funcs[0].func); lua_rawset(L, -3); } } ! lua_rawset(L, -3); // same as lua_settable(L, tableOffset); // ------------------------------------------------------------------ --- 1148,1160 ---- { lua_pushstring(L, wxlMethod->name); ! lua_pushlightuserdata(L, wxlMethod); ! lua_pushcclosure(L, wxlMethod->funcs[0].func, 1); lua_rawset(L, -3); } } ! // Finalize the class table since we may not have a constructor ! // or have multiple constructors. ! lua_rawset(L, -3); // set t[key] = value, pops key and value // ------------------------------------------------------------------ *************** *** 1154,1157 **** --- 1166,1170 ---- { #if 1 // C++ class constructors are tables and use the __call metatable to make them "functions" + // push name of nested table and create the table or use existing // we do it this way since we can have multiple constructors (renamed) *************** *** 1166,1170 **** // add the items to the table as t[first pushed] = second pushed lua_pushlstring(L, "new", 3); ! lua_pushcfunction(L, wxlMethod->funcs[0].func); lua_rawset(L, -3); // same as lua_setfield(L, -2, "key") --- 1179,1184 ---- // add the items to the table as t[first pushed] = second pushed lua_pushlstring(L, "new", 3); ! lua_pushlightuserdata(L, wxlMethod); ! lua_pushcclosure(L, wxlMethod->funcs[0].func, 1); lua_rawset(L, -3); // same as lua_setfield(L, -2, "key") *************** *** 1173,1178 **** lua_pushlstring(L, "__call", 6); lua_pushlightuserdata(L, &wxluabind_checkremovetable); // push tag to recognize table call ! lua_pushcclosure(L, wxlMethod->funcs[0].func, 1); // push func with tag as upvalue lua_rawset(L, -3); --- 1187,1193 ---- lua_pushlstring(L, "__call", 6); + lua_pushlightuserdata(L, wxlMethod); lua_pushlightuserdata(L, &wxluabind_checkremovetable); // push tag to recognize table call ! lua_pushcclosure(L, wxlMethod->funcs[0].func, 2); // push func with tag as upvalue lua_rawset(L, -3); *************** *** 1184,1188 **** // add table to the binding table t[wxlMethod->name] = { this table } ! lua_rawset(L, -3); #elif 0 // C++ constructors are userdata, use metatable for access to items. --- 1199,1203 ---- // add table to the binding table t[wxlMethod->name] = { this table } ! lua_rawset(L, -3); // set t[key] = value, pops key and value #elif 0 // C++ constructors are userdata, use metatable for access to items. *************** *** 1211,1222 **** } ! m_last_tag = iTag; ! // register all the builtin functions, global C style functions wxLuaBindMethod* wxlMethod = m_functionArray; for (size_t i_func = 0; i_func < m_functionCount; ++i_func, ++wxlMethod) { lua_pushstring(L, wxlMethod->name); ! lua_pushcfunction(L, wxlMethod->funcs[0].func); lua_rawset(L, -3); } --- 1226,1238 ---- } ! m_last_tag = wxl_tag; ! // register the global C style functions wxLuaBindMethod* wxlMethod = m_functionArray; for (size_t i_func = 0; i_func < m_functionCount; ++i_func, ++wxlMethod) { lua_pushstring(L, wxlMethod->name); ! lua_pushlightuserdata(L, wxlMethod); ! lua_pushcclosure(L, wxlMethod->funcs[0].func, 1); lua_rawset(L, -3); } *************** *** 1254,1258 **** } ! // register all the wxevent types wxLuaBindEvent *wxlEvent = m_eventArray; for (size_t i_event = 0; i_event < m_eventCount; ++i_event, ++wxlEvent) --- 1270,1274 ---- } ! // register the wxEvent types wxLuaBindEvent *wxlEvent = m_eventArray; for (size_t i_event = 0; i_event < m_eventCount; ++i_event, ++wxlEvent) *************** *** 1286,1290 **** // we've sorted this, see InitBinding() const wxEventType eventType = eventType_; ! wxLuaBindEvent eventItem = { "", &eventType }; const wxLuaBindEvent *pLuaEvent = (wxLuaBindEvent *)bsearch(&eventItem, --- 1302,1306 ---- // we've sorted this, see InitBinding() const wxEventType eventType = eventType_; ! wxLuaBindEvent eventItem = { "", &eventType, NULL }; const wxLuaBindEvent *pLuaEvent = (wxLuaBindEvent *)bsearch(&eventItem, *************** *** 1523,1531 **** wxCHECK_MSG(wxlClass, NULL, wxT("Invalid wxLuaBindClass in wxLuaState::GetClassProperty")); int i_method, method_count = wxlClass->methods_n; // find a method in the class, recurse through classes from which this class is derived. ! for (i_method = 0; i_method < method_count; ++i_method) { - wxLuaBindMethod *wxlMethod = wxlClass->methods + i_method; if (isLuaSetProp) { --- 1539,1547 ---- wxCHECK_MSG(wxlClass, NULL, wxT("Invalid wxLuaBindClass in wxLuaState::GetClassProperty")); + wxLuaBindMethod *wxlMethod = wxlClass->methods; int i_method, method_count = wxlClass->methods_n; // find a method in the class, recurse through classes from which this class is derived. ! for (i_method = 0; i_method < method_count; ++i_method, ++wxlMethod) { if (isLuaSetProp) { |