- Timestamp:
- 06/01/13 17:07:09 (12 years ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/nv/lua/lua_state.hh
r9 r74 30 30 public: 31 31 stack_guard( state* L ); 32 int get_level() const { return m_level; } 32 33 ~stack_guard(); 33 p ublic:34 private: 34 35 state* L; 35 36 int m_level; … … 60 61 friend class table_guard; 61 62 public: 62 state( bool is_main= false );63 state( bool load_libs = false ); 63 64 state( lua_State* state ); 64 65 int load_string( const std::string& code, const std::string& name ); … … 72 73 lua_State* get_raw(); 73 74 ~state(); 74 static state* get();75 75 private: 76 76 int do_current( const std::string& name ); -
trunk/src/lua/lua_glm.cc
r53 r74 335 335 nlua_swizzel_lookup['v'] = 0; 336 336 nlua_swizzel_lookup['3'] = 3; 337 int stack = lua_gettop( L ); 337 338 luaL_requiref(L, "ivec2", luaopen_vec<glm::ivec2>, 1); 338 339 luaL_requiref(L, "ivec3", luaopen_vec<glm::ivec3>, 1); … … 341 342 luaL_requiref(L, "vec3", luaopen_vec<glm::vec3>, 1); 342 343 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 24 24 } 25 25 26 lua::state::state( bool is_main /*= false*/ ) 27 { 26 lua::state::state( bool load_libs /*= false*/ ) 27 { 28 load_lua_library(); 28 29 m_owner = true; 29 30 L = luaL_newstate( ); … … 33 34 lua_call(L, 1, 0); 34 35 35 if (is_main) 36 { 37 NV_ASSERT( this == get(), "lua_state : another main state created!" ); 36 if ( load_libs ) 37 { 38 38 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" ); 46 54 } 47 55 … … 121 129 } 122 130 123 lua::state* lua::state::get()124 {125 static lua::state main_state(true);126 return &main_state;127 }128 129 131 bool lua::state::push( const std::string& path, bool global ) 130 132 { … … 275 277 for ( int i = 0; i < top; ++i ) 276 278 { 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) ); 278 280 } 279 281 } -
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.