Changeset 503 for trunk/src/lua/lua_state.cc
- Timestamp:
- 06/28/16 21:09:19 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lua/lua_state.cc
r490 r503 9 9 #include "nv/lua/lua_raw.hh" 10 10 #include "nv/lua/lua_nova.hh" 11 #include "nv/lua/lua_types.hh" 11 12 #include "nv/core/logging.hh" 12 13 #include "nv/stl/string.hh" … … 143 144 144 145 lua::table_guard::table_guard( lua::state* lstate, const path& p, bool global ) 145 : state_wrapper( lstate->get_raw(), false ), m_level(0)146 : state_wrapper( lstate->get_raw(), lstate->m_lua_types, false ), m_parent( lstate ), m_level(0) 146 147 { 147 148 m_global = false; … … 155 156 156 157 lua::table_guard::table_guard( const table_guard& parent, const path& p ) 157 : state_wrapper( parent.m_state, false), m_level(0)158 : state_wrapper( parent.m_state, parent.m_lua_types, false ), m_parent( parent.m_parent ), m_level(0) 158 159 { 159 160 m_global = false; … … 350 351 351 352 353 bool nv::lua::table_guard::read( const string_view& element, const type_entry* entry, void* object ) 354 { 355 NV_ASSERT_ALWAYS( m_lua_types->get_type_database() == entry->type_db, "Type database mismatch between Lua and entry!" ); 356 lua_getfield( m_state, -1, element.data() ); 357 if ( lua_type( m_state, -1 ) != LUA_TTABLE ) 358 { 359 lua_pop( m_state, 1 ); 360 return false; 361 } 362 if ( !nv::lua::read_rtti_type( m_parent, entry, object, -1 ) ) 363 { 364 lua_pop( m_state, 1 ); 365 return false; 366 } 367 lua_pop( m_state, 1 ); 368 return true; 369 } 370 352 371 // state 353 372 354 lua::state::state( lua_State* state ) : state_wrapper( state, false ) 355 { 356 357 } 358 359 lua::state::state( bool load_libs /*= false*/ ) : state_wrapper( nullptr, true ) 373 lua::state::state( lua_State* state, type_database* types ) 374 : state_wrapper( state, new type_data( types ), false ) 375 { 376 377 } 378 379 lua::state::state( bool load_libs /*= false*/, type_database* types ) 380 : state_wrapper( nullptr, new type_data( types ), true ) 360 381 { 361 382 load_lua_library(); … … 575 596 } 576 597 598 void nv::lua::state::register_rtti_type( thash64 tid, lua_rtti_push_function p, lua_rtti_read_function r ) 599 { 600 m_lua_types->insert( tid, p, r ); 601 } 602 577 603 nv::lua::ref nv::lua::state::register_handle_component_impl( string_view id, bool empty ) 578 604 { … … 626 652 } 627 653 654 nv::lua::state::~state() 655 { 656 delete m_lua_types; 657 m_lua_types = nullptr; 658 } 659 660 template < typename T > 661 void nlua_rtti_signed_push( nv::lua::state* state, const type_entry*, void* object ) 662 { 663 T* value = reinterpret_cast<T*>( object ); 664 lua_pushinteger( state->get_raw(), lua_Integer( *value ) ); 665 } 666 667 template < typename T > 668 void nlua_rtti_unsigned_push( nv::lua::state* state, const type_entry*, void* object ) 669 { 670 T* value = reinterpret_cast<T*>( object ); 671 lua_pushinteger( state->get_raw(), lua_Unsigned( *value ) ); 672 } 673 674 template < typename T > 675 void nlua_rtti_floating_push( nv::lua::state* state, const type_entry*, void* object ) 676 { 677 T* value = reinterpret_cast< T* >( object ); 678 lua_pushnumber( state->get_raw(), lua_Number( *value ) ); 679 } 680 681 static void nlua_rtti_boolean_push( nv::lua::state* state, const type_entry*, void* object ) 682 { 683 bool* value = reinterpret_cast<bool*>( object ); 684 lua_pushboolean( state->get_raw(), *value ); 685 } 686 687 template < typename T > 688 bool nlua_rtti_signed_read( nv::lua::state* state, const type_entry*, void* object, int index ) 689 { 690 T* value = reinterpret_cast<T*>( object ); 691 if ( lua_type( state->get_raw(), index ) == LUA_TNUMBER ) 692 { 693 *value = T( lua_tointeger( state->get_raw(), index ) ); 694 return true; 695 } 696 return false; 697 } 698 699 template < typename T > 700 bool nlua_rtti_unsigned_read( nv::lua::state* state, const type_entry*, void* object, int index ) 701 { 702 T* value = reinterpret_cast<T*>( object ); 703 if ( lua_type( state->get_raw(), index ) == LUA_TNUMBER ) 704 { 705 *value = T( lua_tointeger( state->get_raw(), index ) ); 706 return true; 707 } 708 return false; 709 } 710 711 template < typename T > 712 bool nlua_rtti_floating_read( nv::lua::state* state, const type_entry*, void* object, int index ) 713 { 714 T* value = reinterpret_cast<T*>( object ); 715 if ( lua_type( state->get_raw(), index ) == LUA_TNUMBER ) 716 { 717 *value = T( lua_tonumber( state->get_raw(), index ) ); 718 return true; 719 } 720 return false; 721 } 722 723 static bool nlua_rtti_boolean_read( nv::lua::state* state, const type_entry*, void* object, int index ) 724 { 725 bool* value = reinterpret_cast<bool*>( object ); 726 if ( lua_type( state->get_raw(), index ) == LUA_TBOOLEAN ) 727 { 728 *value = bool( lua_toboolean( state->get_raw(), index ) ); 729 return true; 730 } 731 return false; 732 } 733 734 735 void nv::lua::type_data::insert( thash64 tid, lua_rtti_push_function p, lua_rtti_read_function r ) 736 { 737 m_type_read.assign( tid, r ); 738 m_type_push.assign( tid, p ); 739 } 740 741 void nv::lua::type_data::register_standard_types() 742 { 743 insert<bool>( nlua_rtti_boolean_push, nlua_rtti_boolean_read ); 744 insert<nv::sint8> ( nlua_rtti_signed_push<nv::sint8>, nlua_rtti_signed_read<nv::sint8> ); 745 insert<nv::sint16>( nlua_rtti_signed_push<nv::sint16>, nlua_rtti_signed_read<nv::sint16> ); 746 insert<nv::sint32>( nlua_rtti_signed_push<nv::sint32>, nlua_rtti_signed_read<nv::sint32> ); 747 insert<nv::uint8> ( nlua_rtti_unsigned_push<nv::uint8>, nlua_rtti_unsigned_read<nv::uint8> ); 748 insert<nv::uint16>( nlua_rtti_unsigned_push<nv::uint16>, nlua_rtti_unsigned_read<nv::uint16> ); 749 insert<nv::uint32>( nlua_rtti_unsigned_push<nv::uint32>, nlua_rtti_unsigned_read<nv::uint32> ); 750 insert<nv::f32> ( nlua_rtti_floating_push<nv::f32>, nlua_rtti_floating_read<nv::f32> ); 751 insert<nv::f64> ( nlua_rtti_floating_push<nv::f64>, nlua_rtti_floating_read<nv::f64> ); 752 // insert<nv::sint64>( nlua_rtti_floating_push<nv::sint64>, nlua_rtti_floating_read<nv::sint64> ); 753 // insert<nv::uint64>( nlua_rtti_floating_push<nv::uint64>, nlua_rtti_floating_read<nv::uint64> ); 754 }
Note: See TracChangeset
for help on using the changeset viewer.