From e3d8eb04b3b2a7cad57d7a4bc68cc30e9368b60b Mon Sep 17 00:00:00 2001 From: Daniel Wallin Date: Thu, 29 Jan 2009 14:11:10 +0100 Subject: [PATCH] Reimplement class_info() for the new property system. This was left out of 0.8 by mistake. --- luabind/class_info.hpp | 2 +- src/class_info.cpp | 55 ++++++++++++++++++++++++++++++-------------------- 2 files changed, 34 insertions(+), 23 deletions(-) diff --git a/luabind/class_info.hpp b/luabind/class_info.hpp index 92c9acf..5fdca42 100755 --- a/luabind/class_info.hpp +++ b/luabind/class_info.hpp @@ -38,7 +38,7 @@ namespace luabind object attributes; }; - class_info get_class_info(const object&); + class_info get_class_info(argument const&); void bind_class_info(lua_State*); } diff --git a/src/class_info.cpp b/src/class_info.cpp index 3a8bc5e..ff2fe0d 100755 --- a/src/class_info.cpp +++ b/src/class_info.cpp @@ -29,37 +29,48 @@ namespace luabind { - class_info get_class_info(const object& o) + class_info get_class_info(argument const& o) { lua_State* L = o.interpreter(); - class_info result; - o.push(L); - detail::object_rep* obj = static_cast(lua_touserdata(L, -1)); + detail::object_rep* obj = detail::is_class_object(L, -1); lua_pop(L, 1); - result.name = obj->crep()->name(); - obj->crep()->get_table(L); + obj->crep()->get_table(L); + object table(from_stack(L, -1)); + lua_pop(L, 1); - object methods(from_stack(L, -1)); - - methods.swap(result.methods); - lua_pop(L, 1); - - result.attributes = newtable(L); + class_info result; + result.name = obj->crep()->name(); + result.methods = newtable(L); + result.attributes = newtable(L); - typedef detail::class_rep::property_map map_type; - - std::size_t index = 1; - - for (map_type::const_iterator i = obj->crep()->properties().begin(); - i != obj->crep()->properties().end(); ++i, ++index) - { - result.attributes[index] = i->first; - } + std::size_t index = 1; + + for (iterator i(table), e; i != e; ++i) + { + if (type(*i) != LUA_TFUNCTION) + continue; + + // We have to create a temporary `object` here, otherwise the proxy + // returned by operator->() will mess up the stack. This is a known + // problem that probably doesn't show up in real code very often. + object member(*i); + member.push(L); + detail::stack_pop pop(L, 1); + + if (lua_tocfunction(L, -1) == &detail::property_tag) + { + result.attributes[index++] = i.key(); + } + else + { + result.methods[i.key()] = *i; + } + } - return result; + return result; } void bind_class_info(lua_State* L) -- 2.11.4.GIT