Ignore:
Timestamp:
06/28/16 21:09:19 (9 years ago)
Author:
epyon
Message:
  • nv::random - support for different rng sources
  • nv::types - fixes and better support
  • nv::mesh_creator - full range transform
  • nv::context - buffer mask as separate type
  • nv::lua - initializations from lua::state instead of lua_State
  • nv::lua - initial support for rtti
File:
1 edited

Legend:

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

    r490 r503  
    99#include "nv/lua/lua_raw.hh"
    1010#include "nv/lua/lua_nova.hh"
     11#include "nv/lua/lua_types.hh"
    1112#include "nv/core/logging.hh"
    1213#include "nv/stl/string.hh"
     
    143144
    144145lua::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)
    146147{
    147148        m_global = false;
     
    155156
    156157lua::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)
    158159{
    159160        m_global = false;
     
    350351
    351352
     353bool 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
    352371// state
    353372
    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 )
     373lua::state::state( lua_State* state, type_database* types )
     374        : state_wrapper( state, new type_data( types ), false )
     375{
     376
     377}
     378
     379lua::state::state( bool load_libs /*= false*/, type_database* types )
     380        : state_wrapper( nullptr, new type_data( types ), true )
    360381{
    361382        load_lua_library();
     
    575596}
    576597
     598void 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
    577603nv::lua::ref nv::lua::state::register_handle_component_impl( string_view id, bool empty )
    578604{
     
    626652}
    627653
     654nv::lua::state::~state()
     655{
     656        delete m_lua_types;
     657        m_lua_types = nullptr;
     658}
     659
     660template < typename T >
     661void 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
     667template < typename T >
     668void 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
     674template < typename T >
     675void 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
     681static 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
     687template < typename T >
     688bool 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
     699template < typename T >
     700bool 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
     711template < typename T >
     712bool 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
     723static 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
     735void 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
     741void 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.