From: John L. <jr...@us...> - 2008-01-08 00:55:39
|
Update of /cvsroot/wxlua/wxLua/modules/wxlua/src In directory sc8-pr-cvs9.sourceforge.net:/tmp/cvs-serv13069/wxLua/modules/wxlua/src Modified Files: wxlbind.cpp wxlstate.cpp Log Message: Allow "%member static int m_var" to work Index: wxlstate.cpp =================================================================== RCS file: /cvsroot/wxlua/wxLua/modules/wxlua/src/wxlstate.cpp,v retrieving revision 1.160 retrieving revision 1.161 diff -C2 -d -r1.160 -r1.161 *** wxlstate.cpp 7 Jan 2008 19:55:26 -0000 1.160 --- wxlstate.cpp 8 Jan 2008 00:55:35 -0000 1.161 *************** *** 656,660 **** // check if they've dynamic casted the object or if it was casted in C++ ! if (wxlua_iswxuserdata(L, -1) && (wxl_type == wxluaT_type(L, -1))) { if (push_on_stack) --- 656,660 ---- // check if they've dynamic casted the object or if it was casted in C++ ! if (wxl_type == wxluaT_type(L, -1)) { if (push_on_stack) Index: wxlbind.cpp =================================================================== RCS file: /cvsroot/wxlua/wxLua/modules/wxlua/src/wxlbind.cpp,v retrieving revision 1.111 retrieving revision 1.112 diff -C2 -d -r1.111 -r1.112 *** wxlbind.cpp 4 Jan 2008 00:21:08 -0000 1.111 --- wxlbind.cpp 8 Jan 2008 00:55:35 -0000 1.112 *************** *** 335,339 **** // for class instances. ! // Lua stack : 1 = userdata, 2 = "FuncName" // You cannot seem to get the calling convention (. or :) or if it was // called as a function() or a .member? --- 335,339 ---- // for class instances. ! // Lua stack : 1 = userdata, 2 = key; userdata:key() // You cannot seem to get the calling convention (. or :) or if it was // called as a function() or a .member? *************** *** 344,348 **** wxlua_setcallbaseclassfunction(L, false); - bool callbase = false; bool found = false; int result = 0; --- 344,347 ---- *************** *** 350,370 **** wxCHECK_MSG(wxlClass, 0, wxT("Invalid wxLuaBindClass")); - // // See if the function or value is in our metatable - // if (lua_getmetatable(L, 1)) - // { - // lua_pushvalue(L, 2); - // lua_rawget(L, -2); - // - // if (!lua_isnil(L, -1)) - // { - // found = true; - // result = 1; - // lua_remove(L, -2); // remove metatable, leave value - // return result; - // } - // else - // lua_pop(L, 2); // pop nil and metatable - // } - void *obj_ptr = wxlua_touserdata(L, 1, false); const char *name = lua_tostring(L, 2); // name of the __index method called in Lua --- 349,352 ---- *************** *** 379,396 **** { // check if we're to call the baseclass function or if it's a Lua derived function ! if (!found) ! { ! callbase = (name[0] == '_'); ! if (callbase) ! name++; // skip past "_"[FunctionName] ! else { ! // 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 ! } } } --- 361,375 ---- { // check if we're to call the baseclass function or if it's a Lua derived function ! bool callbase = (name[0] == '_'); ! if (callbase) ! name++; // skip past "_"[FunctionName] ! else ! { ! // 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 } } *************** *** 470,474 **** wxCHECK_MSG(wxlClass, 0, wxT("Invalid wxLuaBindClass")); ! // Lua Stack 1 = userdata, 2 = key, 3 = value; userdata.key = value const char *name = lua_tostring(L, 2); --- 449,453 ---- wxCHECK_MSG(wxlClass, 0, wxT("Invalid wxLuaBindClass")); ! // Lua Stack: 1 = userdata, 2 = key, 3 = value; userdata.key = value const char *name = lua_tostring(L, 2); *************** *** 560,563 **** --- 539,614 ---- } + int LUACALL wxlua_wxLuaBindMethod_table__index(lua_State *L) + { + // 1 = table, 2 = key + + wxLuaBindClass *wxlClass = (wxLuaBindClass *)lua_touserdata(L, lua_upvalueindex(1)); + wxCHECK_MSG(wxlClass, 0, wxT("Invalid wxLuaBindClass")); + + int result = 0; + + const char* name = lua_tostring(L, 2); + if (!name) + { + // name is NULL if it's not a string + wxlua_error(L, wxString::Format(_("wxLua: Attempt to call a static class method using '%s' on a '%s' type."), + wxlua_luaL_typename(L, 2).c_str(), lua2wx(wxlClass->name).c_str())); + return 0; + } + + wxLuaBindMethod* wxlMethod = wxLuaBinding::GetClassMethod(wxlClass, name, WXLUAMETHOD_GETPROP, true); + + if (wxlMethod && WXLUA_HASBIT(wxlMethod->method_type, WXLUAMETHOD_STATIC)) + { + lua_pop(L, 2); // remove the table and the name of the function + result = (*wxlMethod->wxluacfuncs[0].lua_cfunc)(L); // + } + else + { + lua_pushvalue(L, -1); + lua_rawget(L, -3); + if (lua_isnil(L, -1)) + lua_pop(L, 1); + else + result = 1; + } + + return result; + } + + int LUACALL wxlua_wxLuaBindMethod_table__newindex(lua_State *L) + { + // 1 = table, 2 = key, 3 = value + + wxLuaBindClass *wxlClass = (wxLuaBindClass *)lua_touserdata(L, lua_upvalueindex(1)); + wxCHECK_MSG(wxlClass, 0, wxT("Invalid wxLuaBindClass")); + + const char* name = lua_tostring(L, 2); + if (!name) + { + // name is NULL if it's not a string + wxlua_error(L, wxString::Format(_("wxLua: Attempt to call a static class method using '%s' on a '%s' type."), + wxlua_luaL_typename(L, 2).c_str(), lua2wx(wxlClass->name).c_str())); + return 0; + } + + wxLuaBindMethod* wxlMethod = wxLuaBinding::GetClassMethod(wxlClass, name, WXLUAMETHOD_SETPROP, true); + + if (wxlMethod && WXLUA_HASBIT(wxlMethod->method_type, WXLUAMETHOD_STATIC)) + { + lua_remove(L, 2); // remove the key + lua_remove(L, 1); // remove the table + (*wxlMethod->wxluacfuncs[0].lua_cfunc)(L); + } + else + { + lua_pushvalue(L, -2); + lua_pushvalue(L, -2); + lua_rawset(L, -5); + } + + return 0; + } + // ---------------------------------------------------------------------------- // Central function to call for overloaded functions *************** *** 1069,1073 **** for (int i_static_method = 0; i_static_method < method_count; ++i_static_method, ++wxlMethod) { ! if (WXLUA_HASBIT(wxlMethod->method_type, WXLUAMETHOD_STATIC) && wxlMethod->wxluacfuncs_n) { lua_pushstring(L, wxlMethod->name); --- 1120,1126 ---- for (int i_static_method = 0; i_static_method < method_count; ++i_static_method, ++wxlMethod) { ! // we will handle the WXLUAMETHOD_GET/SETPROP|WXLUAMETHOD_STATIC using __index and __newindex ! if (((wxlMethod->method_type & (WXLUAMETHOD_METHOD|WXLUAMETHOD_STATIC)) == (WXLUAMETHOD_METHOD|WXLUAMETHOD_STATIC)) && ! (wxlMethod->wxluacfuncs_n > 0)) { lua_pushstring(L, wxlMethod->name); *************** *** 1078,1081 **** --- 1131,1151 ---- } + // Create a metatable for the "class" table + lua_newtable(L); + lua_pushlstring(L, "__index", 7); + lua_pushlightuserdata(L, wxlClass); + lua_pushcclosure(L, wxlua_wxLuaBindMethod_table__index, 1); + lua_rawset(L, -3); + + lua_pushlstring(L, "__newindex", 10); + lua_pushlightuserdata(L, wxlClass); + lua_pushcclosure(L, wxlua_wxLuaBindMethod_table__newindex, 1); + lua_rawset(L, -3); + + //lua_pushstring(L, "__metatable"); + //lua_pushstring(L, "Metatable is not accessible"); + //lua_rawset(L, -3); + lua_setmetatable(L, -2); + // Finalize the class table since we may not have a constructor // or have multiple constructors. *************** *** 1107,1112 **** lua_rawset(L, -3); ! // Create the metatable for this table ! lua_newtable(L); lua_pushlstring(L, "__call", 6); --- 1177,1183 ---- lua_rawset(L, -3); ! // Add __call to the metatable for this table ! bool has_meta = (lua_getmetatable(L, -1) != 0); ! if (!has_meta) lua_newtable(L); lua_pushlstring(L, "__call", 6); *************** *** 1119,1123 **** //lua_rawset(L, -3); ! lua_setmetatable(L, -2); // add table to the binding table t[wxlMethod->name] = { this table } --- 1190,1197 ---- //lua_rawset(L, -3); ! if (!has_meta) ! lua_setmetatable(L, -2); ! else ! lua_pop(L, 1); // add table to the binding table t[wxlMethod->name] = { this table } |