Skip to content

Commit c2ee1f8

Browse files
rpavlikOberon00
authored andcommitted
Actually be able to take in a class as argument to class_info
Includes test. Conflicts: src/class_info.cpp
1 parent dbce738 commit c2ee1f8

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

src/class_info.cpp

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,33 +28,53 @@
2828
#include <luabind/class_info.hpp>
2929
#include <luabind/detail/class_registry.hpp>
3030

31+
/*
32+
#include <iostream>
33+
#define VERBOSE(X) std::cout << __FILE__ << ":" << __LINE__ << ": " << X << std::endl
34+
*/
35+
#define VERBOSE(X)
36+
3137
namespace luabind
3238
{
3339
LUABIND_API class_info get_class_info(argument const& o)
3440
{
3541
lua_State* L = o.interpreter();
42+
detail::class_rep * crep = NULL;
43+
bool givenClassRep = false;
3644

3745
o.push(L);
38-
detail::object_rep* obj = detail::get_instance(L, -1);
39-
40-
if (!obj)
41-
{
42-
class_info result;
43-
result.name = lua_typename(L, lua_type(L, -1));
46+
if (detail::is_class_rep(L, -1)) {
47+
VERBOSE("OK, got a class rep");
48+
// OK, o is a class rep, now at the top of the stack
49+
givenClassRep = true;
50+
crep = static_cast<detail::class_rep *>(lua_touserdata(L, -1));
4451
lua_pop(L, 1);
45-
result.methods = newtable(L);
46-
result.attributes = newtable(L);
47-
return result;
48-
}
52+
} else {
4953

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

52-
obj->crep()->get_table(L);
57+
if (!obj)
58+
{
59+
VERBOSE("Not a obj rep");
60+
class_info result;
61+
result.name = lua_typename(L, lua_type(L, -1));
62+
lua_pop(L, 1);
63+
result.methods = newtable(L);
64+
result.attributes = newtable(L);
65+
return result;
66+
} else {
67+
lua_pop(L, 1);
68+
// OK, we were given an object - gotta get the crep.
69+
crep = obj->crep();
70+
}
71+
}
72+
crep->get_table(L);
5373
object table(from_stack(L, -1));
5474
lua_pop(L, 1);
5575

5676
class_info result;
57-
result.name = obj->crep()->name();
77+
result.name = crep->name();
5878
result.methods = newtable(L);
5979
result.attributes = newtable(L);
6080

test/test_class_info.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ void test_main(lua_State* L)
4646
"assert(type(names) == 'table')\n"
4747
"assert(#names == 2)\n"
4848
"assert(names[1] == 'X' or names[2] == 'X')\n"
49-
"assert(names[1] == 'class_info_data' or names[2] == 'class_info_data')\n"
49+
"assert(names[1] == 'class_info_data' or names[2] == 'class_info_data')\n");
50+
51+
DOSTRING(L,
52+
"info = class_info(X)\n"
53+
"assert(info.name == 'X')\n"
54+
"assert(info.methods['f'] == X().f)\n"
55+
"assert(info.methods['__init'] == X().__init)\n"
56+
"assert(info.attributes[1] == 'y')\n"
57+
"assert(info.attributes[2] == 'x')\n"
5058
);
5159
}

0 commit comments

Comments
 (0)