Changeset 212


Ignore:
Timestamp:
09/09/13 19:29:23 (12 years ago)
Author:
epyon
Message:
  • lua/dispatch - powerful module for binding any function or method to lua
  • lua/state - added initial method/function registration
Location:
trunk
Files:
1 added
2 edited

Legend:

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

    r209 r212  
    1212#include <nv/common.hh>
    1313#include <nv/flags.hh>
     14
    1415#include <nv/lua/lua_path.hh>
    1516#include <nv/lua/lua_values.hh>
     17#include <nv/lua/lua_dispatch.hh>
    1618#include <string>
    17 
    18 struct lua_State;
    1919
    2020namespace nv
     
    2626                const int ret_multi = -1;
    2727
    28                 class state;
     28                int upvalue_index( int i );
     29
    2930                typedef int reference;
    3031
     
    131132
    132133                        bool is_defined( const path& p ) { return is_defined( p, m_global ); }
     134                        void register_native_function( lfunction f, const char* name );
     135                        template < typename F, F* f >
     136                        void register_function( const char* name )
     137                        {
     138                                register_native_function( detail::function_wrapper< F, f >, name );
     139                        }
    133140                protected:
    134141                        bool is_defined( const path& p, bool global );
     
    178185                        void store_metadata( object* o, const std::string& metaname, void* pointer );
    179186                        void unregister_object( object * o );
     187                        void register_native_object_method( const char* lua_name, const char* name, lfunction f );
     188                        template < typename F, F* f >
     189                        void register_object_method( const char* lua_name, const char* name )
     190                        {
     191                                register_native_object_method( lua_name, name, detail::object_method_wrapper< typename memfn_class_type<F>, F, f > );
     192                        }
    180193                        void register_enum( type_database* db, const std::string& name, const std::string& prefix = std::string() );
    181194                        operator lua_State*() { return m_state; }
  • trunk/src/lua/lua_state.cc

    r209 r212  
    251251}
    252252
     253
     254void lua::state_wrapper::register_native_function( lfunction f, const char* name )
     255{
     256        if ( m_global ) push_global_table();
     257        lua_pushcfunction( m_state, f );
     258        lua_setfield( m_state, -2, name );
     259        if ( m_global ) pop_global_table();
     260}
     261
    253262void lua::state::log_stack()
    254263{
     
    287296        if ( t == nullptr ) return ref_none;
    288297        return register_object( o, t->name.c_str() );
     298}
     299
     300void lua::state::register_native_object_method( const char* lua_name, const char* name, lfunction f )
     301{
     302        stack_guard guard( this );
     303        lua_getglobal( m_state, lua_name );
     304        if ( lua_isnil( m_state, -1 ) )
     305        {
     306                NV_THROW( runtime_error, std::string( lua_name ) + " type not registered!" );
     307        }
     308        lua_pushcfunction( m_state, f );
     309        lua_setfield( m_state, -2, name );
    289310}
    290311
     
    411432        lua_settop( m_state, m_level );
    412433}
     434
     435int nv::lua::upvalue_index( int i )
     436{
     437        return lua_upvalueindex(i);
     438}
Note: See TracChangeset for help on using the changeset viewer.