- Timestamp:
- 09/09/13 23:29:37 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lua/lua_state.cc
r215 r216 15 15 using namespace nv; 16 16 17 // stack_guard 18 17 19 lua::stack_guard::stack_guard( lua::state* aL ) 18 20 : L(aL), m_level( lua_gettop(aL->m_state) ) … … 31 33 lua_settop( L->m_state, m_level ); 32 34 } 35 36 // state_wrapper 37 38 void nv::lua::state_wrapper::push_global_table() 39 { 40 lua_pushglobaltable( m_state ); 41 } 42 43 void nv::lua::state_wrapper::pop_global_table() 44 { 45 lua_replace( m_state, -2 ); 46 } 47 48 void lua::state_wrapper::call_get() 49 { 50 lua_gettable( m_state, -2 ); 51 } 52 53 void lua::state_wrapper::call_get_raw() 54 { 55 lua_rawget( m_state, -2 ); 56 } 57 58 void lua::state_wrapper::register_native_function( lfunction f, const char* name ) 59 { 60 if ( m_global ) push_global_table(); 61 lua_pushcfunction( m_state, f ); 62 lua_setfield( m_state, -2, name ); 63 if ( m_global ) pop_global_table(); 64 } 65 66 lua::state_wrapper::~state_wrapper() 67 { 68 if (m_owner) 69 { 70 lua_close( m_state ); 71 } 72 } 73 74 bool nv::lua::state_wrapper::is_defined( const path& p, bool global ) 75 { 76 if ( !p.resolve( m_state, global ) ) 77 { 78 return false; 79 } 80 bool result = !lua_isnil( m_state, -1 ); 81 lua_pop( m_state, 1 ); 82 return result; 83 } 84 85 bool nv::lua::state_wrapper::push_function( const path& p, bool global ) 86 { 87 if ( !p.resolve( m_state, global ) ) 88 { 89 NV_LOG( LOG_ERROR, "Lua error : not a valid path - " + p.to_string() ); 90 return false; 91 } 92 93 if ( !lua_isfunction( m_state, -1 ) ) 94 { 95 lua_pop( m_state, 1 ); 96 NV_LOG( LOG_ERROR, "Lua error : not a valid function - " + p.to_string() ); 97 return false; 98 } 99 return true; 100 } 101 102 int nv::lua::state_wrapper::call_function( int nargs, int nresults ) 103 { 104 int status = lua_pcall( m_state, nargs, nresults, 0 ); 105 if ( status != 0 ) 106 { 107 std::string error = lua_tostring( m_state, -1 ); 108 lua_pop( m_state, 1 ); 109 NV_LOG( LOG_ERROR, "Lua error : " << error ) 110 } 111 return status; 112 } 113 114 // table_guard 115 116 lua::table_guard::table_guard( lua::state* lstate, const path& p, bool global ) 117 : state_wrapper( lstate->get_raw(), false ), m_level(0) 118 { 119 m_global = false; 120 m_level = lua_gettop( m_state ); 121 if ( !p.resolve( m_state, global ) ) 122 { 123 // TODO : error handling 124 } 125 } 126 127 lua::table_guard::table_guard( const table_guard& parent, const path& p ) 128 : state_wrapper( parent.m_state, false ), m_level(0) 129 { 130 m_global = false; 131 m_level = lua_gettop( m_state ); 132 if ( !p.resolve( m_state, false ) ) 133 { 134 // TODO : error handling 135 } 136 } 137 138 lua::table_guard::~table_guard() 139 { 140 lua_settop( m_state, m_level ); 141 } 142 143 size_t lua::table_guard::get_size() 144 { 145 return lua_rawlen( m_state, -1 ); 146 } 147 148 bool lua::table_guard::has_field( const string& element ) 149 { 150 lua_getfield( m_state, -1, element.c_str() ); 151 bool result = lua_isnil( m_state, -1 ); 152 lua_pop( m_state, 1 ); 153 return result; 154 } 155 156 string lua::table_guard::get_string( const string& element, const string& defval /*= "" */ ) 157 { 158 lua_getfield( m_state, -1, element.c_str() ); 159 string result( ( lua_type( m_state, -1 ) == LUA_TSTRING ) ? lua_tostring( m_state, -1 ) : defval ); 160 lua_pop( m_state, 1 ); 161 return result; 162 } 163 164 char lua::table_guard::get_char( const string& element, char defval /*= "" */ ) 165 { 166 lua_getfield( m_state, -1, element.c_str() ); 167 char result = ( lua_type( m_state, -1 ) == LUA_TSTRING && lua_rawlen( m_state, -1 ) > 0 ) ? lua_tostring( m_state, -1 )[0] : defval; 168 lua_pop( m_state, 1 ); 169 return result; 170 } 171 172 int lua::table_guard::get_integer( const string& element, int defval /*= "" */ ) 173 { 174 lua_getfield( m_state, -1, element.c_str() ); 175 lua_Integer result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tointeger( m_state, -1 ) : defval; 176 lua_pop( m_state, 1 ); 177 return static_cast< int >( result ); 178 } 179 180 unsigned lua::table_guard::get_unsigned( const string& element, unsigned defval /*= "" */ ) 181 { 182 lua_getfield( m_state, -1, element.c_str() ); 183 unsigned result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tounsigned( m_state, -1 ) : defval; 184 lua_pop( m_state, 1 ); 185 return result; 186 } 187 188 double lua::table_guard::get_double( const string& element, double defval /*= "" */ ) 189 { 190 lua_getfield( m_state, -1, element.c_str() ); 191 double result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tonumber( m_state, -1 ) : defval; 192 lua_pop( m_state, 1 ); 193 return result; 194 } 195 196 bool lua::table_guard::get_boolean( const string& element, bool defval /*= "" */ ) 197 { 198 lua_getfield( m_state, -1, element.c_str() ); 199 bool result = lua_type( m_state, -1 ) == LUA_TBOOLEAN ? lua_toboolean( m_state, -1 ) != 0 : defval; 200 lua_pop( m_state, 1 ); 201 return result; 202 } 203 204 // state 33 205 34 206 lua::state::state( lua_State* state ) : state_wrapper( state, false ), m_type_database( nullptr ) … … 135 307 } 136 308 137 lua::state_wrapper::~state_wrapper()138 {139 if (m_owner)140 {141 lua_close( m_state );142 }143 }144 145 309 int lua::state::get_stack_size() 146 310 { 147 311 return lua_gettop( m_state ); 148 }149 150 lua::table_guard::table_guard( lua::state* lstate, const path& p, bool global )151 : state_wrapper( lstate->get_raw(), false ), m_level(0)152 {153 m_global = false;154 m_level = lua_gettop( m_state );155 if ( !p.resolve( m_state, global ) )156 {157 // TODO : error handling158 }159 }160 161 lua::table_guard::table_guard( const table_guard& parent, const path& p )162 : state_wrapper( parent.m_state, false ), m_level(0)163 {164 m_global = false;165 m_level = lua_gettop( m_state );166 if ( !p.resolve( m_state, false ) )167 {168 // TODO : error handling169 }170 }171 172 size_t lua::table_guard::get_size()173 {174 return lua_rawlen( m_state, -1 );175 }176 177 178 bool lua::table_guard::has_field( const string& element )179 {180 lua_getfield( m_state, -1, element.c_str() );181 bool result = lua_isnil( m_state, -1 );182 lua_pop( m_state, 1 );183 return result;184 }185 186 string lua::table_guard::get_string( const string& element, const string& defval /*= "" */ )187 {188 lua_getfield( m_state, -1, element.c_str() );189 string result( ( lua_type( m_state, -1 ) == LUA_TSTRING ) ? lua_tostring( m_state, -1 ) : defval );190 lua_pop( m_state, 1 );191 return result;192 }193 194 char lua::table_guard::get_char( const string& element, char defval /*= "" */ )195 {196 lua_getfield( m_state, -1, element.c_str() );197 char result = ( lua_type( m_state, -1 ) == LUA_TSTRING && lua_rawlen( m_state, -1 ) > 0 ) ? lua_tostring( m_state, -1 )[0] : defval;198 lua_pop( m_state, 1 );199 return result;200 }201 202 int lua::table_guard::get_integer( const string& element, int defval /*= "" */ )203 {204 lua_getfield( m_state, -1, element.c_str() );205 lua_Integer result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tointeger( m_state, -1 ) : defval;206 lua_pop( m_state, 1 );207 return static_cast< int >( result );208 }209 210 unsigned lua::table_guard::get_unsigned( const string& element, unsigned defval /*= "" */ )211 {212 lua_getfield( m_state, -1, element.c_str() );213 unsigned result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tounsigned( m_state, -1 ) : defval;214 lua_pop( m_state, 1 );215 return result;216 }217 218 double lua::table_guard::get_double( const string& element, double defval /*= "" */ )219 {220 lua_getfield( m_state, -1, element.c_str() );221 double result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tonumber( m_state, -1 ) : defval;222 lua_pop( m_state, 1 );223 return result;224 }225 226 bool lua::table_guard::get_boolean( const string& element, bool defval /*= "" */ )227 {228 lua_getfield( m_state, -1, element.c_str() );229 bool result = lua_type( m_state, -1 ) == LUA_TBOOLEAN ? lua_toboolean( m_state, -1 ) != 0 : defval;230 lua_pop( m_state, 1 );231 return result;232 }233 234 void nv::lua::state_wrapper::push_global_table()235 {236 lua_pushglobaltable( m_state );237 }238 239 void nv::lua::state_wrapper::pop_global_table()240 {241 lua_replace( m_state, -2 );242 }243 244 void lua::state_wrapper::call_get()245 {246 lua_gettable( m_state, -2 );247 }248 249 void lua::state_wrapper::call_get_raw()250 {251 lua_rawget( m_state, -2 );252 }253 254 255 void lua::state_wrapper::register_native_function( lfunction f, const char* name )256 {257 if ( m_global ) push_global_table();258 lua_pushcfunction( m_state, f );259 lua_setfield( m_state, -2, name );260 if ( m_global ) pop_global_table();261 312 } 262 313 … … 395 446 } 396 447 397 bool nv::lua::state_wrapper::is_defined( const path& p, bool global )398 {399 if ( !p.resolve( m_state, global ) )400 {401 return false;402 }403 bool result = !lua_isnil( m_state, -1 );404 lua_pop( m_state, 1 );405 return result;406 }407 408 bool nv::lua::state_wrapper::push_function( const path& p, bool global )409 {410 if ( !p.resolve( m_state, global ) )411 {412 NV_LOG( LOG_ERROR, "Lua error : not a valid path - " + p.to_string() );413 return false;414 }415 416 if ( !lua_isfunction( m_state, -1 ) )417 {418 lua_pop( m_state, 1 );419 NV_LOG( LOG_ERROR, "Lua error : not a valid function - " + p.to_string() );420 return false;421 }422 return true;423 }424 425 int nv::lua::state_wrapper::call_function( int nargs, int nresults )426 {427 int status = lua_pcall( m_state, nargs, nresults, 0 );428 if ( status != 0 )429 {430 std::string error = lua_tostring( m_state, -1 );431 lua_pop( m_state, 1 );432 NV_LOG( LOG_ERROR, "Lua error : " << error )433 }434 return status;435 }436 437 lua::table_guard::~table_guard()438 {439 lua_settop( m_state, m_level );440 }441
Note: See TracChangeset
for help on using the changeset viewer.