Changeset 262 for trunk/src/lua
- Timestamp:
- 06/19/14 00:02:55 (11 years ago)
- Location:
- trunk/src/lua
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lua/lua_state.cc
r256 r262 11 11 #include "nv/logging.hh" 12 12 #include "nv/string.hh" 13 #include "nv/root.hh"14 #include "nv/types.hh"15 13 16 14 using namespace nv; … … 205 203 // state 206 204 207 lua::state::state( lua_State* state ) : state_wrapper( state, false ) , m_type_database( nullptr )208 { 209 210 } 211 212 lua::state::state( bool load_libs /*= false*/ ) : state_wrapper( nullptr, true ) , m_type_database( nullptr )205 lua::state::state( lua_State* state ) : state_wrapper( state, false ) 206 { 207 208 } 209 210 lua::state::state( bool load_libs /*= false*/ ) : state_wrapper( nullptr, true ) 213 211 { 214 212 load_lua_library(); … … 222 220 if ( load_libs ) 223 221 { 224 m_type_database = new type_database();225 222 stack_guard guard( this ); 226 223 static const luaL_Reg lualibs[] = … … 331 328 } 332 329 333 lua::reference lua::state::register_object( object* o, const char* lua_name )330 lua::reference lua::state::register_object( void* o, const char* lua_name ) 334 331 { 335 332 if ( o == nullptr ) return ref_none; … … 344 341 } 345 342 346 lua::reference lua::state::register_proto( object * o, const char* storage )343 lua::reference lua::state::register_proto( const char* id, const char* storage ) 347 344 { 348 345 stack_guard guard( this ); … … 352 349 NV_THROW( runtime_error, std::string( storage ) + " storage not registered!" ); 353 350 } 354 lua_getfield( m_state, -1, o->get_id().c_str());351 lua_getfield( m_state, -1, id ); 355 352 if ( lua_isnil( m_state, -1 ) ) 356 353 { 357 NV_THROW( runtime_error, std::string( o->get_id()) + " not found in " + std::string( storage ) + " storage!" );354 NV_THROW( runtime_error, std::string( id ) + " not found in " + std::string( storage ) + " storage!" ); 358 355 } 359 356 return luaL_ref( m_state, LUA_REGISTRYINDEX ); 360 }361 362 lua::reference lua::state::register_object( object * o )363 {364 if ( o == nullptr || m_type_database == nullptr ) return ref_none;365 type_entry* t = m_type_database->get_type(typeid(*o));366 if ( t == nullptr ) return ref_none;367 return register_object( o, t->name.c_str() );368 357 } 369 358 … … 380 369 } 381 370 382 void lua::state::unregister_object( object * o ) 383 { 384 if (!o) return; 385 unregister_object( o->get_lua_index() ); 386 } 387 388 void lua::state::unregister_object( int index ) 389 { 371 void lua::state::unregister_object( reference object_index ) 372 { 373 if ( object_index == ref_nil ) return; 390 374 stack_guard guard( this ); 391 lua_rawgeti( m_state, LUA_REGISTRYINDEX, index );375 lua_rawgeti( m_state, LUA_REGISTRYINDEX, object_index ); 392 376 lua_pushstring( m_state, "__ptr" ); 393 377 lua_pushboolean( m_state, false ); 394 378 lua_rawset( m_state, -3 ); 395 379 lua_pop( m_state, 1 ); 396 luaL_unref( m_state, LUA_REGISTRYINDEX, index );380 luaL_unref( m_state, LUA_REGISTRYINDEX, object_index ); 397 381 } 398 382 … … 435 419 } 436 420 437 void lua::state::register_enum_values( const std::string& name, const std::string& prefix /*= std::string() */ ) 438 { 439 type_entry* et = m_type_database->get_type( name ); 440 441 for ( const auto& entry : et->enum_list ) 442 { 443 lua_pushinteger( m_state, entry.value ); 444 if ( prefix.empty() ) 445 { 446 lua_setglobal( m_state, entry.name.c_str() ); 447 } 448 else 449 { 450 lua_setglobal( m_state, ( prefix + entry.name ).c_str() ); 451 } 452 } 453 } 454 455 void nv::lua::state::store_metadata( object* o, const std::string& metaname, void* pointer ) 456 { 457 if (!o) return; 421 void nv::lua::state::store_metadata( reference object_index, const std::string& metaname, void* pointer ) 422 { 423 if ( object_index == ref_nil ) return; 458 424 stack_guard guard( this ); 459 lua_rawgeti( m_state, LUA_REGISTRYINDEX, o ->get_lua_index());425 lua_rawgeti( m_state, LUA_REGISTRYINDEX, object_index ); 460 426 lua_pushstring( m_state, metaname.c_str() ); 461 427 lua_pushlightuserdata( m_state, pointer ); … … 464 430 } 465 431 466 nv::lua::state::~state() 467 { 468 if (m_type_database != nullptr ) 469 { 470 delete m_type_database; 471 } 472 } 473 432 void nv::lua::state::register_enum( const char* name, int value ) 433 { 434 lua_pushinteger( m_state, value ); 435 lua_setglobal( m_state, name ); 436 } 437 -
trunk/src/lua/lua_values.cc
r213 r262 8 8 9 9 #include "nv/lua/lua_raw.hh" 10 #include "nv/object.hh"11 10 12 11 using nv::lua::linteger; … … 71 70 } 72 71 73 void nv::lua::detail::push_object ( lua_State *L, object* o )74 {75 if ( o == nullptr )76 {77 lua_pushnil( L );78 }79 else80 {81 lua_rawgeti( L, LUA_REGISTRYINDEX, o->get_lua_index() );82 }83 }84 85 72 void nv::lua::detail::push_pointer ( lua_State *L, void* p ) 86 73 { … … 118 105 } 119 106 120 nv::object* nv::lua::detail::to_object( lua_State *L, int index )107 void* nv::lua::detail::to_pointer ( lua_State *L, int index ) 121 108 { 122 object* o = nullptr; 109 return lua_touserdata( L, index ); 110 } 111 112 void* nv::lua::detail::to_ref_object ( lua_State *L, int index ) 113 { 114 void* o = nullptr; 123 115 if ( lua_istable( L , index ) ) 124 116 { … … 127 119 if ( lua_isuserdata( L, -1 ) ) 128 120 { 129 o = static_cast<object*>( lua_touserdata( L, -1 ));121 o = lua_touserdata( L, -1 ); 130 122 } 131 123 lua_pop( L, 1 ); 132 124 } 133 125 return o; 134 }135 136 void* nv::lua::detail::to_pointer ( lua_State *L, int index )137 {138 return lua_touserdata( L, index );139 126 } 140 127 … … 169 156 } 170 157 171 nv::object* nv::lua::detail::to_object ( lua_State *L, int index, nv::object* def )158 void* nv::lua::detail::to_pointer ( lua_State *L, int index, void* def ) 172 159 { 173 object* o = def; 160 return ( lua_type( L, index ) == LUA_TUSERDATA ? lua_touserdata( L, index ) : def ); 161 } 162 163 void* nv::lua::detail::to_ref_object( lua_State *L, int index, void* def ) 164 { 165 void* o = def; 174 166 if ( lua_istable( L , index ) ) 175 167 { … … 178 170 if ( lua_isuserdata( L, -1 ) ) 179 171 { 180 o = static_cast<object*>( lua_touserdata( L, -1 ));172 o = lua_touserdata( L, -1 ); 181 173 } 182 174 lua_pop( L, 1 ); … … 184 176 return o; 185 177 } 186 187 void* nv::lua::detail::to_pointer ( lua_State *L, int index, void* def )188 {189 return ( lua_type( L, index ) == LUA_TUSERDATA ? lua_touserdata( L, index ) : def );190 }
Note: See TracChangeset
for help on using the changeset viewer.