From: John L. <jr...@us...> - 2007-06-08 01:36:49
|
Update of /cvsroot/wxlua/wxLua/modules/wxlua/src In directory sc8-pr-cvs9.sourceforge.net:/tmp/cvs-serv15480/wxLua/modules/wxlua/src Modified Files: wxlbind.cpp Log Message: Add a simple method to get the binding info, seems small and works well Addd sample lua program bindings.wx.lua to show them in a listctrl Fix incircles to work with new bindings Put the "name" of the struct binding items first always Index: wxlbind.cpp =================================================================== RCS file: /cvsroot/wxlua/wxLua/modules/wxlua/src/wxlbind.cpp,v retrieving revision 1.65 retrieving revision 1.66 diff -C2 -d -r1.65 -r1.66 *** wxlbind.cpp 6 Jun 2007 23:43:16 -0000 1.65 --- wxlbind.cpp 8 Jun 2007 01:36:31 -0000 1.66 *************** *** 25,28 **** --- 25,30 ---- WX_DEFINE_LIST(wxLuaBindingList); + int LUACALL wxluabind_wxLuaBinding_index(lua_State* L); + // Binding tags are generated as positive tag id automatically when bound // so we set the inbuilt lua arg tags to negative values *************** *** 685,688 **** --- 687,708 ---- PostRegister(wxlState, registerTypes, tableOffset); + // Push function to access the binding info + lua_pushstring(L, wx2lua(wxT("wxLuaBinding_") + m_bindingName)); + const void **ptr = (const void **)lua_newuserdata(L, sizeof(void *)); + *ptr = this; + lua_newtable(L); + lua_pushstring(L, "__index"); + lua_pushlightuserdata(L, this); // push tag to recognize table call + lua_pushcclosure(L, wxluabind_wxLuaBinding_index, 1); // push func with tag as upvalue + lua_rawset(L, -3); + + //lua_pushstring(L, "__metatable"); + //lua_pushstring(L, "Metatable is not accessible"); + //lua_rawset(L, -3); + + lua_setmetatable(L, -2); + lua_rawset(L, -3); + + m_bindings_registered = true; *************** *** 712,718 **** // install the classes, functions and methods, creating new tags // if this is the first time we're registering them ! for (size_t i_class = 0; i_class < m_classCount; ++i_class) { - wxLuaBindClass *wxlClass = m_classArray + i_class; // Create a new tag if registering types, else use tag already set --- 732,738 ---- // install the classes, functions and methods, creating new tags // if this is the first time we're registering them ! wxLuaBindClass *wxlClass = m_classArray; ! for (size_t i_class = 0; i_class < m_classCount; ++i_class, ++wxlClass) { // Create a new tag if registering types, else use tag already set *************** *** 774,780 **** // Install public functions like constructors or global functions ! for (i_method = 0; i_method < method_count; ++i_method) { - wxLuaBindMethod *wxlMethod = wxlClass->methods + i_method; if (WXLUA_HASBIT(wxlMethod->type, WXLUAMETHOD_CONSTRUCTOR | WXLUAMETHOD_CFUNCTION)) { --- 794,800 ---- // Install public functions like constructors or global functions ! wxLuaBindMethod *wxlMethod = wxlClass->methods; ! for (i_method = 0; i_method < method_count; ++i_method, ++wxlMethod) { if (WXLUA_HASBIT(wxlMethod->type, WXLUAMETHOD_CONSTRUCTOR | WXLUAMETHOD_CFUNCTION)) { *************** *** 963,967 **** { const int eventType = eventType_; ! wxLuaBindEvent eventItem = { &eventType, "" }; const wxLuaBindEvent *pLuaEvent = (wxLuaBindEvent *)bsearch(&eventItem, --- 983,987 ---- { const int eventType = eventType_; ! wxLuaBindEvent eventItem = { "", &eventType }; const wxLuaBindEvent *pLuaEvent = (wxLuaBindEvent *)bsearch(&eventItem, *************** *** 981,998 **** const wxLuaBindClass* wxLuaBinding::GetLuaClass(const wxLuaBindMethod* wxlMethod) const { ! for (size_t i_class = 0; i_class < m_classCount; ++i_class) { ! wxLuaBindClass *wxlClass = m_classArray + i_class; ! int i_method, method_count = wxlClass->methods_n; ! 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; ! } } - } --- 1001,1017 ---- const wxLuaBindClass* wxLuaBinding::GetLuaClass(const wxLuaBindMethod* wxlMethod) const { ! wxLuaBindClass* wxlClass_i = m_classArray; ! wxLuaBindMethod* wxlMethod_i = NULL; ! ! for (size_t i_class = 0; i_class < m_classCount; ++i_class, ++wxlClass_i) { ! wxlMethod_i = wxlClass_i->methods; ! int i_method, methods_n = wxlClass_i->methods_n; ! for (i_method = 0; i_method < methods_n; ++i_method, ++wxlMethod_i) { ! if (wxlMethod_i == wxlMethod) ! return wxlClass_i; } } *************** *** 1002,1024 **** const wxLuaBindClass* wxLuaBinding::GetLuaClass(const wxLuaBindCFunc* wxlMethod_cfunc) const { ! for (size_t i_class = 0; i_class < m_classCount; ++i_class) { ! wxLuaBindClass *wxlClass = m_classArray + i_class; ! int i_method, method_count = wxlClass->methods_n; ! 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; } } } } ! return NULL; } --- 1021,1541 ---- const wxLuaBindClass* wxLuaBinding::GetLuaClass(const wxLuaBindCFunc* wxlMethod_cfunc) const { ! wxLuaBindClass* wxlClass_i = m_classArray; ! wxLuaBindMethod* wxlMethod_i = NULL; ! wxLuaBindCFunc* wxlCFunc_i = NULL; ! ! for (size_t i_class = 0; i_class < m_classCount; ++i_class, ++wxlClass_i) { ! wxlMethod_i = wxlClass_i->methods; ! int i_method, methods_n = wxlClass_i->methods_n; ! for (i_method = 0; i_method < methods_n; ++i_method, ++wxlMethod_i) { ! wxlCFunc_i = wxlMethod_i->funcs; ! int i_func, funcs_n = wxlMethod_i->funcs_n; ! ! for (i_func = 0; i_func < funcs_n; ++i_func, ++wxlCFunc_i) { ! if (wxlCFunc_i == wxlMethod_cfunc) ! return wxlClass_i; ! } ! } ! } ! ! return NULL; ! } ! ! //----------------------------------------------------------------------------- ! // wxluabind_wxLuaBindCFunc_index ! //----------------------------------------------------------------------------- ! ! int LUACALL wxluabind_wxLuaBindCFunc_index(lua_State* L) ! { ! wxLuaState wxlState(L); ! wxCHECK_MSG(wxlState.Ok(), 0, wxT("Invalid wxLuaState")); ! ! void **ptr = (void **)lua_touserdata(L, 1); ! wxLuaBindCFunc* wxlCFunc= (wxLuaBindCFunc*)*ptr; ! wxLuaBinding *wxlBinding = (wxLuaBinding *)lua_touserdata(L, lua_upvalueindex(1)); ! ! int idx_type = lua_type(L, 2); ! ! if (idx_type == LUA_TSTRING) ! { ! const char* idx_str = lua_tostring(L, 2); ! ! if (strcmp(idx_str, "func") == 0) ! { ! lua_pushcfunction(L, wxlCFunc->func); ! return 1; ! } ! else if (strcmp(idx_str, "type") == 0) ! { ! lua_pushnumber(L, wxlCFunc->type); ! return 1; ! } ! else if (strcmp(idx_str, "minargs") == 0) ! { ! lua_pushnumber(L, wxlCFunc->minargs); ! return 1; ! } ! else if (strcmp(idx_str, "maxargs") == 0) ! { ! lua_pushnumber(L, wxlCFunc->maxargs); ! return 1; ! } ! else if (strcmp(idx_str, "argtags") == 0) ! { ! size_t idx, count = wxlCFunc->maxargs; ! lua_createtable(L, count, 0); ! ! // check for terminating null argtag ! for (idx = 0; (idx < count) && wxlCFunc->argtags[idx]; ++idx) ! { ! lua_pushnumber(L, *wxlCFunc->argtags[idx]); ! lua_rawseti(L, -2, idx + 1); ! } ! ! return 1; ! } ! else if (strcmp(idx_str, "argtags_name") == 0) ! { ! size_t idx, count = wxlCFunc->maxargs; ! lua_createtable(L, count, 0); ! ! // check for terminating null argtag ! for (idx = 0; (idx < count) && wxlCFunc->argtags[idx]; ++idx) ! { ! lua_pushstring(L, wxlState.GetLuaTagName(*wxlCFunc->argtags[idx])); ! lua_rawseti(L, -2, idx + 1); ! } ! ! return 1; ! } ! ! ! } ! ! return 0; ! } ! ! //----------------------------------------------------------------------------- ! // wxluabind_wxLuaBindMethod_index ! //----------------------------------------------------------------------------- ! ! int LUACALL wxluabind_wxLuaBindMethod_index(lua_State* L) ! { ! wxLuaState wxlState(L); ! wxCHECK_MSG(wxlState.Ok(), 0, wxT("Invalid wxLuaState")); ! ! void **ptr = (void **)lua_touserdata(L, 1); ! wxLuaBindMethod* wxlMethod = (wxLuaBindMethod*)*ptr; ! wxLuaBinding *wxlBinding = (wxLuaBinding *)lua_touserdata(L, lua_upvalueindex(1)); ! ! int idx_type = lua_type(L, 2); ! ! if (idx_type == LUA_TSTRING) ! { ! const char* idx_str = lua_tostring(L, 2); ! ! if (strcmp(idx_str, "name") == 0) ! { ! lua_pushstring(L, wxlMethod->name); ! return 1; ! } ! else if (strcmp(idx_str, "type") == 0) ! { ! lua_pushnumber(L, wxlMethod->type); ! return 1; ! } ! else if (strcmp(idx_str, "funcs") == 0) ! { ! wxLuaBindCFunc* wxlCFunc = wxlMethod->funcs; ! size_t idx, count = wxlMethod->funcs_n; ! lua_createtable(L, count, 0); ! ! for (idx = 0; idx < count; ++idx, ++wxlCFunc) ! { ! const void **ptr = (const void **)lua_newuserdata(L, sizeof(void *)); ! *ptr = wxlCFunc; ! lua_newtable(L); ! lua_pushstring(L, "__index"); ! lua_pushlightuserdata(L, wxlBinding); ! lua_pushcclosure(L, wxluabind_wxLuaBindCFunc_index, 1); // push func with tag as upvalue ! lua_rawset(L, -3); ! lua_setmetatable(L, -2); ! ! lua_rawseti(L, -2, idx + 1); ! } ! ! return 1; ! } ! else if (strcmp(idx_str, "funcs_n") == 0) ! { ! lua_pushnumber(L, wxlMethod->funcs_n); ! return 1; ! } ! else if (strcmp(idx_str, "basemethod") == 0) ! { ! if (wxlMethod->basemethod) ! { ! const void **ptr = (const void **)lua_newuserdata(L, sizeof(void *)); ! *ptr = wxlMethod->basemethod; ! lua_newtable(L); ! lua_pushstring(L, "__index"); ! lua_pushlightuserdata(L, wxlBinding); ! lua_pushcclosure(L, wxluabind_wxLuaBindMethod_index, 1); // push func with tag as upvalue ! lua_rawset(L, -3); ! lua_setmetatable(L, -2); ! ! return 1; ! } ! ! return 0; ! } ! } ! ! return 0; ! } ! ! //----------------------------------------------------------------------------- ! // wxluabind_wxLuaBindClass_index ! //----------------------------------------------------------------------------- ! ! int LUACALL wxluabind_wxLuaBindClass_index(lua_State* L) ! { ! wxLuaState wxlState(L); ! wxCHECK_MSG(wxlState.Ok(), 0, wxT("Invalid wxLuaState")); ! ! void **ptr = (void **)lua_touserdata(L, 1); ! wxLuaBindClass* wxlClass = (wxLuaBindClass*)*ptr; ! wxLuaBinding *wxlBinding = (wxLuaBinding *)lua_touserdata(L, lua_upvalueindex(1)); ! ! int idx_type = lua_type(L, 2); ! ! if (idx_type == LUA_TSTRING) ! { ! const char* idx_str = lua_tostring(L, 2); ! ! if (strcmp(idx_str, "name") == 0) ! { ! lua_pushstring(L, wxlClass->name); ! return 1; ! } ! else if (strcmp(idx_str, "methods") == 0) ! { ! if (wxlClass->methods_n > 0) ! { ! wxLuaBindMethod* wxlMethod = wxlClass->methods; ! size_t idx, count = wxlClass->methods_n; ! lua_createtable(L, count, 0); ! ! for (idx = 0; idx < count; ++idx, ++wxlMethod) { ! // Force the baseclass methods to be found ! wxlState.GetLuaMethod(wxlClass, wxlMethod->name, true); ! ! // Create table { wxLuaBindClass userdata } ! const void **ptr = (const void **)lua_newuserdata(L, sizeof(void *)); ! *ptr = wxlMethod; ! lua_newtable(L); ! lua_pushstring(L, "__index"); ! lua_pushlightuserdata(L, wxlBinding); ! lua_pushcclosure(L, wxluabind_wxLuaBindMethod_index, 1); // push func with tag as upvalue ! lua_rawset(L, -3); ! lua_setmetatable(L, -2); ! ! lua_rawseti(L, -2, idx + 1); } + + return 1; } + + return 0; } + else if (strcmp(idx_str, "methods_n") == 0) + { + lua_pushnumber(L, wxlClass->methods_n); + return 1; + } + else if (strcmp(idx_str, "classInfo") == 0) + { + if (wxlClass->classInfo) + { + const wxLuaBindClass* classInfoClass = wxlState.GetLuaClass("wxClassInfo"); + if (classInfoClass) + { + wxlState.PushUserDataType(*classInfoClass->class_tag, wxlClass->classInfo); + return 1; + } + } + return 0; + } + else if (strcmp(idx_str, "class_tag") == 0) + { + lua_pushnumber(L, *wxlClass->class_tag); + return 1; + } + else if (strcmp(idx_str, "baseclassName") == 0) + { + lua_pushstring(L, wxlClass->baseclassName); + return 1; + } + else if (strcmp(idx_str, "baseclass") == 0) + { + if (wxlClass->baseclass) + { + const void **ptr = (const void **)lua_newuserdata(L, sizeof(void *)); + *ptr = wxlClass->baseclass; + lua_newtable(L); + lua_pushstring(L, "__index"); + lua_pushlightuserdata(L, wxlBinding); + lua_pushcclosure(L, wxluabind_wxLuaBindClass_index, 1); // push func with tag as upvalue + lua_rawset(L, -3); + lua_setmetatable(L, -2); + + return 1; + } + + return 0; + } + else if (strcmp(idx_str, "enums") == 0) + { + if (wxlClass->enums_n > 0) + { + wxLuaBindDefine* wxlDefine = wxlClass->enums; + size_t idx, count = wxlClass->enums_n; + lua_createtable(L, count, 0); + + for (idx = 0; idx < count; ++idx, ++wxlDefine) + { + // Create table { name, value } + lua_createtable(L, 0, 2); + lua_pushstring(L, "name"); + lua_pushstring(L, wxlDefine->name); + lua_rawset(L, -3); + lua_pushstring(L, "value"); + lua_pushnumber(L, wxlDefine->value); + lua_rawset(L, -3); + + lua_rawseti(L, -2, idx + 1); + } + + return 1; + } + + return 0; + } + else if (strcmp(idx_str, "enums_n") == 0) + { + lua_pushnumber(L, wxlClass->enums_n); + return 1; + } } ! return 0; ! } ! ! //----------------------------------------------------------------------------- ! // wxluabind_wxLuaBinding_index ! //----------------------------------------------------------------------------- ! ! int LUACALL wxluabind_wxLuaBinding_index(lua_State* L) ! { ! wxLuaState wxlState(L); ! wxCHECK_MSG(wxlState.Ok(), 0, wxT("Invalid wxLuaState")); ! ! void **ptr = (void **)lua_touserdata(L, 1); ! wxLuaBinding* wxlBinding = (wxLuaBinding*)*ptr; ! ! int idx_type = lua_type(L, 2); ! ! if (idx_type == LUA_TSTRING) ! { ! const char* idx_str = lua_tostring(L, 2); ! ! if (strcmp(idx_str, "GetBindingName") == 0) ! { ! lua_pushstring(L, wx2lua(wxlBinding->GetBindingName())); ! return 1; ! } ! else if (strcmp(idx_str, "GetLuaNamespace") == 0) ! { ! lua_pushstring(L, wx2lua(wxlBinding->GetLuaNamespace())); ! return 1; ! } ! else if (strcmp(idx_str, "GetClassCount") == 0) ! { ! lua_pushnumber(L, wxlBinding->GetClassCount()); ! return 1; ! } ! else if (strcmp(idx_str, "GetDefineCount") == 0) ! { ! lua_pushnumber(L, wxlBinding->GetDefineCount()); ! return 1; ! } ! else if (strcmp(idx_str, "GetEventCount") == 0) ! { ! lua_pushnumber(L, wxlBinding->GetEventCount()); ! return 1; ! } ! else if (strcmp(idx_str, "GetObjectCount") == 0) ! { ! lua_pushnumber(L, wxlBinding->GetObjectCount()); ! return 1; ! } ! else if (strcmp(idx_str, "GetFunctionCount") == 0) ! { ! lua_pushnumber(L, wxlBinding->GetFunctionCount()); ! return 1; ! } ! else if (strcmp(idx_str, "GetClassArray") == 0) ! { ! wxLuaBindClass* wxlClass = wxlBinding->GetClassArray(); ! size_t idx, count = wxlBinding->GetClassCount(); ! lua_createtable(L, count, 0); ! ! for (idx = 0; idx < count; ++idx, ++wxlClass) ! { ! // Create table { wxLuaBindClass userdata } ! const void **ptr = (const void **)lua_newuserdata(L, sizeof(void *)); ! *ptr = wxlClass; ! lua_newtable(L); ! lua_pushstring(L, "__index"); ! lua_pushlightuserdata(L, wxlBinding); ! lua_pushcclosure(L, wxluabind_wxLuaBindClass_index, 1); // push func with tag as upvalue ! lua_rawset(L, -3); ! lua_setmetatable(L, -2); ! ! lua_rawseti(L, -2, idx + 1); ! } ! ! return 1; ! } ! else if (strcmp(idx_str, "GetFunctionArray") == 0) ! { ! wxLuaBindMethod* wxlMethod = wxlBinding->GetFunctionArray(); ! size_t idx, count = wxlBinding->GetFunctionCount(); ! lua_createtable(L, count, 0); ! ! for (idx = 0; idx < count; ++idx, ++wxlMethod) ! { ! // Create table { wxLuaBindClass userdata } ! const void **ptr = (const void **)lua_newuserdata(L, sizeof(void *)); ! *ptr = wxlMethod; ! lua_newtable(L); ! lua_pushstring(L, "__index"); ! lua_pushlightuserdata(L, wxlBinding); ! lua_pushcclosure(L, wxluabind_wxLuaBindMethod_index, 1); // push func with tag as upvalue ! lua_rawset(L, -3); ! lua_setmetatable(L, -2); ! ! lua_rawseti(L, -2, idx + 1); ! } ! ! return 1; ! } ! else if (strcmp(idx_str, "GetDefineArray") == 0) ! { ! wxLuaBindDefine* wxlDefine = wxlBinding->GetDefineArray(); ! size_t idx, count = wxlBinding->GetDefineCount(); ! lua_createtable(L, count, 0); ! ! for (idx = 0; idx < count; ++idx, ++wxlDefine) ! { ! // Create table { name, value } ! lua_createtable(L, 0, 2); ! lua_pushstring(L, "name"); ! lua_pushstring(L, wxlDefine->name); ! lua_rawset(L, -3); ! lua_pushstring(L, "value"); ! lua_pushnumber(L, wxlDefine->value); ! lua_rawset(L, -3); ! ! lua_rawseti(L, -2, idx + 1); ! } ! ! return 1; ! } ! else if (strcmp(idx_str, "GetStringArray") == 0) ! { ! wxLuaBindString* wxlString = wxlBinding->GetStringArray(); ! size_t idx, count = wxlBinding->GetStringCount(); ! lua_createtable(L, count, 0); ! ! for (idx = 0; idx < count; ++idx, ++wxlString) ! { ! // Create table { name, value } ! lua_createtable(L, 0, 2); ! lua_pushstring(L, "name"); ! lua_pushstring(L, wxlString->name); ! lua_rawset(L, -3); ! lua_pushstring(L, "value"); ! lua_pushstring(L, wx2lua(wxlString->value)); ! lua_rawset(L, -3); ! ! lua_rawseti(L, -2, idx + 1); ! } ! ! return 1; ! } ! else if (strcmp(idx_str, "GetEventArray") == 0) ! { ! wxLuaBindEvent* wxlEvent = wxlBinding->GetEventArray(); ! size_t idx, count = wxlBinding->GetEventCount(); ! lua_createtable(L, count, 0); ! ! for (idx = 0; idx < count; ++idx, ++wxlEvent) ! { ! // Create table { name, eventType, class_tag } ! lua_createtable(L, 0, 3); ! lua_pushstring(L, "name"); ! lua_pushstring(L, wxlEvent->name); ! lua_rawset(L, -3); ! lua_pushstring(L, "eventType"); ! lua_pushnumber(L, *wxlEvent->eventType); ! lua_rawset(L, -3); ! lua_pushstring(L, "class_tag"); ! lua_pushnumber(L, *wxlEvent->class_tag); ! lua_rawset(L, -3); ! ! lua_rawseti(L, -2, idx + 1); ! } ! ! return 1; ! } ! else if (strcmp(idx_str, "GetObjectArray") == 0) ! { ! wxLuaBindObject* wxlObject = wxlBinding->GetObjectArray(); ! size_t idx, count = wxlBinding->GetObjectCount(); ! lua_createtable(L, count, 0); ! ! for (idx = 0; idx < count; ++idx, ++wxlObject) ! { ! // Create table { name, object, class_tag } ! lua_createtable(L, 0, 3); ! lua_pushstring(L, "name"); ! lua_pushstring(L, wxlObject->name); ! lua_rawset(L, -3); ! ! lua_pushstring(L, "object"); ! if (wxlObject->objPtr != 0) ! wxlState.tpushusertag(wxlObject->objPtr, *wxlObject->class_tag); ! else ! wxlState.tpushusertag(*wxlObject->pObjPtr, *wxlObject->class_tag); ! lua_rawset(L, -3); ! ! lua_pushstring(L, "class_tag"); ! lua_pushnumber(L, *wxlObject->class_tag); ! lua_rawset(L, -3); ! ! lua_rawseti(L, -2, idx + 1); ! } ! ! return 1; ! } ! } ! ! return 0; } |