Changeset 503 for trunk/src/lua


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
Location:
trunk/src/lua
Files:
1 added
6 edited

Legend:

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

    r491 r503  
    459459}
    460460
    461 void nv::lua::register_area( lua_State* L )
    462 {
    463         int stack = lua_gettop( L );
    464         nlua_requiref(L, "area", luaopen_area, 1);
    465         lua_settop( L, stack );
    466 }
    467 
     461void nv::lua::register_area( lua::state* state )
     462{
     463        int stack = lua_gettop( state->get_raw() );
     464        nlua_requiref( state->get_raw(), "area", luaopen_area, 1);
     465        lua_settop( state->get_raw(), stack );
     466}
     467
  • trunk/src/lua/lua_aux.cc

    r490 r503  
    137137};
    138138
    139 void nv::lua::register_aux( lua_State* L )
     139void nv::lua::register_aux( lua::state* state )
    140140{
    141         nlua_register( L, "table", nluaaux_table_aux_f );
    142         nlua_register( L, "math", nluaaux_math_aux_f );
     141        nlua_register( state->get_raw(), "table", nluaaux_table_aux_f );
     142        nlua_register( state->get_raw(), "math", nluaaux_math_aux_f );
    143143}
    144144
  • trunk/src/lua/lua_map_area.cc

    r490 r503  
    223223}
    224224
    225 void nv::lua::register_map_area_interface( lua_State* L, int index )
    226 {
    227         nlua_register( L, nlua_map_area_f, index );
    228 }
    229 
    230 void nv::lua::register_map_area( lua_State* L )
    231 {
    232         luaopen_map_area(L);
    233 }
    234 
    235 void nv::lua::register_map_area_instance( lua_State* L, ref object_index, map_area* area )
    236 {
     225void nv::lua::register_map_area_interface( lua::state* state, int index )
     226{
     227        nlua_register( state->get_raw(), nlua_map_area_f, index );
     228}
     229
     230void nv::lua::register_map_area( lua::state* state )
     231{
     232        luaopen_map_area( state->get_raw() );
     233}
     234
     235void nv::lua::register_map_area_instance( lua::state* state, ref object_index, map_area* area )
     236{
     237        lua_State* L = state->get_raw();
    237238        lua_rawgeti( L, LUA_REGISTRYINDEX, object_index.get() );
    238239        lua_pushliteral( L, "__map_area_ptr" );
  • trunk/src/lua/lua_map_tile.cc

    r490 r503  
    366366};
    367367
    368 void nv::lua::register_map_tile( lua_State * L )
     368void nv::lua::register_map_tile( lua::state* state )
    369369{
    370370        // TODO: check if __gc is used!
     371        lua_State* L = state->get_raw();
    371372        luaL_newmetatable( L, NLUA_MAP_TILE_METATABLE );
    372373        lua_pushvalue( L, -1 );
  • trunk/src/lua/lua_math.cc

    r490 r503  
    359359}
    360360
    361 void nv::lua::register_math( lua_State* L )
     361template < typename T >
     362void nlua_rtti_vec_push( nv::lua::state* state, const nv::type_entry*, void* object )
     363{
     364        T* value = reinterpret_cast<T*>( object );
     365        push_vec<T>( state->get_raw(), *value );
     366}
     367
     368template < typename T >
     369bool nlua_rtti_vec_read( nv::lua::state* state, const nv::type_entry*, void* object, int index )
     370{
     371        T* value = reinterpret_cast<T*>( object );
     372        int type = lua_type( state->get_raw(), index );
     373        if ( type == LUA_TUSERDATA )
     374        {
     375                T* from = to_pvec<T>( state->get_raw(), index );
     376                if ( !from ) return false;
     377                *value = *from;
     378        }
     379//      else if ( type == LUA_TTABLE )
     380//      {
     381//
     382//      }
     383        else
     384                return false;
     385        int todo; int table_constructor;
     386        return true;
     387}
     388
     389void nv::lua::register_math( lua::state* state )
    362390{
    363391        for (size_t i = 0; i < 256; ++i ) nlua_swizzel_lookup[i] = 255;
     
    379407        nlua_swizzel_lookup[uchar8( 'v' )] = 0;
    380408        nlua_swizzel_lookup[uchar8( '3' )] = 3;
     409
     410        lua_State* L = state->get_raw();
    381411        int stack = lua_gettop( L );
    382412
     
    389419        nlua_requiref(L, "vec4", luaopen_vec<nv::vec4>, 1);
    390420        lua_settop( L, stack );
    391 }
    392 
     421
     422        state->register_rtti_type< nv::ivec2 >( nlua_rtti_vec_push<nv::ivec2>, nlua_rtti_vec_read<nv::ivec2> );
     423        state->register_rtti_type< nv::ivec3 >( nlua_rtti_vec_push<nv::ivec3>, nlua_rtti_vec_read<nv::ivec3> );
     424        state->register_rtti_type< nv::ivec4 >( nlua_rtti_vec_push<nv::ivec4>, nlua_rtti_vec_read<nv::ivec4> );
     425        state->register_rtti_type< nv::vec2 > ( nlua_rtti_vec_push<nv::vec2>,  nlua_rtti_vec_read<nv::vec2> );
     426        state->register_rtti_type< nv::vec3 > ( nlua_rtti_vec_push<nv::vec3>,  nlua_rtti_vec_read<nv::vec3> );
     427        state->register_rtti_type< nv::vec4 > ( nlua_rtti_vec_push<nv::vec4>,  nlua_rtti_vec_read<nv::vec4> );
     428}
     429
  • 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.