Skip to content

Commit

Permalink
Actually be able to take in a class as argument to class_info
Browse files Browse the repository at this point in the history
Includes test.

Conflicts:
	src/class_info.cpp
  • Loading branch information
rpavlik authored and Oberon00 committed Jun 29, 2013
1 parent dbce738 commit c2ee1f8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
46 changes: 33 additions & 13 deletions src/class_info.cpp
Expand Up @@ -28,33 +28,53 @@
#include <luabind/class_info.hpp>
#include <luabind/detail/class_registry.hpp>

/*
#include <iostream>
#define VERBOSE(X) std::cout << __FILE__ << ":" << __LINE__ << ": " << X << std::endl
*/
#define VERBOSE(X)

namespace luabind
{
LUABIND_API class_info get_class_info(argument const& o)
{
lua_State* L = o.interpreter();
detail::class_rep * crep = NULL;
bool givenClassRep = false;

o.push(L);
detail::object_rep* obj = detail::get_instance(L, -1);

if (!obj)
{
class_info result;
result.name = lua_typename(L, lua_type(L, -1));
if (detail::is_class_rep(L, -1)) {
VERBOSE("OK, got a class rep");
// OK, o is a class rep, now at the top of the stack
givenClassRep = true;
crep = static_cast<detail::class_rep *>(lua_touserdata(L, -1));
lua_pop(L, 1);
result.methods = newtable(L);
result.attributes = newtable(L);
return result;
}
} else {

lua_pop(L, 1);
VERBOSE("Not a class rep");
detail::object_rep* obj = detail::get_instance(L, -1);

obj->crep()->get_table(L);
if (!obj)
{
VERBOSE("Not a obj rep");
class_info result;
result.name = lua_typename(L, lua_type(L, -1));
lua_pop(L, 1);
result.methods = newtable(L);
result.attributes = newtable(L);
return result;
} else {
lua_pop(L, 1);
// OK, we were given an object - gotta get the crep.
crep = obj->crep();
}
}
crep->get_table(L);
object table(from_stack(L, -1));
lua_pop(L, 1);

class_info result;
result.name = obj->crep()->name();
result.name = crep->name();
result.methods = newtable(L);
result.attributes = newtable(L);

Expand Down
10 changes: 9 additions & 1 deletion test/test_class_info.cpp
Expand Up @@ -46,6 +46,14 @@ void test_main(lua_State* L)
"assert(type(names) == 'table')\n"
"assert(#names == 2)\n"
"assert(names[1] == 'X' or names[2] == 'X')\n"
"assert(names[1] == 'class_info_data' or names[2] == 'class_info_data')\n"
"assert(names[1] == 'class_info_data' or names[2] == 'class_info_data')\n");

DOSTRING(L,
"info = class_info(X)\n"
"assert(info.name == 'X')\n"
"assert(info.methods['f'] == X().f)\n"
"assert(info.methods['__init'] == X().__init)\n"
"assert(info.attributes[1] == 'y')\n"
"assert(info.attributes[2] == 'x')\n"
);
}

0 comments on commit c2ee1f8

Please sign in to comment.