source: trunk/tests/lualib_test/lualib_test.cc @ 74

Last change on this file since 74 was 74, checked in by epyon, 12 years ago
  • lua::state - removed notion of global state
  • lua_glm - fixed stack pollution at registration
  • lualib_test - made use of lua::state
File size: 2.1 KB
Line 
1#include <nv/lib/lua.hh>
2#include <nv/lua/lua_state.hh>
3#include <nv/lua/lua_raw.hh>
4#include <nv/lua/lua_glm.hh>
5#include <nv/logger.hh>
6#include <nv/types.hh>
7#include <nv/object.hh>
8#include <string>
9#include <iostream>
10#include <functional>
11#include <nv/gui/gui_element.hh>
12
13struct test_struct
14{
15        std::string f;
16        int i;
17};
18
19NV_REGISTER_NAME( test_struct )
20
21int main(int, char* [])
22{
23        nv::type_database db;
24        nv::object::register_type( &db );
25
26        db.create_type<int>();
27        db.create_type<std::string>();
28        nv::type_field fields[] = {
29                nv::type_field("f", &test_struct::f ),
30                nv::type_field("i", &test_struct::i ),
31        };
32        db.create_type<test_struct>().fields( fields );
33
34        nv::logger log(nv::LOG_TRACE);
35        log.add_sink( new nv::log_file_sink("log.txt"), nv::LOG_TRACE );
36        log.add_sink( new nv::log_console_sink(), nv::LOG_TRACE );
37       
38        NV_LOG( nv::LOG_NOTICE, "Logging started" );
39
40        // create new Lua state
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" );
47
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                        }
59
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                        }
66
67                        std::cout << "> " << input << std::endl;
68
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                        }
77
78                        if (lua_gettop( state.get_raw() ) > stack)
79                        {
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                                }
84                        }
85                }
86        }
87        NV_LOG( nv::LOG_NOTICE, "Logging stopped" );
88
89        return 0;
90}
91
Note: See TracBrowser for help on using the repository browser.