- Timestamp:
- 08/19/13 06:37:47 (12 years ago)
- Location:
- trunk/src/lua
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lua/lua_area.cc
r204 r206 150 150 static int nlua_area_coords( lua_State* L ) 151 151 { 152 nv::rectangle* a( nlua_to_parea( L, lua_upvalueindex(1)) );152 nv::rectangle* a( nlua_to_parea( L, 1 ) ); 153 153 nv::ivec2 c( a->ul ); 154 154 c.x--; … … 184 184 static int nlua_area_edges( lua_State* L ) 185 185 { 186 nv::rectangle* a( nlua_to_parea( L, lua_upvalueindex(1)) );186 nv::rectangle* a( nlua_to_parea( L, 1 ) ); 187 187 nv::ivec2 c( a->ul ); 188 188 c.x--; -
trunk/src/lua/lua_map_area.cc
r198 r206 9 9 #include "nv/lua/lua_area.hh" 10 10 #include "nv/lua/lua_glm.hh" 11 #include "nv/lua/lua_values.hh" 11 12 #include "nv/lua/lua_raw.hh" 12 13 … … 53 54 nv::map_area* nlua_to_map_area( lua_State* L, int index ) 54 55 { 55 return *(nv::map_area**)luaL_checkudata( L, index, NLUA_MAP_AREA_METATABLE ); 56 if ( lua_type( L, index ) == LUA_TTABLE ) 57 { 58 nv::map_area* o = nullptr; 59 if ( lua_istable( L , index ) ) 60 { 61 lua_pushstring( L, "__map_area_ptr" ); 62 lua_rawget( L, index ); 63 if ( lua_isuserdata( L, -1 ) ) 64 { 65 o = static_cast<nv::map_area*>( lua_touserdata( L, -1 ) ); 66 } 67 lua_pop( L, 1 ); 68 } 69 return o; 70 } 71 else 72 { 73 return *(nv::map_area**)luaL_checkudata( L, index, NLUA_MAP_AREA_METATABLE ); 74 } 56 75 } 57 76 … … 83 102 { 84 103 nv::map_area* ma = nlua_to_map_area( L, 1 ); 85 nlua_push_area( L, ma->get_rectangle() ); 104 nv::rectangle r = ma->get_rectangle(); 105 r.lr.x -= 1; 106 r.lr.y -= 1; 107 nlua_push_area( L, r ); 86 108 return 1; 87 109 } -
trunk/src/lua/lua_state.cc
r204 r206 16 16 17 17 lua::stack_guard::stack_guard( lua::state* aL ) 18 : L(aL), m_level( lua_gettop(aL-> L) )18 : L(aL), m_level( lua_gettop(aL->m_state) ) 19 19 { 20 20 … … 22 22 23 23 lua::stack_guard::stack_guard( lua::state& aL ) 24 : L(&aL), m_level( lua_gettop(aL. L) )24 : L(&aL), m_level( lua_gettop(aL.m_state) ) 25 25 { 26 26 … … 29 29 lua::stack_guard::~stack_guard() 30 30 { 31 lua_settop( L->L, m_level ); 32 } 33 34 lua::state::state( bool load_libs /*= false*/ ) 31 lua_settop( L->m_state, m_level ); 32 } 33 34 lua::state::state( lua_State* state ) : state_wrapper( state, false ) 35 { 36 37 } 38 39 lua::state::state( bool load_libs /*= false*/ ) : state_wrapper( nullptr, true ) 35 40 { 36 41 load_lua_library(); 37 42 m_owner = true; 38 L= luaL_newstate( );39 40 lua_pushcfunction( L, luaopen_base);41 lua_pushliteral( L, LUA_TABLIBNAME);42 lua_call( L, 1, 0);43 m_state = luaL_newstate( ); 44 45 lua_pushcfunction(m_state, luaopen_base); 46 lua_pushliteral(m_state, LUA_TABLIBNAME); 47 lua_call(m_state, 1, 0); 43 48 44 49 if ( load_libs ) … … 55 60 for(; lib->func != NULL; lib++) 56 61 { 57 lib->func( L);62 lib->func( m_state ); 58 63 } 59 64 } … … 65 70 { 66 71 NV_LOG( nv::LOG_TRACE, "Loading Lua string '" << name << "'"); 67 return luaL_loadbuffer( L, code.c_str(), code.length(), name.c_str() );72 return luaL_loadbuffer( m_state, code.c_str(), code.length(), name.c_str() ); 68 73 } 69 74 … … 79 84 { 80 85 NV_LOG( nv::LOG_NOTICE, "Loading Lua file '" << filename << "'"); 81 return luaL_loadfile( L, filename.c_str() );86 return luaL_loadfile( m_state, filename.c_str() ); 82 87 } 83 88 … … 88 93 if (result) 89 94 { 90 NV_LOG( nv::LOG_WARNING, "Failed to load string " << name << ": " << lua_tostring( L, -1));95 NV_LOG( nv::LOG_WARNING, "Failed to load string " << name << ": " << lua_tostring(m_state, -1)); 91 96 return false; 92 97 } … … 100 105 if (result) 101 106 { 102 NV_LOG( nv::LOG_WARNING, "Failed to open stream " << name << ": " << lua_tostring( L, -1));107 NV_LOG( nv::LOG_WARNING, "Failed to open stream " << name << ": " << lua_tostring(m_state, -1)); 103 108 return false; 104 109 } … … 112 117 if (result) 113 118 { 114 NV_LOG( nv::LOG_WARNING, "Failed to open file " << filename << ": " << lua_tostring( L, -1));119 NV_LOG( nv::LOG_WARNING, "Failed to open file " << filename << ": " << lua_tostring(m_state, -1)); 115 120 return false; 116 121 } … … 120 125 int lua::state::do_current( const std::string& name, int rvalues ) 121 126 { 122 int result = lua_pcall( L, 0, rvalues, 0);127 int result = lua_pcall(m_state, 0, rvalues, 0); 123 128 if (result) 124 129 { 125 NV_LOG( nv::LOG_WARNING, "Failed to run script " << name << ": " << lua_tostring( L, -1));126 lua_pop( L, 1 );127 } 128 return result; 129 } 130 131 lua::state ::~state()130 NV_LOG( nv::LOG_WARNING, "Failed to run script " << name << ": " << lua_tostring(m_state, -1)); 131 lua_pop( m_state, 1 ); 132 } 133 return result; 134 } 135 136 lua::state_wrapper::~state_wrapper() 132 137 { 133 138 if (m_owner) 134 139 { 135 lua_close( L);140 lua_close( m_state ); 136 141 } 137 142 } … … 145 150 if (global) 146 151 { 147 lua_getglobal( L, path.c_str() );152 lua_getglobal( m_state, path.c_str() ); 148 153 } 149 154 else 150 155 { 151 lua_getfield( L, -1, path.c_str() );152 } 153 return !lua_isnil( L, -1 );156 lua_getfield( m_state, -1, path.c_str() ); 157 } 158 return !lua_isnil( m_state, -1 ); 154 159 } 155 160 … … 163 168 if (global) 164 169 { 165 lua_getglobal( L, path.substr(start,point-start).c_str() );170 lua_getglobal( m_state, path.substr(start,point-start).c_str() ); 166 171 } 167 172 else 168 173 { 169 lua_getfield( L, -1, path.substr(start,point-start).c_str() );174 lua_getfield( m_state, -1, path.substr(start,point-start).c_str() ); 170 175 } 171 176 } 172 177 else 173 178 { 174 if ( lua_istable( L, -1 ) )179 if ( lua_istable( m_state, -1 ) ) 175 180 { 176 lua_pushstring( L, path.substr(start,point-start).c_str() );177 lua_gettable( L, -2 );178 lua_insert( L, -2 );179 lua_pop( L, 1 );181 lua_pushstring( m_state, path.substr(start,point-start).c_str() ); 182 lua_gettable( m_state, -2 ); 183 lua_insert( m_state, -2 ); 184 lua_pop( m_state, 1 ); 180 185 } 181 186 else 182 187 { 183 lua_pop( L, 1);184 lua_pushnil( L);188 lua_pop(m_state, 1); 189 lua_pushnil(m_state); 185 190 return false; 186 191 } … … 195 200 int lua::state::get_stack_size() 196 201 { 197 return lua_gettop( L);202 return lua_gettop( m_state ); 198 203 } 199 204 200 205 lua::table_guard::table_guard( lua::state* lstate, const path& p, bool global ) 201 : L(lstate), m_guard(lstate) 202 { 203 if ( !p.resolve( L->get_raw(), global ) ) 206 : state_wrapper( lstate->get_raw(), false ), m_level(0) 207 { 208 m_global = false; 209 m_level = lua_gettop( m_state ); 210 if ( !p.resolve( m_state, global ) ) 204 211 { 205 212 // TODO : error handling … … 208 215 209 216 lua::table_guard::table_guard( const table_guard& parent, const path& p ) 210 : L( parent.L ), m_guard( parent.L ) 211 { 212 if ( !p.resolve( L->get_raw(), false ) ) 217 : state_wrapper( parent.m_state, false ), m_level(0) 218 { 219 m_global = false; 220 m_level = lua_gettop( m_state ); 221 if ( !p.resolve( m_state, false ) ) 213 222 { 214 223 // TODO : error handling … … 218 227 size_t lua::table_guard::get_size() 219 228 { 220 return lua_rawlen( L->get_raw(), -1 );229 return lua_rawlen( m_state, -1 ); 221 230 } 222 231 … … 224 233 bool lua::table_guard::has_field( const string& element ) 225 234 { 226 lua_getfield( L->L, -1, element.c_str() );227 bool result = lua_isnil( L->L, -1 );228 lua_pop( L->L, 1 );235 lua_getfield( m_state, -1, element.c_str() ); 236 bool result = lua_isnil( m_state, -1 ); 237 lua_pop( m_state, 1 ); 229 238 return result; 230 239 } … … 232 241 string lua::table_guard::get_string( const string& element, const string& defval /*= "" */ ) 233 242 { 234 lua_getfield( L->L, -1, element.c_str() );235 string result( ( lua_type( L->L, -1 ) == LUA_TSTRING ) ? lua_tostring( L->L, -1 ) : defval );236 lua_pop( L->L, 1 );243 lua_getfield( m_state, -1, element.c_str() ); 244 string result( ( lua_type( m_state, -1 ) == LUA_TSTRING ) ? lua_tostring( m_state, -1 ) : defval ); 245 lua_pop( m_state, 1 ); 237 246 return result; 238 247 } … … 240 249 char lua::table_guard::get_char( const string& element, char defval /*= "" */ ) 241 250 { 242 lua_getfield( L->L, -1, element.c_str() );243 char result = ( lua_type( L->L, -1 ) == LUA_TSTRING && lua_rawlen( L->L, -1 ) > 0 ) ? lua_tostring( L->L, -1 )[0] : defval;244 lua_pop( L->L, 1 );251 lua_getfield( m_state, -1, element.c_str() ); 252 char result = ( lua_type( m_state, -1 ) == LUA_TSTRING && lua_rawlen( m_state, -1 ) > 0 ) ? lua_tostring( m_state, -1 )[0] : defval; 253 lua_pop( m_state, 1 ); 245 254 return result; 246 255 } … … 248 257 int lua::table_guard::get_integer( const string& element, int defval /*= "" */ ) 249 258 { 250 lua_getfield( L->L, -1, element.c_str() );251 lua_Integer result = lua_type( L->L, -1 ) == LUA_TNUMBER ? lua_tointeger( L->L, -1 ) : defval;252 lua_pop( L->L, 1 );259 lua_getfield( m_state, -1, element.c_str() ); 260 lua_Integer result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tointeger( m_state, -1 ) : defval; 261 lua_pop( m_state, 1 ); 253 262 return static_cast< int >( result ); 254 263 } … … 256 265 unsigned lua::table_guard::get_unsigned( const string& element, unsigned defval /*= "" */ ) 257 266 { 258 lua_getfield( L->L, -1, element.c_str() );259 unsigned result = lua_type( L->L, -1 ) == LUA_TNUMBER ? lua_tounsigned( L->L, -1 ) : defval;260 lua_pop( L->L, 1 );267 lua_getfield( m_state, -1, element.c_str() ); 268 unsigned result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tounsigned( m_state, -1 ) : defval; 269 lua_pop( m_state, 1 ); 261 270 return result; 262 271 } … … 264 273 double lua::table_guard::get_double( const string& element, double defval /*= "" */ ) 265 274 { 266 lua_getfield( L->L, -1, element.c_str() );267 double result = lua_type( L->L, -1 ) == LUA_TNUMBER ? lua_tonumber( L->L, -1 ) : defval;268 lua_pop( L->L, 1 );275 lua_getfield( m_state, -1, element.c_str() ); 276 double result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tonumber( m_state, -1 ) : defval; 277 lua_pop( m_state, 1 ); 269 278 return result; 270 279 } … … 272 281 bool lua::table_guard::get_boolean( const string& element, bool defval /*= "" */ ) 273 282 { 274 lua_getfield( L->L, -1, element.c_str() ); 275 bool result = lua_type( L->L, -1 ) == LUA_TBOOLEAN ? lua_toboolean( L->L, -1 ) != 0 : defval; 276 lua_pop( L->L, 1 ); 277 return result; 278 } 279 280 void lua::table_guard::call_get() 281 { 282 lua_gettable( L->L, -2 ); 283 } 284 285 void lua::table_guard::call_get_raw() 286 { 287 lua_rawget( L->L, -2 ); 288 } 289 290 bool nv::lua::table_guard::is_defined( const path& p ) 291 { 292 return L->is_defined( p, false ); 283 lua_getfield( m_state, -1, element.c_str() ); 284 bool result = lua_type( m_state, -1 ) == LUA_TBOOLEAN ? lua_toboolean( m_state, -1 ) != 0 : defval; 285 lua_pop( m_state, 1 ); 286 return result; 287 } 288 289 void nv::lua::state_wrapper::push_global_table() 290 { 291 lua_pushglobaltable( m_state ); 292 } 293 294 void nv::lua::state_wrapper::pop_global_table() 295 { 296 lua_replace( m_state, -2 ); 297 } 298 299 void lua::state_wrapper::call_get() 300 { 301 lua_gettable( m_state, -2 ); 302 } 303 304 void lua::state_wrapper::call_get_raw() 305 { 306 lua_rawget( m_state, -2 ); 293 307 } 294 308 295 309 void lua::table_guard::get_raw_flags( const std::string& element, uint8* data, uint32 count ) 296 310 { 297 lua_getfield( L->L, -1, element.c_str() );298 if ( lua_type( L->L, -1 ) != LUA_TTABLE )299 { 300 lua_pop( L->L, 1 );311 lua_getfield( m_state, -1, element.c_str() ); 312 if ( lua_type( m_state, -1 ) != LUA_TTABLE ) 313 { 314 lua_pop( m_state, 1 ); 301 315 return; 302 316 } 303 nlua_toflags( L->L, -1, data, count );304 lua_pop( L->L, 1 );317 nlua_toflags( m_state, -1, data, count ); 318 lua_pop( m_state, 1 ); 305 319 } 306 320 … … 308 322 void lua::state::log_stack() 309 323 { 310 int top = lua_gettop( L);324 int top = lua_gettop(m_state); 311 325 NV_LOG( LOG_DEBUG, "Stack dump (" << top << ")"); 312 326 for ( int i = 0; i < top; ++i ) 313 327 { 314 NV_LOG( LOG_DEBUG, "#" << i+1 << " - " << lua_typename( L, lua_type(L, i+1) ) << " = " << nlua_typecontent(L, i+1) );328 NV_LOG( LOG_DEBUG, "#" << i+1 << " - " << lua_typename(m_state, lua_type(m_state, i+1) ) << " = " << nlua_typecontent(m_state, i+1) ); 315 329 } 316 330 } … … 318 332 lua_State* lua::state::get_raw() 319 333 { 320 return L;334 return m_state; 321 335 } 322 336 … … 325 339 if ( o == nullptr ) return ref_none; 326 340 stack_guard guard( this ); 327 lua_getglobal( L, lua_name );328 if ( lua_isnil( L, -1 ) )341 lua_getglobal( m_state, lua_name ); 342 if ( lua_isnil( m_state, -1 ) ) 329 343 { 330 344 NV_THROW( runtime_error, std::string( lua_name ) + " type not registered!" ); 331 345 } 332 346 deep_pointer_copy( -1, o ); 333 return luaL_ref( L, LUA_REGISTRYINDEX );347 return luaL_ref( m_state, LUA_REGISTRYINDEX ); 334 348 } 335 349 … … 348 362 if (!o) return; 349 363 stack_guard guard( this ); 350 lua_rawgeti( L, LUA_REGISTRYINDEX, o->get_lua_index() );351 lua_pushstring( L, "__ptr" );352 lua_pushboolean( L, false );353 lua_rawset( L, -3 );354 lua_pop( L, 1 );355 luaL_unref( L, LUA_REGISTRYINDEX, o->get_lua_index() );364 lua_rawgeti( m_state, LUA_REGISTRYINDEX, o->get_lua_index() ); 365 lua_pushstring( m_state, "__ptr" ); 366 lua_pushboolean( m_state, false ); 367 lua_rawset( m_state, -3 ); 368 lua_pop( m_state, 1 ); 369 luaL_unref( m_state, LUA_REGISTRYINDEX, o->get_lua_index() ); 356 370 } 357 371 358 372 void lua::state::deep_pointer_copy( int index, void* obj ) 359 373 { 360 index = lua_absindex( L, index );361 lua_newtable( L);362 lua_pushnil( L);374 index = lua_absindex( m_state, index ); 375 lua_newtable( m_state ); 376 lua_pushnil( m_state ); 363 377 bool has_functions = false; 364 378 bool has_metatable = false; 365 379 366 while ( lua_next( L, index ) != 0 )367 { 368 if ( lua_isfunction( L, -1 ) )380 while ( lua_next( m_state, index ) != 0 ) 381 { 382 if ( lua_isfunction( m_state, -1 ) ) 369 383 has_functions = true; 370 else if ( lua_istable( L, -1 ) )384 else if ( lua_istable( m_state, -1 ) ) 371 385 { 372 386 deep_pointer_copy( -1, obj ); 373 lua_insert( L, -2 );374 lua_pop( L, 1 );375 } 376 lua_pushvalue( L, -2 );377 lua_insert( L, -2 );378 lua_settable( L, -4 );379 } 380 381 if ( lua_getmetatable( L, -2 ) )382 { 383 lua_setmetatable( L, -2 );387 lua_insert( m_state, -2 ); 388 lua_pop( m_state, 1 ); 389 } 390 lua_pushvalue( m_state, -2 ); 391 lua_insert( m_state, -2 ); 392 lua_settable( m_state, -4 ); 393 } 394 395 if ( lua_getmetatable( m_state, -2 ) ) 396 { 397 lua_setmetatable( m_state, -2 ); 384 398 has_metatable = true; 385 399 } … … 387 401 if ( has_functions || has_metatable ) 388 402 { 389 lua_pushstring( L, "__ptr" );390 lua_pushlightuserdata( L, obj );391 lua_rawset( L, -3 );403 lua_pushstring( m_state, "__ptr" ); 404 lua_pushlightuserdata( m_state, obj ); 405 lua_rawset( m_state, -3 ); 392 406 } 393 407 } … … 399 413 for ( const auto& entry : et->enum_list ) 400 414 { 401 lua_pushinteger( L, entry.value );415 lua_pushinteger( m_state, entry.value ); 402 416 if ( prefix.empty() ) 403 417 { 404 lua_setglobal( L, entry.name.c_str() );418 lua_setglobal( m_state, entry.name.c_str() ); 405 419 } 406 420 else 407 421 { 408 lua_setglobal( L, ( prefix + entry.name ).c_str() ); 409 } 410 } 411 } 412 413 414 bool nv::lua::state::is_defined( const path& p, bool global ) 415 { 416 if ( !p.resolve( L, global ) ) 417 { 418 return false; 419 } 420 bool result = !lua_isnil( L, -1 ); 421 lua_pop( L, 1 ); 422 return result; 423 } 424 425 int lua::state::call_function( int nargs, int nresults ) 426 { 427 int status = lua_pcall( L, nargs, nresults, 0 ); 422 lua_setglobal( m_state, ( prefix + entry.name ).c_str() ); 423 } 424 } 425 } 426 427 void nv::lua::state::store_metadata( object* o, const std::string& metaname, void* pointer ) 428 { 429 if (!o) return; 430 stack_guard guard( this ); 431 lua_rawgeti( m_state, LUA_REGISTRYINDEX, o->get_lua_index() ); 432 lua_pushstring( m_state, metaname.c_str() ); 433 lua_pushlightuserdata( m_state, pointer ); 434 lua_rawset( m_state, -3 ); 435 lua_pop( m_state, 1 ); 436 } 437 438 bool nv::lua::state_wrapper::is_defined( const path& p, bool global ) 439 { 440 if ( !p.resolve( m_state, global ) ) 441 { 442 return false; 443 } 444 bool result = !lua_isnil( m_state, -1 ); 445 lua_pop( m_state, 1 ); 446 return result; 447 } 448 449 bool nv::lua::state_wrapper::push_function( const path& p, bool global ) 450 { 451 if ( !p.resolve( m_state, global ) ) 452 { 453 NV_LOG( LOG_ERROR, "Lua error : not a valid path - " + p.to_string() ); 454 return false; 455 } 456 457 if ( !lua_isfunction( m_state, -1 ) ) 458 { 459 lua_pop( m_state, 1 ); 460 NV_LOG( LOG_ERROR, "Lua error : not a valid function - " + p.to_string() ); 461 return false; 462 } 463 return true; 464 } 465 466 int nv::lua::state_wrapper::call_function( int nargs, int nresults ) 467 { 468 int status = lua_pcall( m_state, nargs, nresults, 0 ); 428 469 if ( status != 0 ) 429 470 { 430 std::string error = lua_tostring( L, -1 );431 lua_pop( L, 1 );471 std::string error = lua_tostring( m_state, -1 ); 472 lua_pop( m_state, 1 ); 432 473 NV_LOG( LOG_ERROR, "Lua error : " << error ) 433 474 } … … 435 476 } 436 477 437 bool lua::state::push_function( const path& p, bool global ) 438 { 439 if ( !p.resolve( L, global ) ) 440 { 441 NV_LOG( LOG_ERROR, "Lua error : not a valid path - " + p.to_string() ); 442 return false; 443 } 444 445 if ( !lua_isfunction( L, -1 ) ) 446 { 447 lua_pop( L, 1 ); 448 NV_LOG( LOG_ERROR, "Lua error : not a valid function - " + p.to_string() ); 449 return false; 450 } 451 return true; 452 } 453 478 lua::table_guard::~table_guard() 479 { 480 lua_settop( m_state, m_level ); 481 } -
trunk/src/lua/lua_values.cc
r183 r206 52 52 } 53 53 54 void nv::lua::detail::pop_value( lua_State *L, int& n ) 55 { 56 n = lua_tointeger( L, -1 ); 57 lua_pop( L, 1 ); 58 } 59 54 60 void nv::lua::detail::pop_value( lua_State *L, long& n ) 55 61 {
Note: See TracChangeset
for help on using the changeset viewer.