Lua: Use full userdata to store class binds

This commit is contained in:
Ilari Liusvaara 2013-08-21 23:32:48 +03:00
parent 9092dea931
commit e7472118a5
4 changed files with 12 additions and 18 deletions

View file

@ -624,7 +624,7 @@ template<class T> struct lua_class_bind_data
/** /**
* The name of the method to pass. * The name of the method to pass.
*/ */
std::string fname; char fname[];
}; };
template<class T> class lua_class; template<class T> class lua_class;
@ -734,22 +734,16 @@ public:
* Parameter fn: The method to call. * Parameter fn: The method to call.
* Parameter force: If true, overwrite existing method. * 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); load_metatable(state);
state.pushstring(keyname); state.pushstring(keyname);
state.rawget(-2); std::string fname = std::string("Method ") + keyname;
if(!state.isnil(-1) && !force) { void* ptr = state.newuserdata(sizeof(lua_class_bind_data<T>) + fname.length() + 1);
state.pop(2); lua_class_bind_data<T>* bdata = reinterpret_cast<lua_class_bind_data<T>*>(ptr);
return;
}
state.pop(1);
lua_class_bind_data<T>* bdata = new lua_class_bind_data<T>;
bdata->fn = fn; bdata->fn = fn;
bdata->fname = std::string("Method ") + keyname;
bdata->state = &state.get_master(); bdata->state = &state.get_master();
state.pushstring(keyname); std::copy(fname.begin(), fname.end(), bdata->fname);
state.pushlightuserdata(bdata);
state.pushcclosure(class_bind_trampoline, 1); state.pushcclosure(class_bind_trampoline, 1);
state.rawset(-3); state.rawset(-3);
state.pop(1); state.pop(1);

View file

@ -34,8 +34,8 @@ namespace
{ {
static char doonce_key; static char doonce_key;
if(L.do_once(&doonce_key)) { if(L.do_once(&doonce_key)) {
objclass<lua_callbacks_list>().bind(L, "__index", &lua_callbacks_list::index, true); objclass<lua_callbacks_list>().bind(L, "__index", &lua_callbacks_list::index);
objclass<lua_callbacks_list>().bind(L, "__newindex", &lua_callbacks_list::newindex, true); objclass<lua_callbacks_list>().bind(L, "__newindex", &lua_callbacks_list::newindex);
} }
} }

View file

@ -476,8 +476,8 @@ lua_mmap_struct::lua_mmap_struct(lua_state& L)
{ {
static char done_key; static char done_key;
if(L.do_once(&done_key)) { if(L.do_once(&done_key)) {
objclass<lua_mmap_struct>().bind(L, "__index", &lua_mmap_struct::index, true); objclass<lua_mmap_struct>().bind(L, "__index", &lua_mmap_struct::index);
objclass<lua_mmap_struct>().bind(L, "__newindex", &lua_mmap_struct::newindex, true); objclass<lua_mmap_struct>().bind(L, "__newindex", &lua_mmap_struct::newindex);
objclass<lua_mmap_struct>().bind(L, "__call", &lua_mmap_struct::map); objclass<lua_mmap_struct>().bind(L, "__call", &lua_mmap_struct::map);
} }
} }

View file

@ -138,8 +138,8 @@ namespace
{ {
static char doonce_key; static char doonce_key;
if(L.do_once(&doonce_key)) { if(L.do_once(&doonce_key)) {
objclass<lua_vma_list>().bind(L, "__index", &lua_vma_list::index, true); objclass<lua_vma_list>().bind(L, "__index", &lua_vma_list::index);
objclass<lua_vma_list>().bind(L, "__newindex", &lua_vma_list::newindex, true); objclass<lua_vma_list>().bind(L, "__newindex", &lua_vma_list::newindex);
objclass<lua_vma_list>().bind(L, "__call", &lua_vma_list::call); objclass<lua_vma_list>().bind(L, "__call", &lua_vma_list::call);
} }
} }