Changeset 74 for trunk/tests/lualib_test


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
File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.