Re: is it possible to obtain registed classes and functions list of a lua state?

Evan Wies <[email protected]>
Newsgroups gmane.comp.lang.lua.bind.user
Message-ID <[email protected]>
Everything I know is from looking at source code and playing around...

http://github.com/luabind/luabind/blob/master/luabind/class_info.hpp
http://github.com/luabind/luabind/blob/master/src/class_info.cpp

I think since it uses the class_registry, then it will work with any luabind object you pass to it,
even if it wasn't constructed directlyin Lua.    I submitted a patch which fixes a problem with
passing plain Lua types, including non-luabind userdata, to class_info.  See attached -- I think it
still has some problems though.

I sent Daniel the attached patch last night... only slightly tested, but it lets you get a table of
luabind classes names (it's a mercurial patch, not a git patch).  Maybe you'll find it useful to
play with.  It may or may not officially land on the luabind tree.


-Evan


[email protected] wrote:
> thank you evan,for the good news of 0.9.but in your post i see the
> class_info takes a object as parameter,is that means all classes need a
> constructor? what about these classes doesn`t allow to be constructed
> via lua but generated by object factories in c++ side?
>  
> but whatever,it`s not very important.some thing i really worried about
> is,can we get the singnature info of methods?this seems not possible in
> 8.1,and you doesn`t show it in your previous post.so...would you please
> talk a little more about it?
>  
> btw:i forgot to register into maillist,so i haven`t receive previous
> posts,cannot reply directly,so the quote of your post will be
> missing.sorry about that.
>  
> Thanks,
> cloudage
> 
> 
> ------------------------------------------------------------------------
> 
> ------------------------------------------------------------------------------
> Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
> trial. Simplify your report design, integration and deployment - and focus on 
> what you do best, core application coding. Discover what's new with
> Crystal Reports now.  http://p.sf.net/sfu/bobj-july
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> luabind-user mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/luabind-user

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july

_______________________________________________
luabind-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/luabind-user
class_names.patch (text/x-patch, 3 KB)
# HG changeset patch
# User Evan Wies <[email protected]>
# Date 1258437041 18000
# Node ID e8bfc790cf48df527d218a71bf10edf1a3c2d285
# Parent  8d47ac638cc15c3b583d45b51f88e5d129d1b0ea
acr+5, added class_names

diff -r 8d47ac638cc1 -r e8bfc790cf48 luabind/class_info.hpp
--- a/luabind/class_info.hpp	Fri Nov 13 06:17:55 2009 -0500
+++ b/luabind/class_info.hpp	Tue Nov 17 00:50:41 2009 -0500
@@ -40,6 +40,9 @@
 
     LUABIND_API class_info get_class_info(argument const&);
 
+    // returns a table of bound class names
+    LUABIND_API object get_class_names(lua_State* L);
+
 	LUABIND_API void bind_class_info(lua_State*);
 }
 
diff -r 8d47ac638cc1 -r e8bfc790cf48 luabind/detail/class_registry.hpp
--- a/luabind/detail/class_registry.hpp	Fri Nov 13 06:17:55 2009 -0500
+++ b/luabind/detail/class_registry.hpp	Tue Nov 17 00:50:41 2009 -0500
@@ -51,6 +51,8 @@
 
 		class_rep* find_class(type_id const& info) const;
 
+        const std::map<type_id, class_rep*>& get_classes() const { return m_classes; }
+
 	private:
 
 		std::map<type_id, class_rep*> m_classes;
diff -r 8d47ac638cc1 -r e8bfc790cf48 src/class_info.cpp
--- a/src/class_info.cpp	Fri Nov 13 06:17:55 2009 -0500
+++ b/src/class_info.cpp	Tue Nov 17 00:50:41 2009 -0500
@@ -26,6 +26,7 @@
 
 #include <luabind/luabind.hpp>
 #include <luabind/class_info.hpp>
+#include <luabind/detail/class_registry.hpp>
 
 namespace luabind
 {
@@ -82,6 +83,26 @@
         return result;
 	}
 
+    LUABIND_API object get_class_names(lua_State* L)
+    {
+        detail::class_registry* reg = detail::class_registry::get_registry(L);
+        if (!reg)
+        {
+            lua_pushnil(L);
+            return object(from_stack(L,-1));
+        }
+
+        const std::map<type_id, detail::class_rep*>& classes = reg->get_classes();
+        object result = newtable(L);
+        std::size_t index = 1;
+        for (std::map<type_id, detail::class_rep*>::const_iterator iter = classes.begin();
+            iter != classes.end(); ++iter)
+        {
+            result[index++] = iter->second->name();
+        }
+        return result;
+    }
+
 	LUABIND_API void bind_class_info(lua_State* L)
 	{
 		module(L)
@@ -91,7 +112,8 @@
 				.def_readonly("methods", &class_info::methods)
 				.def_readonly("attributes", &class_info::attributes),
 		
-			def("class_info", &get_class_info)
+			def("class_info", &get_class_info),
+            def("class_names", &get_class_names)
 		];
 	}
 }
diff -r 8d47ac638cc1 -r e8bfc790cf48 test/test_class_info.cpp
--- a/test/test_class_info.cpp	Fri Nov 13 06:17:55 2009 -0500
+++ b/test/test_class_info.cpp	Tue Nov 17 00:50:41 2009 -0500
@@ -41,5 +41,10 @@
         "assert(info.name == 'number')\n"
         "assert(#info.methods == 0)\n"
         "assert(#info.attributes == 0)\n"
+        "names = class_names()\n"
+        "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"
     );
 }
get_class_info.patch.git (text/plain, 965 B)
commit ad43b5fe8509fb793de7a6ee08c15309db8a75e3
Author: Evan Wies <[email protected]>
Date:   Mon Oct 26 17:49:59 2009 -0400

    Fix get_class_info crash
    
    If an object that is not a luabind class is passed to get_class_info, it would segfault.
    
    The new behavior is to return a class_info object with the Lua type as its name.

diff --git a/src/class_info.cpp b/src/class_info.cpp
index 3733878..2169d5d 100755
--- a/src/class_info.cpp
+++ b/src/class_info.cpp
@@ -35,6 +35,15 @@ namespace luabind
        
                o.push(L);
         detail::object_rep* obj = detail::is_class_object(L, -1);
+        if (!obj)
+        {
+            class_info result;
+            result.name = lua_typename(L, -1);
+            lua_pop(L, 1);
+            result.methods = newtable(L);
+            result.attributes = newtable(L);
+            return result;
+        }
                lua_pop(L, 1);
 
         obj->crep()->get_table(L);
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.