Index: trunk/src/lua/lua_state.cc
===================================================================
--- trunk/src/lua/lua_state.cc	(revision 215)
+++ trunk/src/lua/lua_state.cc	(revision 216)
@@ -15,4 +15,6 @@
 using namespace nv;
 
+// stack_guard
+
 lua::stack_guard::stack_guard( lua::state* aL )
 	: L(aL), m_level( lua_gettop(aL->m_state) )
@@ -31,4 +33,174 @@
 	lua_settop( L->m_state, m_level );
 }
+
+// state_wrapper
+
+void nv::lua::state_wrapper::push_global_table()
+{
+	lua_pushglobaltable( m_state );
+}
+
+void nv::lua::state_wrapper::pop_global_table()
+{
+	lua_replace( m_state, -2 );
+}
+
+void lua::state_wrapper::call_get()
+{
+	lua_gettable( m_state, -2 );
+}
+
+void lua::state_wrapper::call_get_raw()
+{
+	lua_rawget( m_state, -2 );
+}
+
+void lua::state_wrapper::register_native_function( lfunction f, const char* name )
+{
+	if ( m_global ) push_global_table();
+	lua_pushcfunction( m_state, f );
+	lua_setfield( m_state, -2, name );
+	if ( m_global ) pop_global_table();
+}
+
+lua::state_wrapper::~state_wrapper()
+{
+	if (m_owner)
+	{
+		lua_close( m_state );
+	}
+}
+
+bool nv::lua::state_wrapper::is_defined( const path& p, bool global )
+{
+	if ( !p.resolve( m_state, global ) )
+	{
+		return false;
+	}
+	bool result = !lua_isnil( m_state, -1 );
+	lua_pop( m_state, 1 );
+	return result;
+}
+
+bool nv::lua::state_wrapper::push_function( const path& p, bool global )
+{
+	if ( !p.resolve( m_state, global ) )
+	{
+		NV_LOG( LOG_ERROR, "Lua error : not a valid path - " + p.to_string() );
+		return false;
+	}
+
+	if ( !lua_isfunction( m_state, -1 ) ) 
+	{
+		lua_pop( m_state, 1 );
+		NV_LOG( LOG_ERROR, "Lua error : not a valid function - " + p.to_string() );
+		return false;
+	}
+	return true;
+}
+
+int nv::lua::state_wrapper::call_function( int nargs, int nresults )
+{
+	int status = lua_pcall( m_state, nargs, nresults, 0 );
+	if ( status != 0 )
+	{
+		std::string error = lua_tostring( m_state, -1 );
+		lua_pop( m_state, 1 );
+		NV_LOG( LOG_ERROR, "Lua error : " << error )
+	}
+	return status;
+}
+
+// table_guard
+
+lua::table_guard::table_guard( lua::state* lstate, const path& p, bool global )
+	: state_wrapper( lstate->get_raw(), false ), m_level(0)
+{
+	m_global = false;
+	m_level  = lua_gettop( m_state );
+	if ( !p.resolve( m_state, global ) )
+	{
+		// TODO : error handling
+	}
+}
+
+lua::table_guard::table_guard( const table_guard& parent, const path& p )
+	: state_wrapper( parent.m_state, false ), m_level(0)
+{
+	m_global = false;
+	m_level  = lua_gettop( m_state );
+	if ( !p.resolve( m_state, false ) )
+	{
+		// TODO : error handling
+	}
+}
+
+lua::table_guard::~table_guard()
+{
+	lua_settop( m_state, m_level );
+}
+
+size_t lua::table_guard::get_size()
+{
+	return lua_rawlen( m_state, -1 );
+}
+
+bool lua::table_guard::has_field( const string& element )
+{
+	lua_getfield( m_state, -1, element.c_str() );
+	bool result = lua_isnil( m_state, -1 );
+	lua_pop( m_state, 1 );
+	return result;
+}
+
+string lua::table_guard::get_string( const string& element, const string& defval /*= "" */ )
+{
+	lua_getfield( m_state, -1, element.c_str() );
+	string result( ( lua_type( m_state, -1 ) == LUA_TSTRING ) ? lua_tostring( m_state, -1 ) : defval );
+	lua_pop( m_state, 1 );
+	return result;
+}
+
+char lua::table_guard::get_char( const string& element, char defval /*= "" */ )
+{
+	lua_getfield( m_state, -1, element.c_str() );
+	char result = ( lua_type( m_state, -1 ) == LUA_TSTRING && lua_rawlen( m_state, -1 ) > 0 ) ? lua_tostring( m_state, -1 )[0] : defval;
+	lua_pop( m_state, 1 );
+	return result;
+}
+
+int lua::table_guard::get_integer( const string& element, int defval /*= "" */ )
+{
+	lua_getfield( m_state, -1, element.c_str() );
+	lua_Integer result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tointeger( m_state, -1 ) : defval;
+	lua_pop( m_state, 1 );
+	return static_cast< int >( result );
+}
+
+unsigned lua::table_guard::get_unsigned( const string& element, unsigned defval /*= "" */ )
+{
+	lua_getfield( m_state, -1, element.c_str() );
+	unsigned result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tounsigned( m_state, -1 ) : defval;
+	lua_pop( m_state, 1 );
+	return result;
+}
+
+double lua::table_guard::get_double( const string& element, double defval /*= "" */ )
+{
+	lua_getfield( m_state, -1, element.c_str() );
+	double result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tonumber( m_state, -1 ) : defval;
+	lua_pop( m_state, 1 );
+	return result;
+}
+
+bool lua::table_guard::get_boolean( const string& element, bool defval /*= "" */ )
+{
+	lua_getfield( m_state, -1, element.c_str() );
+	bool result = lua_type( m_state, -1 ) == LUA_TBOOLEAN ? lua_toboolean( m_state, -1 ) != 0 : defval;
+	lua_pop( m_state, 1 );
+	return result;
+}
+
+// state
 
 lua::state::state( lua_State* state ) : state_wrapper( state, false ), m_type_database( nullptr )
