Changeset 206


Ignore:
Timestamp:
08/19/13 06:37:47 (12 years ago)
Author:
epyon
Message:
  • lua - major refactoring of the lua state/table
  • lua - added state_wrapper common to state/table
  • lua_values - fixed returning of simple types
  • lua_area - fixed area.coords and area.edges
  • lua_map_area - "fixed" case when object is used as base
Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/nv/lua/lua_state.hh

    r197 r206  
    3131                typedef int reference;
    3232
     33                class state_wrapper
     34                {
     35                public:
     36                        state_wrapper( lua_State* a_state, bool a_owner ) : m_state( a_state ), m_owner( a_owner ), m_global( true ) {}
     37                        virtual ~state_wrapper();
     38
     39                        template < typename R >
     40                        R call( const path& p )
     41                        {
     42                                if ( push_function( p, m_global ) )
     43                                {
     44                                        if ( call_function( 0, detail::return_count< R >::value ) == 0 )
     45                                        {
     46                                                return detail::pop_return_value<R>( m_state );
     47                                        }
     48                                }
     49                                return R();
     50                        }
     51                        template < typename R, typename T1 >
     52                        R call( const path& p, const T1& p1 )
     53                        {
     54                                if ( push_function( p, m_global ) )
     55                                {
     56                                        detail::push_value( m_state, p1 );
     57                                        if ( call_function( 1, detail::return_count< R >::value ) == 0 )
     58                                        {
     59                                                return detail::pop_return_value<R>( m_state );
     60                                        }
     61                                }
     62                                return R();
     63                        }
     64                        template < typename R, typename T1, typename T2 >
     65                        R call( const path& p, const T1& p1, const T2& p2 )
     66                        {
     67                                if ( push_function( p, m_global ) )
     68                                {
     69                                        detail::push_values( m_state, p1, p2 );
     70                                        if ( call_function( 2, detail::return_count< R >::value ) == 0 )
     71                                        {
     72                                                return detail::pop_return_value<R>( m_state );
     73                                        }
     74                                }
     75                                return R();
     76                        }
     77                        template < typename R, typename T1, typename T2, typename T3 >
     78                        R call( const path& p, const T1& p1, const T2& p2, const T3& p3 )
     79                        {
     80                                if ( push_function( p, m_global ) )
     81                                {
     82                                        detail::push_values( m_state, p1, p2, p3 );
     83                                        if ( call_function( 3, detail::return_count< R >::value ) == 0 )
     84                                        {
     85                                                return detail::pop_return_value<R>( m_state );
     86                                        }
     87                                }
     88                                return R();
     89                        }
     90                        template < typename R, typename T1, typename T2, typename T3, typename T4 >
     91                        R call( const path& p, const T1& p1, const T2& p2, const T3& p3, const T4& p4 )
     92                        {
     93                                if ( push_function( p, m_global ) )
     94                                {
     95                                        detail::push_value( m_state, p1, p2, p3, p4 );
     96                                        if ( call_function( 4, detail::return_count< R >::value ) == 0 )
     97                                        {
     98                                                return detail::pop_return_value<R>( m_state );
     99                                        }
     100                                }
     101                                return R();
     102                        }
     103
     104                        template< typename R >
     105                        R get( const path& p )
     106                        {
     107                                if ( p.resolve( m_state, m_global ) )
     108                                {
     109                                        return detail::pop_return_value<R>( m_state );
     110                                }
     111                                return R();
     112                        }
     113
     114                        template< typename R, typename T >
     115                        R get( const T& key )
     116                        {
     117                                if ( m_global ) push_global_table();
     118                                detail::push_value( m_state, key );
     119                                call_get();
     120                                if ( m_global ) pop_global_table();
     121                                return detail::pop_return_value<R>( m_state );
     122                        }
     123
     124                        template< typename R, typename T >
     125                        R raw_get( const T& key )
     126                        {
     127                                if ( m_global ) push_global_table();
     128                                detail::push_value( m_state, key );
     129                                call_get_raw();
     130                                if ( m_global ) pop_global_table();
     131                                return detail::pop_return_value<R>( m_state );
     132                        }
     133
     134                        bool is_defined( const path& p ) { return is_defined( p, m_global ); }
     135                protected:
     136                        bool is_defined( const path& p, bool global );
     137                        bool push_function( const path& p, bool global );
     138                        int call_function( int nargs, int nresults );
     139                private:
     140                        void push_global_table();
     141                        void pop_global_table();
     142                        void call_get();
     143                        void call_get_raw();
     144                protected:
     145                        lua_State* m_state;
     146                        bool m_owner;
     147                        bool m_global;
     148                };
     149
    33150                class stack_guard
    34151                {
     
    43160                };
    44161
     162
     163
    45164                class table_guard;
    46                 class state
     165                class state : public state_wrapper
    47166                {
    48167                        friend class stack_guard;
     
    56175                        int get_stack_size();
    57176                        void log_stack();
    58                         bool is_defined( const path& p, bool global = true );
    59177                        lua_State* get_raw();
    60178                        reference register_object( object * o );
    61179                        reference register_object( object * o, const char* lua_name );
     180                        void store_metadata( object* o, const std::string& metaname, void* pointer );
    62181                        void unregister_object( object * o );
    63182                        void register_enum( type_database* db, const std::string& name, const std::string& prefix = std::string() );
    64                         operator lua_State*() { return L; }
    65                         ~state();
    66 
    67                         template < typename R >
    68                         R call( const path& p )
    69                         {
    70                                 if ( push_function( p ) )
    71                                 {
    72                                         if ( call_function( 0, detail::return_count< R >::value ) == 0 )
    73                                         {
    74                                                 return detail::pop_return_value<R>( L );
    75                                         }
    76                                 }
    77                                 return R();
    78                         }
    79                         template < typename R, typename T1 >
    80                         R call( const path& p, const T1& p1 )
    81                         {
    82                                 if ( push_function( p ) )
    83                                 {
    84                                         detail::push_value( L, p1 );
    85                                         if ( call_function( 1, detail::return_count< R >::value ) == 0 )
    86                                         {
    87                                                 return detail::pop_return_value<R>( L );
    88                                         }
    89                                 }
    90                                 return R();
    91                         }
    92                         template < typename R, typename T1, typename T2 >
    93                         R call( const path& p, const T1& p1, const T2& p2 )
    94                         {
    95                                 if ( push_function( p ) )
    96                                 {
    97                                         detail::push_values( L, p1, p2 );
    98                                         if ( call_function( 2, detail::return_count< R >::value ) == 0 )
    99                                         {
    100                                                 return detail::pop_return_value<R>( L );
    101                                         }
    102                                 }
    103                                 return R();
    104                         }
    105                         template < typename R, typename T1, typename T2, typename T3 >
    106                         R call( const path& p, const T1& p1, const T2& p2, const T3& p3 )
    107                         {
    108                                 if ( push_function( p ) )
    109                                 {
    110                                         detail::push_values( L, p1, p2, p3 );
    111                                         if ( call_function( 3, detail::return_count< R >::value ) == 0 )
    112                                         {
    113                                                 return detail::pop_return_value<R>( L );
    114                                         }
    115                                 }
    116                                 return R();
    117                         }
    118                         template < typename R, typename T1, typename T2, typename T3, typename T4 >
    119                         R call( const path& p, const T1& p1, const T2& p2, const T3& p3, const T4& p4 )
    120                         {
    121                                 if ( push_function( p ) )
    122                                 {
    123                                         detail::push_value( L, p1, p2, p3, p4 );
    124                                         if ( call_function( 4, detail::return_count< R >::value ) == 0 )
    125                                         {
    126                                                 return detail::pop_return_value<R>( L );
    127                                         }
    128                                 }
    129                                 return R();
    130                         }
    131 
    132                 private:
    133                         bool push_function( const path& p, bool global = true );
    134                         int call_function( int nargs, int nresults );
     183                        operator lua_State*() { return m_state; }
     184                private:
    135185                        int load_string( const std::string& code, const std::string& name );
    136186                        int load_stream( std::istream& stream, const std::string& name );
    137187                        int load_file( const std::string& filename );
    138188                        int do_current( const std::string& name, int rvalues = 0 );
    139                         bool push( const std::string& path, bool global = true );
    140189                        void deep_pointer_copy( int index, void* obj );
    141                 private:
    142                         bool m_owner;
    143                         lua_State* L;
    144                 };
    145 
    146                 class table_guard
     190                };
     191
     192                class table_guard : public state_wrapper
    147193                {
    148194                public:
    149195                        table_guard( state* lstate, const path& p, bool global = true );
    150196                        table_guard( const table_guard& parent, const path& p );
     197                        virtual ~table_guard();
    151198                        size_t get_size();
    152199                        bool has_field( const std::string& element );
     
    157204                        double get_double( const std::string& element, double defval = 0.0 );
    158205                        bool get_boolean( const std::string& element, bool defval = false );
    159                         bool is_defined( const path& p );
    160 
    161                         template< typename R, typename T >
    162                         R get( const T& key )
    163                         {
    164                                 detail::push_value( L->L, key );
    165                                 call_get();
    166                                 return detail::pop_return_value<R>( L->L );
    167                         }
    168 
    169                         template< typename R, typename T >
    170                         R raw_get( const T& key )
    171                         {
    172                                 detail::push_value( L->L, key );
    173                                 call_get_raw();
    174                                 return detail::pop_return_value<R>( L->L );
    175                         }
    176206
    177207                        template< uint32 SIZE, typename T >
     
    189219                        }
    190220
    191                         template < typename R >
    192                         R call( const path& p )
    193                         {
    194                                 if ( L->push_function( p, false ) )
    195                                 {
    196                                         if ( call_function( 0, detail::return_count< R >::value ) == 0 )
    197                                         {
    198                                                 return detail::pop_return_value<R>( L );
    199                                         }
    200                                 }
    201                                 return R();
    202                         }
    203                         template < typename R, typename T1 >
    204                         R call( const path& p, const T1& p1 )
    205                         {
    206                                 if ( L->push_function( p, false ) )
    207                                 {
    208                                         detail::push_value( L, p1 );
    209                                         if ( L->call_function( 1, detail::return_count< R >::value ) == 0 )
    210                                         {
    211                                                 return detail::pop_return_value<R>( L );
    212                                         }
    213                                 }
    214                                 return R();
    215                         }
    216                         template < typename R, typename T1, typename T2 >
    217                         R call( const path& p, const T1& p1, const T2& p2 )
    218                         {
    219                                 if ( L->push_function( p, false ) )
    220                                 {
    221                                         detail::push_values( L, p1, p2 );
    222                                         if ( L->call_function( 2, detail::return_count< R >::value ) == 0 )
    223                                         {
    224                                                 return detail::pop_return_value<R>( L );
    225                                         }
    226                                 }
    227                                 return R();
    228                         }
    229                         template < typename R, typename T1, typename T2, typename T3 >
    230                         R call( const path& p, const T1& p1, const T2& p2, const T3& p3 )
    231                         {
    232                                 if ( L->push_function( p, false ) )
    233                                 {
    234                                         detail::push_values( L, p1, p2, p3 );
    235                                         if ( L->call_function( 3, detail::return_count< R >::value ) == 0 )
    236                                         {
    237                                                 return detail::pop_return_value<R>( L );
    238                                         }
    239                                 }
    240                                 return R();
    241                         }
    242                         template < typename R, typename T1, typename T2, typename T3, typename T4 >
    243                         R call( const path& p, const T1& p1, const T2& p2, const T3& p3, const T4& p4 )
    244                         {
    245                                 if ( L->push_function( p, false ) )
    246                                 {
    247                                         detail::push_value( L, p1, p2, p3, p4 );
    248                                         if ( L->call_function( 4, detail::return_count< R >::value ) == 0 )
    249                                         {
    250                                                 return detail::pop_return_value<R>( L );
    251                                         }
    252                                 }
    253                                 return R();
    254                         }
    255 
    256                 private:
    257                         bool push_function( const path& p );
    258                         int call_function( int nargs, int nresults );
    259                         void call_get();
    260                         void call_get_raw();
     221                private:
    261222                        void get_raw_flags( const std::string& element, uint8* data, uint32 count );
    262223
    263                         state* L;
    264                         stack_guard m_guard;
     224                        int m_level;
    265225                };
    266226
  • trunk/nv/lua/lua_values.hh

    r197 r206  
    8080                        void push_values( lua_State *L, const T1& p1, const T2& p2, const T3& p3, const T4& p4, const T5& p5 ) { push_value( L, p1 ); push_value( L, p2 ); push_value( L, p3 ); push_value( L, p4 ); push_value( L, p5 ); }
    8181
     82                        void pop_value( lua_State *L, int& n );
    8283                        void pop_value( lua_State *L, long& n );
    8384                        void pop_value( lua_State *L, double& n );
     
    9596                        {
    9697                                T ret;
    97                                 pass_traits<T>::pop( L, ret );
     98                                pop_value( L, ret );
    9899                                return ret;
    99100                        }
  • trunk/src/lua/lua_area.cc

    r204 r206  
    150150static int nlua_area_coords( lua_State* L )
    151151{
    152         nv::rectangle* a( nlua_to_parea( L, lua_upvalueindex(1) ) );
     152        nv::rectangle* a( nlua_to_parea( L, 1 ) );
    153153        nv::ivec2      c( a->ul );
    154154        c.x--;
     
    184184static int nlua_area_edges( lua_State* L )
    185185{
    186         nv::rectangle*   a( nlua_to_parea( L, lua_upvalueindex(1) ) );
     186        nv::rectangle*   a( nlua_to_parea( L, 1 ) );
    187187        nv::ivec2 c( a->ul );
    188188        c.x--;
  • trunk/src/lua/lua_map_area.cc

    r198 r206  
    99#include "nv/lua/lua_area.hh"
    1010#include "nv/lua/lua_glm.hh"
     11#include "nv/lua/lua_values.hh"
    1112#include "nv/lua/lua_raw.hh"
    1213
     
    5354nv::map_area* nlua_to_map_area( lua_State* L, int index )
    5455{
    55         return *(nv::map_area**)luaL_checkudata( L, index, NLUA_MAP_AREA_METATABLE );
     56        if ( lua_type( L, index ) == LUA_TTABLE )
     57        {
     58                nv::map_area* o = nullptr;
     59                if ( lua_istable( L , index ) )
     60                {
     61                        lua_pushstring( L, "__map_area_ptr" );
     62                        lua_rawget( L, index );
     63                        if ( lua_isuserdata( L, -1 ) )
     64                        {
     65                                o = static_cast<nv::map_area*>( lua_touserdata( L, -1 ) );
     66                        }
     67                        lua_pop( L, 1 );
     68                }
     69                return o;
     70        }
     71        else
     72        {
     73                return *(nv::map_area**)luaL_checkudata( L, index, NLUA_MAP_AREA_METATABLE );
     74        }
    5675}
    5776
     
    83102{
    84103        nv::map_area* ma = nlua_to_map_area( L, 1 );
    85         nlua_push_area( L, ma->get_rectangle() );
     104        nv::rectangle r  = ma->get_rectangle();
     105        r.lr.x -= 1;
     106        r.lr.y -= 1;
     107        nlua_push_area( L, r );
    86108        return 1;
    87109}
  • trunk/src/lua/lua_state.cc

    r204 r206  
    1616
    1717lua::stack_guard::stack_guard( lua::state* aL )
    18         : L(aL), m_level( lua_gettop(aL->L) )
     18        : L(aL), m_level( lua_gettop(aL->m_state) )
    1919{
    2020
     
    2222
    2323lua::stack_guard::stack_guard( lua::state& aL )
    24         : L(&aL), m_level( lua_gettop(aL.L) )
     24        : L(&aL), m_level( lua_gettop(aL.m_state) )
    2525{
    2626
     
    2929lua::stack_guard::~stack_guard()
    3030{
    31         lua_settop( L->L, m_level );
    32 }
    33 
    34 lua::state::state( bool load_libs /*= false*/ )
     31        lua_settop( L->m_state, m_level );
     32}
     33
     34lua::state::state( lua_State* state ) : state_wrapper( state, false )
     35{
     36
     37}
     38
     39lua::state::state( bool load_libs /*= false*/ ) : state_wrapper( nullptr, true )
    3540{
    3641        load_lua_library();
    3742        m_owner = true;
    38         L = luaL_newstate( );
    39 
    40         lua_pushcfunction(L, luaopen_base);
    41         lua_pushliteral(L, LUA_TABLIBNAME);
    42         lua_call(L, 1, 0);
     43        m_state = luaL_newstate( );
     44
     45        lua_pushcfunction(m_state, luaopen_base);
     46        lua_pushliteral(m_state, LUA_TABLIBNAME);
     47        lua_call(m_state, 1, 0);
    4348
    4449        if ( load_libs )
     
    5560                for(; lib->func != NULL; lib++)
    5661                {
    57                         lib->func( L );
     62                        lib->func( m_state );
    5863                }
    5964        }
     
    6570{
    6671        NV_LOG( nv::LOG_TRACE, "Loading Lua string '" << name << "'");
    67         return luaL_loadbuffer( L, code.c_str(), code.length(), name.c_str() );
     72        return luaL_loadbuffer( m_state, code.c_str(), code.length(), name.c_str() );
    6873}
    6974
     
    7984{
    8085        NV_LOG( nv::LOG_NOTICE, "Loading Lua file '" << filename << "'");
    81         return luaL_loadfile( L, filename.c_str() );
     86        return luaL_loadfile( m_state, filename.c_str() );
    8287}
    8388
     
    8893        if (result)
    8994        {
    90                 NV_LOG( nv::LOG_WARNING, "Failed to load string " << name << ": " << lua_tostring(L, -1));
     95                NV_LOG( nv::LOG_WARNING, "Failed to load string " << name << ": " << lua_tostring(m_state, -1));
    9196                return false;
    9297        }
     
    100105        if (result)
    101106        {
    102                 NV_LOG( nv::LOG_WARNING, "Failed to open stream " << name << ": " << lua_tostring(L, -1));
     107                NV_LOG( nv::LOG_WARNING, "Failed to open stream " << name << ": " << lua_tostring(m_state, -1));
    103108                return false;
    104109        }
     
    112117        if (result)
    113118        {
    114                 NV_LOG( nv::LOG_WARNING, "Failed to open file " << filename << ": " << lua_tostring(L, -1));
     119                NV_LOG( nv::LOG_WARNING, "Failed to open file " << filename << ": " << lua_tostring(m_state, -1));
    115120                return false;
    116121        }
     
    120125int lua::state::do_current( const std::string& name, int rvalues )
    121126{
    122         int result = lua_pcall(L, 0, rvalues, 0);
     127        int result = lua_pcall(m_state, 0, rvalues, 0);
    123128        if (result)
    124129        {
    125                 NV_LOG( nv::LOG_WARNING, "Failed to run script " << name << ": " << lua_tostring(L, -1));
    126                 lua_pop( L, 1 );
    127         }
    128         return result;
    129 }
    130 
    131 lua::state::~state()
     130                NV_LOG( nv::LOG_WARNING, "Failed to run script " << name << ": " << lua_tostring(m_state, -1));
     131                lua_pop( m_state, 1 );
     132        }
     133        return result;
     134}
     135
     136lua::state_wrapper::~state_wrapper()
    132137{
    133138        if (m_owner)
    134139        {
    135                 lua_close( L );
     140                lua_close( m_state );
    136141        }
    137142}
     
    145150                if (global)
    146151                {
    147                         lua_getglobal( L, path.c_str() );
     152                        lua_getglobal( m_state, path.c_str() );
    148153                }
    149154                else
    150155                {
    151                         lua_getfield( L, -1, path.c_str() );
    152                 }
    153                 return !lua_isnil( L, -1 );
     156                        lua_getfield( m_state, -1, path.c_str() );
     157                }
     158                return !lua_isnil( m_state, -1 );
    154159        }
    155160
     
    163168                        if (global)
    164169                        {
    165                                 lua_getglobal( L, path.substr(start,point-start).c_str() );
     170                                lua_getglobal( m_state, path.substr(start,point-start).c_str() );
    166171                        }
    167172                        else
    168173                        {
    169                                 lua_getfield( L, -1, path.substr(start,point-start).c_str() );
     174                                lua_getfield( m_state, -1, path.substr(start,point-start).c_str() );
    170175                        }
    171176                }
    172177                else
    173178                {
    174                         if ( lua_istable( L, -1 ) )
     179                        if ( lua_istable( m_state, -1 ) )
    175180                        {
    176                                 lua_pushstring( L, path.substr(start,point-start).c_str() );
    177                                 lua_gettable( L, -2 );
    178                                 lua_insert( L, -2 );
    179                                 lua_pop( L, 1 );
     181                                lua_pushstring( m_state, path.substr(start,point-start).c_str() );
     182                                lua_gettable( m_state, -2 );
     183                                lua_insert( m_state, -2 );
     184                                lua_pop( m_state, 1 );
    180185                        }
    181186                        else
    182187                        {
    183                                 lua_pop(L, 1);
    184                                 lua_pushnil(L);
     188                                lua_pop(m_state, 1);
     189                                lua_pushnil(m_state);
    185190                                return false;
    186191                        }
     
    195200int lua::state::get_stack_size()
    196201{
    197         return lua_gettop( L );
     202        return lua_gettop( m_state );
    198203}
    199204
    200205lua::table_guard::table_guard( lua::state* lstate, const path& p, bool global )
    201         : L(lstate), m_guard(lstate)
    202 {
    203         if ( !p.resolve( L->get_raw(), global ) )
     206        : state_wrapper( lstate->get_raw(), false ), m_level(0)
     207{
     208        m_global = false;
     209        m_level  = lua_gettop( m_state );
     210        if ( !p.resolve( m_state, global ) )
    204211        {
    205212                // TODO : error handling
     
    208215
    209216lua::table_guard::table_guard( const table_guard& parent, const path& p )
    210         : L( parent.L ), m_guard( parent.L )
    211 {
    212         if ( !p.resolve( L->get_raw(), false ) )
     217        : state_wrapper( parent.m_state, false ), m_level(0)
     218{
     219        m_global = false;
     220        m_level  = lua_gettop( m_state );
     221        if ( !p.resolve( m_state, false ) )
    213222        {
    214223                // TODO : error handling
     
    218227size_t lua::table_guard::get_size()
    219228{
    220         return lua_rawlen( L->get_raw(), -1 );
     229        return lua_rawlen( m_state, -1 );
    221230}
    222231
     
    224233bool lua::table_guard::has_field( const string& element )
    225234{
    226         lua_getfield( L->L, -1, element.c_str() );
    227         bool result = lua_isnil( L->L, -1 );
    228         lua_pop( L->L, 1 );
     235        lua_getfield( m_state, -1, element.c_str() );
     236        bool result = lua_isnil( m_state, -1 );
     237        lua_pop( m_state, 1 );
    229238        return result;
    230239}
     
    232241string lua::table_guard::get_string( const string& element, const string& defval /*= "" */ )
    233242{
    234         lua_getfield( L->L, -1, element.c_str() );
    235         string result( ( lua_type( L->L, -1 ) == LUA_TSTRING ) ? lua_tostring( L->L, -1 ) : defval );
    236         lua_pop( L->L, 1 );
     243        lua_getfield( m_state, -1, element.c_str() );
     244        string result( ( lua_type( m_state, -1 ) == LUA_TSTRING ) ? lua_tostring( m_state, -1 ) : defval );
     245        lua_pop( m_state, 1 );
    237246        return result;
    238247}
     
    240249char lua::table_guard::get_char( const string& element, char defval /*= "" */ )
    241250{
    242         lua_getfield( L->L, -1, element.c_str() );
    243         char result = ( lua_type( L->L, -1 ) == LUA_TSTRING && lua_rawlen( L->L, -1 ) > 0 ) ? lua_tostring( L->L, -1 )[0] : defval;
    244         lua_pop( L->L, 1 );
     251        lua_getfield( m_state, -1, element.c_str() );
     252        char result = ( lua_type( m_state, -1 ) == LUA_TSTRING && lua_rawlen( m_state, -1 ) > 0 ) ? lua_tostring( m_state, -1 )[0] : defval;
     253        lua_pop( m_state, 1 );
    245254        return result;
    246255}
     
    248257int lua::table_guard::get_integer( const string& element, int defval /*= "" */ )
    249258{
    250         lua_getfield( L->L, -1, element.c_str() );
    251         lua_Integer result = lua_type( L->L, -1 ) == LUA_TNUMBER ? lua_tointeger( L->L, -1 ) : defval;
    252         lua_pop( L->L, 1 );
     259        lua_getfield( m_state, -1, element.c_str() );
     260        lua_Integer result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tointeger( m_state, -1 ) : defval;
     261        lua_pop( m_state, 1 );
    253262        return static_cast< int >( result );
    254263}
     
    256265unsigned lua::table_guard::get_unsigned( const string& element, unsigned defval /*= "" */ )
    257266{
    258         lua_getfield( L->L, -1, element.c_str() );
    259         unsigned result = lua_type( L->L, -1 ) == LUA_TNUMBER ? lua_tounsigned( L->L, -1 ) : defval;
    260         lua_pop( L->L, 1 );
     267        lua_getfield( m_state, -1, element.c_str() );
     268        unsigned result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tounsigned( m_state, -1 ) : defval;
     269        lua_pop( m_state, 1 );
    261270        return result;
    262271}
     
    264273double lua::table_guard::get_double( const string& element, double defval /*= "" */ )
    265274{
    266         lua_getfield( L->L, -1, element.c_str() );
    267         double result = lua_type( L->L, -1 ) == LUA_TNUMBER ? lua_tonumber( L->L, -1 ) : defval;
    268         lua_pop( L->L, 1 );
     275        lua_getfield( m_state, -1, element.c_str() );
     276        double result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tonumber( m_state, -1 ) : defval;
     277        lua_pop( m_state, 1 );
    269278        return result;
    270279}
     
    272281bool lua::table_guard::get_boolean( const string& element, bool defval /*= "" */ )
    273282{
    274         lua_getfield( L->L, -1, element.c_str() );
    275         bool result = lua_type( L->L, -1 ) == LUA_TBOOLEAN ? lua_toboolean( L->L, -1 ) != 0 : defval;
    276         lua_pop( L->L, 1 );
    277         return result;
    278 }
    279 
    280 void lua::table_guard::call_get()
    281 {
    282         lua_gettable( L->L, -2 );
    283 }
    284 
    285 void lua::table_guard::call_get_raw()
    286 {
    287         lua_rawget( L->L, -2 );
    288 }
    289 
    290 bool nv::lua::table_guard::is_defined( const path& p )
    291 {
    292         return L->is_defined( p, false );
     283        lua_getfield( m_state, -1, element.c_str() );
     284        bool result = lua_type( m_state, -1 ) == LUA_TBOOLEAN ? lua_toboolean( m_state, -1 ) != 0 : defval;
     285        lua_pop( m_state, 1 );
     286        return result;
     287}
     288
     289void nv::lua::state_wrapper::push_global_table()
     290{
     291        lua_pushglobaltable( m_state );
     292}
     293
     294void nv::lua::state_wrapper::pop_global_table()
     295{
     296        lua_replace( m_state, -2 );
     297}
     298
     299void lua::state_wrapper::call_get()
     300{
     301        lua_gettable( m_state, -2 );
     302}
     303
     304void lua::state_wrapper::call_get_raw()
     305{
     306        lua_rawget( m_state, -2 );
    293307}
    294308
    295309void lua::table_guard::get_raw_flags( const std::string& element, uint8* data, uint32 count )
    296310{
    297         lua_getfield( L->L, -1, element.c_str() );
    298         if ( lua_type( L->L, -1 ) != LUA_TTABLE )
    299         {
    300                 lua_pop( L->L, 1 );
     311        lua_getfield( m_state, -1, element.c_str() );
     312        if ( lua_type( m_state, -1 ) != LUA_TTABLE )
     313        {
     314                lua_pop( m_state, 1 );
    301315                return;
    302316        }
    303         nlua_toflags( L->L, -1, data, count );
    304         lua_pop( L->L, 1 );
     317        nlua_toflags( m_state, -1, data, count );
     318        lua_pop( m_state, 1 );
    305319}
    306320
     
    308322void lua::state::log_stack()
    309323{
    310         int top = lua_gettop(L);
     324        int top = lua_gettop(m_state);
    311325        NV_LOG( LOG_DEBUG, "Stack dump (" << top << ")");
    312326        for ( int i = 0; i < top; ++i )
    313327        {
    314                 NV_LOG( LOG_DEBUG, "#" << i+1 << " - " << lua_typename(L, lua_type(L, i+1) ) << " = " << nlua_typecontent(L, i+1) );
     328                NV_LOG( LOG_DEBUG, "#" << i+1 << " - " << lua_typename(m_state, lua_type(m_state, i+1) ) << " = " << nlua_typecontent(m_state, i+1) );
    315329        }
    316330}
     
    318332lua_State* lua::state::get_raw()
    319333{
    320         return L;
     334        return m_state;
    321335}
    322336
     
    325339        if ( o == nullptr ) return ref_none;
    326340        stack_guard guard( this );
    327         lua_getglobal( L, lua_name );
    328         if ( lua_isnil( L, -1 ) )
     341        lua_getglobal( m_state, lua_name );
     342        if ( lua_isnil( m_state, -1 ) )
    329343        {
    330344                NV_THROW( runtime_error, std::string( lua_name ) + " type not registered!" );
    331345        }
    332346        deep_pointer_copy( -1, o );
    333         return luaL_ref( L, LUA_REGISTRYINDEX );
     347        return luaL_ref( m_state, LUA_REGISTRYINDEX );
    334348}
    335349
     
    348362        if (!o) return;
    349363        stack_guard guard( this );
    350         lua_rawgeti( L, LUA_REGISTRYINDEX, o->get_lua_index() );
    351         lua_pushstring( L, "__ptr" );
    352         lua_pushboolean( L, false );
    353         lua_rawset( L, -3 );
    354         lua_pop( L, 1 );
    355         luaL_unref( L, LUA_REGISTRYINDEX, o->get_lua_index() );
     364        lua_rawgeti( m_state, LUA_REGISTRYINDEX, o->get_lua_index() );
     365        lua_pushstring( m_state, "__ptr" );
     366        lua_pushboolean( m_state, false );
     367        lua_rawset( m_state, -3 );
     368        lua_pop( m_state, 1 );
     369        luaL_unref( m_state, LUA_REGISTRYINDEX, o->get_lua_index() );
    356370}
    357371
    358372void lua::state::deep_pointer_copy( int index, void* obj )
    359373{
    360         index = lua_absindex( L, index );
    361         lua_newtable( L );
    362         lua_pushnil( L );
     374        index = lua_absindex( m_state, index );
     375        lua_newtable( m_state );
     376        lua_pushnil( m_state );
    363377        bool has_functions = false;
    364378        bool has_metatable = false;
    365379
    366         while ( lua_next( L, index ) != 0 )
    367         {
    368                 if ( lua_isfunction( L, -1 ) )
     380        while ( lua_next( m_state, index ) != 0 )
     381        {
     382                if ( lua_isfunction( m_state, -1 ) )
    369383                        has_functions = true;
    370                 else if ( lua_istable( L, -1 ) )
     384                else if ( lua_istable( m_state, -1 ) )
    371385                {
    372386                        deep_pointer_copy( -1, obj );
    373                         lua_insert( L, -2 );
    374                         lua_pop( L, 1 );
    375                 }
    376                 lua_pushvalue( L, -2 );
    377                 lua_insert( L, -2 );
    378                 lua_settable( L, -4 );
    379         }
    380 
    381         if ( lua_getmetatable( L, -2 ) )
    382         {
    383                 lua_setmetatable( L, -2 );
     387                        lua_insert( m_state, -2 );
     388                        lua_pop( m_state, 1 );
     389                }
     390                lua_pushvalue( m_state, -2 );
     391                lua_insert( m_state, -2 );
     392                lua_settable( m_state, -4 );
     393        }
     394
     395        if ( lua_getmetatable( m_state, -2 ) )
     396        {
     397                lua_setmetatable( m_state, -2 );
    384398                has_metatable = true;
    385399        }
     
    387401        if ( has_functions || has_metatable )
    388402        {
    389                 lua_pushstring( L, "__ptr" );
    390                 lua_pushlightuserdata( L, obj );
    391                 lua_rawset( L, -3 );
     403                lua_pushstring( m_state, "__ptr" );
     404                lua_pushlightuserdata( m_state, obj );
     405                lua_rawset( m_state, -3 );
    392406        }
    393407}
     
    399413        for ( const auto& entry : et->enum_list )
    400414        {
    401                 lua_pushinteger( L, entry.value );
     415                lua_pushinteger( m_state, entry.value );
    402416                if ( prefix.empty() )
    403417                {
    404                         lua_setglobal( L, entry.name.c_str() );
     418                        lua_setglobal( m_state, entry.name.c_str() );
    405419                }
    406420                else
    407421                {
    408                         lua_setglobal( L, ( prefix + entry.name ).c_str() );
    409                 }
    410         }
    411 }
    412 
    413 
    414 bool nv::lua::state::is_defined( const path& p, bool global )
    415 {
    416         if ( !p.resolve( L, global ) )
    417         {
    418                 return false;
    419         }
    420         bool result = !lua_isnil( L, -1 );
    421         lua_pop( L, 1 );
    422         return result;
    423 }
    424 
    425 int lua::state::call_function( int nargs, int nresults )
    426 {
    427         int status = lua_pcall( L, nargs, nresults, 0 );
     422                        lua_setglobal( m_state, ( prefix + entry.name ).c_str() );
     423                }
     424        }
     425}
     426
     427void nv::lua::state::store_metadata( object* o, const std::string& metaname, void* pointer )
     428{
     429        if (!o) return;
     430        stack_guard guard( this );
     431        lua_rawgeti( m_state, LUA_REGISTRYINDEX, o->get_lua_index() );
     432        lua_pushstring( m_state, metaname.c_str() );
     433        lua_pushlightuserdata( m_state, pointer );
     434        lua_rawset( m_state, -3 );
     435        lua_pop( m_state, 1 );
     436}
     437
     438bool nv::lua::state_wrapper::is_defined( const path& p, bool global )
     439{
     440        if ( !p.resolve( m_state, global ) )
     441        {
     442                return false;
     443        }
     444        bool result = !lua_isnil( m_state, -1 );
     445        lua_pop( m_state, 1 );
     446        return result;
     447}
     448
     449bool nv::lua::state_wrapper::push_function( const path& p, bool global )
     450{
     451        if ( !p.resolve( m_state, global ) )
     452        {
     453                NV_LOG( LOG_ERROR, "Lua error : not a valid path - " + p.to_string() );
     454                return false;
     455        }
     456
     457        if ( !lua_isfunction( m_state, -1 ) )
     458        {
     459                lua_pop( m_state, 1 );
     460                NV_LOG( LOG_ERROR, "Lua error : not a valid function - " + p.to_string() );
     461                return false;
     462        }
     463        return true;
     464}
     465
     466int nv::lua::state_wrapper::call_function( int nargs, int nresults )
     467{
     468        int status = lua_pcall( m_state, nargs, nresults, 0 );
    428469        if ( status != 0 )
    429470        {
    430                 std::string error = lua_tostring( L, -1 );
    431                 lua_pop( L, 1 );
     471                std::string error = lua_tostring( m_state, -1 );
     472                lua_pop( m_state, 1 );
    432473                NV_LOG( LOG_ERROR, "Lua error : " << error )
    433474        }
     
    435476}
    436477
    437 bool lua::state::push_function( const path& p, bool global )
    438 {
    439         if ( !p.resolve( L, global ) )
    440         {
    441                 NV_LOG( LOG_ERROR, "Lua error : not a valid path - " + p.to_string() );
    442                 return false;
    443         }
    444 
    445         if ( !lua_isfunction( L, -1 ) )
    446         {
    447                 lua_pop( L, 1 );
    448                 NV_LOG( LOG_ERROR, "Lua error : not a valid function - " + p.to_string() );
    449                 return false;
    450         }
    451         return true;
    452 }
    453 
     478lua::table_guard::~table_guard()
     479{
     480        lua_settop( m_state, m_level );
     481}
  • trunk/src/lua/lua_values.cc

    r183 r206  
    5252}
    5353
     54void nv::lua::detail::pop_value( lua_State *L, int& n )
     55{
     56        n = lua_tointeger( L, -1 );
     57        lua_pop( L, 1 );
     58}
     59
    5460void nv::lua::detail::pop_value( lua_State *L, long& n )
    5561{
Note: See TracChangeset for help on using the changeset viewer.