Changeset 74 for trunk/tests/lualib_test/lualib_test.cc
- Timestamp:
- 06/01/13 17:07:09 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/lualib_test/lualib_test.cc
r69 r74 1 1 #include <nv/lib/lua.hh> 2 #include <nv/lua/lua_state.hh> 2 3 #include <nv/lua/lua_raw.hh> 3 4 #include <nv/lua/lua_glm.hh> … … 31 32 db.create_type<test_struct>().fields( fields ); 32 33 33 34 34 nv::logger log(nv::LOG_TRACE); 35 35 log.add_sink( new nv::log_file_sink("log.txt"), nv::LOG_TRACE ); 36 36 log.add_sink( new nv::log_console_sink(), nv::LOG_TRACE ); 37 nv::load_lua_library();38 37 39 38 NV_LOG( nv::LOG_NOTICE, "Logging started" ); 40 39 41 40 // 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" ); 44 47 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 } 51 59 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 } 53 66 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; 60 68 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 } 63 77 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) 97 79 { 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 } 99 84 } 100 85 } 101 lua_settop( lua_state, stack );102 86 } 103 104 105 // close the Lua state106 lua_close(lua_state);107 87 NV_LOG( nv::LOG_NOTICE, "Logging stopped" ); 108 88
Note: See TracChangeset
for help on using the changeset viewer.