Changeset 74 for trunk


Ignore:
Timestamp:
06/01/13 17:07:09 (12 years ago)
Author:
epyon
Message:
  • lua::state - removed notion of global state
  • lua_glm - fixed stack pollution at registration
  • lualib_test - made use of lua::state
Location:
trunk
Files:
4 edited

Legend:

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

    r9 r74  
    3030                public:
    3131                        stack_guard( state* L );
     32                        int get_level() const { return m_level; }
    3233                        ~stack_guard();
    33                 public:
     34                private:
    3435                        state* L;
    3536                        int m_level;
     
    6061                        friend class table_guard;
    6162                public:
    62                         state( bool is_main = false );
     63                        state( bool load_libs = false );
    6364                        state( lua_State* state );
    6465                        int load_string( const std::string& code, const std::string& name );
     
    7273                        lua_State* get_raw();
    7374                        ~state();
    74                         static state* get();
    7575                private:
    7676                        int do_current( const std::string& name );
  • trunk/src/lua/lua_glm.cc

    r53 r74  
    335335        nlua_swizzel_lookup['v'] = 0;
    336336        nlua_swizzel_lookup['3'] = 3;
     337        int stack = lua_gettop( L );
    337338        luaL_requiref(L, "ivec2", luaopen_vec<glm::ivec2>, 1);
    338339        luaL_requiref(L, "ivec3", luaopen_vec<glm::ivec3>, 1);
     
    341342        luaL_requiref(L, "vec3", luaopen_vec<glm::vec3>, 1);
    342343        luaL_requiref(L, "vec4", luaopen_vec<glm::vec4>, 1);
    343 }
    344 
     344        lua_settop( L, stack );
     345}
     346
  • trunk/src/lua/lua_state.cc

    r51 r74  
    2424}
    2525
    26 lua::state::state( bool is_main /*= false*/ )
    27 {
     26lua::state::state( bool load_libs /*= false*/ )
     27{
     28        load_lua_library();
    2829        m_owner = true;
    2930        L = luaL_newstate( );
     
    3334        lua_call(L, 1, 0);
    3435
    35         if (is_main)
    36         {
    37                 NV_ASSERT( this == get(), "lua_state : another main state created!" );
     36        if ( load_libs )
     37        {
    3838                stack_guard guard( this );
    39                 luaopen_base( L );
    40                 luaopen_string( L );
    41                 luaopen_table( L );
    42                 luaopen_math( L );
    43         }
    44 
    45         NV_LOG( nv::LOG_TRACE, is_main ? "Main Lua state created" : "Secondary Lua state created");
     39                static const luaL_Reg lualibs[] =
     40                {
     41                        { "string", luaopen_string },
     42                        { "table",  luaopen_table },
     43                        { "math",   luaopen_math },
     44                        { NULL, NULL}
     45                };
     46                const luaL_Reg *lib = lualibs;
     47                for(; lib->func != NULL; lib++)
     48                {
     49                        lib->func( L );
     50                }
     51        }
     52
     53        NV_LOG( nv::LOG_TRACE, "Lua state created" );
    4654}
    4755
     
    121129}
    122130
    123 lua::state* lua::state::get()
    124 {
    125         static lua::state main_state(true);
    126         return &main_state;
    127 }
    128 
    129131bool lua::state::push( const std::string& path, bool global )
    130132{
     
    275277        for ( int i = 0; i < top; ++i )
    276278        {
    277                 NV_LOG( LOG_DEBUG, "#" << i+1 << " - " << lua_typename(L, i+1) << " = " << nlua_typecontent(L, i+1) );
     279                NV_LOG( LOG_DEBUG, "#" << i+1 << " - " << lua_typename(L, lua_type(L, i+1) ) << " = " << nlua_typecontent(L, i+1) );
    278280        }
    279281}
  • trunk/tests/lualib_test/lualib_test.cc

    r69 r74  
    11#include <nv/lib/lua.hh>
     2#include <nv/lua/lua_state.hh>
    23#include <nv/lua/lua_raw.hh>
    34#include <nv/lua/lua_glm.hh>
     
    3132        db.create_type<test_struct>().fields( fields );
    3233
    33 
    3434        nv::logger log(nv::LOG_TRACE);
    3535        log.add_sink( new nv::log_file_sink("log.txt"), nv::LOG_TRACE );
    3636        log.add_sink( new nv::log_console_sink(), nv::LOG_TRACE );
    37         nv::load_lua_library();
    3837       
    3938        NV_LOG( nv::LOG_NOTICE, "Logging started" );
    4039
    4140        // create new Lua state
    42         lua_State *lua_state;
    43         lua_state = luaL_newstate();
     41        {
     42                nv::lua::state state( true );
     43                state.log_stack();
     44                nlua_register_glm( state.get_raw() );
     45                // run the Lua script
     46                state.do_file( "init.lua" );
    4447
    45         // load Lua libraries
    46         static const luaL_Reg lualibs[] =
    47         {
    48                 { "base", luaopen_base },
    49                 { NULL, NULL}
    50         };
     48                for (;;)
     49                {
     50                        nv::lua::stack_guard guard( &state );
     51                        int stack = guard.get_level();
     52                        std::string input;
     53                        std::cout << "LUA (" << stack << ") > ";
     54                        std::getline( std::cin, input );
     55                        if (input == "quit")
     56                        {
     57                                break;
     58                        }
    5159
    52         nlua_register_glm( lua_state );
     60                        if (input.find("=", 0) == std::string::npos &&
     61                                input.find("if", 0) == std::string::npos &&
     62                                input.find("return", 0) == std::string::npos)
     63                        {
     64                                input = "return " + input;
     65                        }
    5366
    54         const luaL_Reg *lib = lualibs;
    55         for(; lib->func != NULL; lib++)
    56         {
    57                 lib->func(lua_state);
    58                 lua_settop(lua_state, 0);
    59         }
     67                        std::cout << "> " << input << std::endl;
    6068
    61         // run the Lua script
    62         luaL_dofile(lua_state, "init.lua");
     69                        int code = luaL_loadstring( state.get_raw(), input.c_str() );
     70                        if (code == 0) code = lua_pcall( state.get_raw(), 0, LUA_MULTRET, 0);
     71                        if (code != 0)
     72                        {
     73                                std::string error = lua_tostring( state.get_raw(), -1 );
     74                                std::cout << "ERROR : " << error << std::endl;
     75                                continue;
     76                        }
    6377
    64         for (;;)
    65         {
    66                 std::string input;
    67                 int stack = lua_gettop( lua_state );
    68                 std::cout << "LUA (" << stack << ") > ";
    69                 std::getline( std::cin, input );
    70                 if (input == "quit")
    71                 {
    72                         break;
    73                 }
    74 
    75                 if (input.find("=", 0) == std::string::npos &&
    76                         input.find("if", 0) == std::string::npos &&
    77                         input.find("return", 0) == std::string::npos)
    78                 {
    79                         input = "return " + input;
    80                 }
    81 
    82                 std::cout << "> " << input << std::endl;
    83 
    84                 int code = luaL_loadstring( lua_state, input.c_str() );
    85                 if (code == 0) code = lua_pcall( lua_state, 0, LUA_MULTRET, 0);
    86                 if (code != 0)
    87                 {
    88                         std::string error = lua_tostring( lua_state, -1 );
    89                         std::cout << "ERROR : " << error << std::endl;
    90                         lua_settop( lua_state, stack );
    91                         continue;
    92                 }
    93 
    94                 if (lua_gettop( lua_state ) > stack)
    95                 {
    96                         for ( int i = stack+1; i <= lua_gettop( lua_state ); ++i )
     78                        if (lua_gettop( state.get_raw() ) > stack)
    9779                        {
    98                                 std::cout << nlua_typecontent( lua_state, i ) << std::endl;
     80                                for ( int i = stack+1; i <= lua_gettop( state.get_raw() ); ++i )
     81                                {
     82                                        std::cout << nlua_typecontent( state.get_raw(), i ) << std::endl;
     83                                }
    9984                        }
    10085                }
    101                 lua_settop( lua_state, stack );
    10286        }
    103 
    104 
    105         // close the Lua state
    106         lua_close(lua_state);
    10787        NV_LOG( nv::LOG_NOTICE, "Logging stopped" );
    10888
Note: See TracChangeset for help on using the changeset viewer.