@@ -135,128 +307,7 @@
 }
 
-lua::state_wrapper::~state_wrapper()
-{
-	if (m_owner)
-	{
-		lua_close( m_state );
-	}
-}
-
 int lua::state::get_stack_size()
 {
 	return lua_gettop( m_state );
-}
-
-lua::table_guard::table_guard( lua::state* lstate, const path& p, bool global )
-	: state_wrapper( lstate->get_raw(), false ), m_level(0)
-{
-	m_global = false;
-	m_level  = lua_gettop( m_state );
-	if ( !p.resolve( m_state, global ) )
-	{
-		// TODO : error handling
-	}
-}
-
-lua::table_guard::table_guard( const table_guard& parent, const path& p )
-	: state_wrapper( parent.m_state, false ), m_level(0)
-{
-	m_global = false;
-	m_level  = lua_gettop( m_state );
-	if ( !p.resolve( m_state, false ) )
-	{
-		// TODO : error handling
-	}
-}
-
-size_t lua::table_guard::get_size()
-{
-	return lua_rawlen( m_state, -1 );
-}
-
-
-bool lua::table_guard::has_field( const string& element )
-{
-	lua_getfield( m_state, -1, element.c_str() );
-	bool result = lua_isnil( m_state, -1 );
-	lua_pop( m_state, 1 );
-	return result;
-}
-
-string lua::table_guard::get_string( const string& element, const string& defval /*= "" */ )
-{
-	lua_getfield( m_state, -1, element.c_str() );
-	string result( ( lua_type( m_state, -1 ) == LUA_TSTRING ) ? lua_tostring( m_state, -1 ) : defval );
-	lua_pop( m_state, 1 );
-	return result;
-}
-
-char lua::table_guard::get_char( const string& element, char defval /*= "" */ )
-{
-	lua_getfield( m_state, -1, element.c_str() );
-	char result = ( lua_type( m_state, -1 ) == LUA_TSTRING && lua_rawlen( m_state, -1 ) > 0 ) ? lua_tostring( m_state, -1 )[0] : defval;
-	lua_pop( m_state, 1 );
-	return result;
-}
-
-int lua::table_guard::get_integer( const string& element, int defval /*= "" */ )
-{
-	lua_getfield( m_state, -1, element.c_str() );
-	lua_Integer result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tointeger( m_state, -1 ) : defval;
-	lua_pop( m_state, 1 );
-	return static_cast< int >( result );
-}
-
-unsigned lua::table_guard::get_unsigned( const string& element, unsigned defval /*= "" */ )
-{
-	lua_getfield( m_state, -1, element.c_str() );
-	unsigned result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tounsigned( m_state, -1 ) : defval;
-	lua_pop( m_state, 1 );
-	return result;
-}
-
-double lua::table_guard::get_double( const string& element, double defval /*= "" */ )
-{
-	lua_getfield( m_state, -1, element.c_str() );
-	double result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tonumber( m_state, -1 ) : defval;
-	lua_pop( m_state, 1 );
-	return result;
-}
-
-bool lua::table_guard::get_boolean( const string& element, bool defval /*= "" */ )
-{
-	lua_getfield( m_state, -1, element.c_str() );
-	bool result = lua_type( m_state, -1 ) == LUA_TBOOLEAN ? lua_toboolean( m_state, -1 ) != 0 : defval;
-	lua_pop( m_state, 1 );
-	return result;
-}
-
-void nv::lua::state_wrapper::push_global_table()
-{
-	lua_pushglobaltable( m_state );
-}
-
-void nv::lua::state_wrapper::pop_global_table()
-{
-	lua_replace( m_state, -2 );
-}
-
-void lua::state_wrapper::call_get()
-{
-	lua_gettable( m_state, -2 );
-}
-
-void lua::state_wrapper::call_get_raw()
-{
-	lua_rawget( m_state, -2 );
-}
-
-
-void lua::state_wrapper::register_native_function( lfunction f, const char* name )
-{
-	if ( m_global ) push_global_table();
-	lua_pushcfunction( m_state, f );
-	lua_setfield( m_state, -2, name );
-	if ( m_global ) pop_global_table();
 }
 
