Index: trunk/nv/lua/lua_flags.hh
===================================================================
--- trunk/nv/lua/lua_flags.hh	(revision 214)
+++ trunk/nv/lua/lua_flags.hh	(revision 215)
@@ -40,5 +40,5 @@
 			void read_flags( lua_State* L, int index, flags<SIZE,T>& flags ) 
 			{ 
-				read_flags( L, flags.data(), flags.size() );
+				read_flags( L, index, flags.data(), flags.size() );
 			}
 
Index: trunk/nv/lua/lua_state.hh
===================================================================
--- trunk/nv/lua/lua_state.hh	(revision 214)
+++ trunk/nv/lua/lua_state.hh	(revision 215)
@@ -16,4 +16,5 @@
 #include <nv/common.hh>
 #include <nv/flags.hh>
+#include <nv/types.hh>
 
 #include <nv/lua/lua_path.hh>
@@ -177,4 +178,5 @@
 			explicit state( bool load_libs = false );
 			explicit state( lua_State* state );
+			virtual ~state();
 			bool do_string( const std::string& code, const std::string& name, int rvalues = 0 );
 			bool do_stream( std::istream& stream, const std::string& name );
@@ -185,4 +187,19 @@
 			reference register_object( object * o );
 			reference register_object( object * o, const char* lua_name );
+
+			template< typename TYPE >
+			type_entry& register_type( const char* name )
+			{
+				m_type_database->create_type<TYPE>( name );
+			}
+
+			template< typename TYPE, int SIZE >
+			void register_enum( const char* name, const char* prefix, type_enum (&init_enums)[SIZE] )
+			{
+				m_type_database->create_type<TYPE>( name ).enums( init_enums );
+				register_enum_values( name, prefix );
+			}
+			void register_enum_values( const std::string& name, const std::string& prefix = std::string() );
+
 			void store_metadata( object* o, const std::string& metaname, void* pointer );
 			void unregister_object( object * o );
@@ -191,8 +208,8 @@
 			void register_object_method( const char* lua_name, const char* name )
 			{
-				register_native_object_method( lua_name, name, detail::object_method_wrapper< typename memfn_class_type<F>, F, f > );
-			}
-			void register_enum( type_database* db, const std::string& name, const std::string& prefix = std::string() );
+				register_native_object_method( lua_name, name, detail::object_method_wrapper< typename memfn_class_type<F>::type, F, f > );
+			}
 			operator lua_State*() { return m_state; }
+			type_database* get_type_database() { return m_type_database; }
 		private:
 			int load_string( const std::string& code, const std::string& name );
@@ -201,4 +218,6 @@
 			int do_current( const std::string& name, int rvalues = 0 );
 			void deep_pointer_copy( int index, void* obj );
+		private:
+			type_database* m_type_database;
 		};
 
Index: trunk/nv/root.hh
===================================================================
--- trunk/nv/root.hh	(revision 214)
+++ trunk/nv/root.hh	(revision 215)
@@ -19,13 +19,11 @@
 	{
 	public:
-		root() : object( nullptr, "" ), m_type_database( nullptr ), m_lua_state( nullptr ), m_uid_store( nullptr ) { m_root = this; }
+		root() : object( nullptr, "" ), m_lua_state( nullptr ), m_uid_store( nullptr ) { m_root = this; }
 		virtual void child_added( object* ) {}
 		virtual void child_removed( object* ) {}
-		type_database* get_type_database() const { return m_type_database; }
 		lua::state*    get_lua_state()     const { return m_lua_state; }
 		uid_store*     get_uid_store()     const { return m_uid_store; }
 		virtual ~root() { destroy_children(); }
 	protected:
-		type_database* m_type_database;
 		lua::state*    m_lua_state;
 		uid_store*     m_uid_store; 
Index: trunk/src/lua/lua_flags.cc
===================================================================
--- trunk/src/lua/lua_flags.cc	(revision 214)
+++ trunk/src/lua/lua_flags.cc	(revision 215)
@@ -20,8 +20,8 @@
 	// TODO : this can be optimized
 	lua_createtable( L, 0, 0 );
-	for ( uint32 c = 0; c < count; ++c )
+	for ( int c = 0; c < static_cast< int >( count ); ++c )
 	{
-		uint32 idx = c / 8;
-		uint32 pos = c % 8;
+		int idx = c / 8;
+		int pos = c % 8;
 		if ( ( data[ idx ] & ( 1 << static_cast< uint8 >( pos ) ) ) != 0 )
 		{
Index: trunk/src/lua/lua_state.cc
===================================================================
--- trunk/src/lua/lua_state.cc	(revision 214)
+++ trunk/src/lua/lua_state.cc	(revision 215)
@@ -32,10 +32,10 @@
 }
 
-lua::state::state( lua_State* state ) : state_wrapper( state, false )
-{
-
-}
-
-lua::state::state( bool load_libs /*= false*/ ) : state_wrapper( nullptr, true )
+lua::state::state( lua_State* state ) : state_wrapper( state, false ), m_type_database( nullptr )
+{
+
+}
+
+lua::state::state( bool load_libs /*= false*/ ) : state_wrapper( nullptr, true ), m_type_database( nullptr )
 {
 	load_lua_library();
@@ -49,4 +49,5 @@
 	if ( load_libs )
 	{
+		m_type_database = new type_database();
 		stack_guard guard( this );
 		static const luaL_Reg lualibs[] =
@@ -290,8 +291,6 @@
 lua::reference lua::state::register_object( object * o )
 {
-	if ( o == nullptr ) return ref_none;
-	type_database *db = o->get_root()->get_type_database();
-	if ( db == nullptr ) return ref_none;
-	type_entry* t = db->get_type(typeid(*o));
+	if ( o == nullptr || m_type_database == nullptr ) return ref_none;
+	type_entry* t = m_type_database->get_type(typeid(*o));
 	if ( t == nullptr ) return ref_none;
 	return register_object( o, t->name.c_str() );
@@ -359,7 +358,7 @@
 }
 
-void lua::state::register_enum( type_database* db, const std::string& name, const std::string& prefix /*= std::string() */ )
-{
-	type_entry* et = db->get_type( name );
+void lua::state::register_enum_values( const std::string& name, const std::string& prefix /*= std::string() */ )
+{
+	type_entry* et = m_type_database->get_type( name );
 
 	for ( const auto& entry : et->enum_list )
@@ -388,4 +387,12 @@
 }
 
+nv::lua::state::~state()
+{
+	if (m_type_database != nullptr )
+	{
+		delete m_type_database;
+	}
+}
+
 bool nv::lua::state_wrapper::is_defined( const path& p, bool global )
 {
