Changeset 540 for trunk/src/lua


Ignore:
Timestamp:
01/25/17 20:20:45 (8 years ago)
Author:
epyon
Message:
  • lua::stack_proxy RTTI read support
  • missing RTTI declarations
  • new lua::table_guard syntax
Location:
trunk/src/lua
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lua/lua_proxy.cc

    r539 r540  
    99#include "nv/lua/lua_raw.hh"
    1010#include "nv/lua/lua_state.hh"
     11#include "nv/lua/lua_types.hh"
    1112
    1213using namespace nv;
     
    5859}
    5960
     61bool nv::lua::stack_proxy::is_valid() const
     62{
     63        return !( lua_isnil( *m_state, m_index ) );
     64}
     65
     66bool nv::lua::stack_proxy::read( const type_entry* entry, void* object ) const
     67{
     68        NV_ASSERT_ALWAYS( m_state->get_type_data()->get_type_database() == entry->type_db, "Type database mismatch between Lua and entry!" );
     69        if ( lua_type( *m_state, m_index ) != LUA_TTABLE ) return false;
     70        return nv::lua::read_rtti_type( m_state, entry, object, m_index );
     71}
     72
    6073bool nv::lua::stack_proxy::is_table() const
    6174{
  • trunk/src/lua/lua_state.cc

    r539 r540  
    153153                // TODO : error handling
    154154        }
     155        m_index = -1;
    155156}
    156157
     
    165166                // TODO : error handling
    166167        }
     168        m_index = -1;
    167169}
    168170
    169171lua::table_guard::~table_guard()
    170172{
    171         lua_settop( m_state, m_level );
    172 }
    173 
    174 nv::uint32 lua::table_guard::get_size()
    175 {
    176         return nlua_rawlen( m_state, -1 );
    177 }
    178 
    179 bool lua::table_guard::has_field( string_view element )
    180 {
    181         lua_getfield( m_state, -1, element.data() );
    182         bool result = !( lua_isnil( m_state, -1 ) );
    183         lua_pop( m_state, 1 );
    184         return result;
     173        if ( m_index == -1 )
     174                lua_settop( m_state, m_level );
     175}
     176
     177nv::uint32 lua::table_guard::size()
     178{
     179        return nlua_rawlen( m_state, m_index );
     180}
     181
     182nv::lua::table_guard::table_guard( stack_proxy& proxy )
     183        : state_wrapper( *proxy.m_state, proxy.m_state->m_lua_types, false ), m_parent( proxy.m_state ), m_level( 0 )
     184{
     185        m_level = lua_gettop( m_state );
     186        m_index = proxy.m_index;
    185187}
    186188
    187189const nv::lua::temporary_proxy nv::lua::table_guard::operator[]( const string_view& key ) const
    188190{
    189         lua_getfield( m_state, -1, key.data() );
     191        lua_getfield( m_state, m_index, key.data() );
    190192        return temporary_proxy( m_parent );
    191193}
     
    193195const nv::lua::temporary_proxy nv::lua::table_guard::operator[]( sint32 key ) const
    194196{
    195         lua_rawgeti( m_state, -1, key );
     197        lua_rawgeti( m_state, m_index, key );
    196198        return temporary_proxy( m_parent );
    197 }
    198 
    199 bool nv::lua::table_guard::read( const string_view& element, const type_entry* entry, void* object )
    200 {
    201         NV_ASSERT_ALWAYS( m_lua_types->get_type_database() == entry->type_db, "Type database mismatch between Lua and entry!" );
    202         lua_getfield( m_state, -1, element.data() );
    203         if ( lua_type( m_state, -1 ) != LUA_TTABLE )
    204         {
    205                 lua_pop( m_state, 1 );
    206                 return false;
    207         }
    208         if ( !nv::lua::read_rtti_type( m_parent, entry, object, -1 ) )
    209         {
    210                 lua_pop( m_state, 1 );
    211                 return false;
    212         }
    213         lua_pop( m_state, 1 );
    214         return true;
    215199}
    216200
     
    590574}
    591575
     576template < typename T >
     577bool nlua_rtti_string_read( nv::lua::state* state, const type_entry*, void* object, int index )
     578{
     579        T* value = reinterpret_cast<T*>( object );
     580        if ( lua_type( state->get_raw(), index ) == LUA_TSTRING )
     581        {
     582                *value = T( nlua_tostringview( state->get_raw(), index ) );
     583                return true;
     584        }
     585        return false;
     586}
     587
    592588
    593589void nv::lua::type_data::insert( thash64 tid, lua_rtti_push_function p, lua_rtti_read_function r )
     
    608604        insert<nv::f32>   ( nlua_rtti_floating_push<nv::f32>,    nlua_rtti_floating_read<nv::f32> );
    609605        insert<nv::f64>   ( nlua_rtti_floating_push<nv::f64>,    nlua_rtti_floating_read<nv::f64> );
    610 //      insert<nv::sint64>( nlua_rtti_floating_push<nv::sint64>, nlua_rtti_floating_read<nv::sint64> );
     606        insert<nv::shash32>  ( nullptr, nlua_rtti_string_read<nv::shash32> );
     607        insert<nv::shash64>  ( nullptr, nlua_rtti_string_read<nv::shash64> );
     608        insert<nv::string32> ( nullptr, nlua_rtti_string_read<nv::string32> );
     609        insert<nv::string64> ( nullptr, nlua_rtti_string_read<nv::string64> );
     610        insert<nv::string128>( nullptr, nlua_rtti_string_read<nv::string128> );
     611        //      insert<nv::sint64>( nlua_rtti_floating_push<nv::sint64>, nlua_rtti_floating_read<nv::sint64> );
    611612//      insert<nv::uint64>( nlua_rtti_floating_push<nv::uint64>, nlua_rtti_floating_read<nv::uint64> );
    612613}
Note: See TracChangeset for help on using the changeset viewer.