@@ -395,47 +446,2 @@
 }
 
-bool nv::lua::state_wrapper::is_defined( const path& p, bool global )
-{
-	if ( !p.resolve( m_state, global ) )
-	{
-		return false;
-	}
-	bool result = !lua_isnil( m_state, -1 );
-	lua_pop( m_state, 1 );
-	return result;
-}
-
-bool nv::lua::state_wrapper::push_function( const path& p, bool global )
-{
-	if ( !p.resolve( m_state, global ) )
-	{
-		NV_LOG( LOG_ERROR, "Lua error : not a valid path - " + p.to_string() );
-		return false;
-	}
-
-	if ( !lua_isfunction( m_state, -1 ) ) 
-	{
-		lua_pop( m_state, 1 );
-		NV_LOG( LOG_ERROR, "Lua error : not a valid function - " + p.to_string() );
-		return false;
-	}
-	return true;
-}
-
-int nv::lua::state_wrapper::call_function( int nargs, int nresults )
-{
-	int status = lua_pcall( m_state, nargs, nresults, 0 );
-	if ( status != 0 )
-	{
-		std::string error = lua_tostring( m_state, -1 );
-		lua_pop( m_state, 1 );
-		NV_LOG( LOG_ERROR, "Lua error : " << error )
-	}
-	return status;
-}
-
-lua::table_guard::~table_guard()
-{
-	lua_settop( m_state, m_level );
-}
-
