From e7472118a5ef73771a6b7dbe6cbb322d52b7207d Mon Sep 17 00:00:00 2001 From: Ilari Liusvaara Date: Wed, 21 Aug 2013 23:32:48 +0300 Subject: [PATCH] Lua: Use full userdata to store class binds --- include/library/luabase.hpp | 18 ++++++------------ src/lua/callback.cpp | 4 ++-- src/lua/memory.cpp | 4 ++-- src/lua/memory2.cpp | 4 ++-- 4 files changed, 12 insertions(+), 18 deletions(-) diff --git a/include/library/luabase.hpp b/include/library/luabase.hpp index abd8e125..bcc9d01f 100644 --- a/include/library/luabase.hpp +++ b/include/library/luabase.hpp @@ -624,7 +624,7 @@ template struct lua_class_bind_data /** * The name of the method to pass. */ - std::string fname; + char fname[]; }; template class lua_class; @@ -734,22 +734,16 @@ public: * Parameter fn: The method to call. * Parameter force: If true, overwrite existing method. */ - void bind(lua_state& state, const char* keyname, int (T::*fn)(lua_state& LS), bool force = false) + void bind(lua_state& state, const char* keyname, int (T::*fn)(lua_state& LS)) { load_metatable(state); state.pushstring(keyname); - state.rawget(-2); - if(!state.isnil(-1) && !force) { - state.pop(2); - return; - } - state.pop(1); - lua_class_bind_data* bdata = new lua_class_bind_data; + std::string fname = std::string("Method ") + keyname; + void* ptr = state.newuserdata(sizeof(lua_class_bind_data) + fname.length() + 1); + lua_class_bind_data* bdata = reinterpret_cast*>(ptr); bdata->fn = fn; - bdata->fname = std::string("Method ") + keyname; bdata->state = &state.get_master(); - state.pushstring(keyname); - state.pushlightuserdata(bdata); + std::copy(fname.begin(), fname.end(), bdata->fname); state.pushcclosure(class_bind_trampoline, 1); state.rawset(-3); state.pop(1); diff --git a/src/lua/callback.cpp b/src/lua/callback.cpp index d3d7e78c..3d346c17 100644 --- a/src/lua/callback.cpp +++ b/src/lua/callback.cpp @@ -34,8 +34,8 @@ namespace { static char doonce_key; if(L.do_once(&doonce_key)) { - objclass().bind(L, "__index", &lua_callbacks_list::index, true); - objclass().bind(L, "__newindex", &lua_callbacks_list::newindex, true); + objclass().bind(L, "__index", &lua_callbacks_list::index); + objclass().bind(L, "__newindex", &lua_callbacks_list::newindex); } } diff --git a/src/lua/memory.cpp b/src/lua/memory.cpp index c82035b6..105c26e8 100644 --- a/src/lua/memory.cpp +++ b/src/lua/memory.cpp @@ -476,8 +476,8 @@ lua_mmap_struct::lua_mmap_struct(lua_state& L) { static char done_key; if(L.do_once(&done_key)) { - objclass().bind(L, "__index", &lua_mmap_struct::index, true); - objclass().bind(L, "__newindex", &lua_mmap_struct::newindex, true); + objclass().bind(L, "__index", &lua_mmap_struct::index); + objclass().bind(L, "__newindex", &lua_mmap_struct::newindex); objclass().bind(L, "__call", &lua_mmap_struct::map); } } diff --git a/src/lua/memory2.cpp b/src/lua/memory2.cpp index 47a51b07..2bb8a9c3 100644 --- a/src/lua/memory2.cpp +++ b/src/lua/memory2.cpp @@ -138,8 +138,8 @@ namespace { static char doonce_key; if(L.do_once(&doonce_key)) { - objclass().bind(L, "__index", &lua_vma_list::index, true); - objclass().bind(L, "__newindex", &lua_vma_list::newindex, true); + objclass().bind(L, "__index", &lua_vma_list::index); + objclass().bind(L, "__newindex", &lua_vma_list::newindex); objclass().bind(L, "__call", &lua_vma_list::call); } }