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

Last change on this file since 76 was 76, checked in by epyon, 12 years ago
  • minor fixes
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                nlua_register_glm( state.get_raw() );
44                // run the Lua script
45                state.do_file( "init.lua" );
46
47                for (;;)
48                {
49                        nv::lua::stack_guard guard( &state );
50                        int stack = guard.get_level();
51                        std::string input;
52                        std::cout << "LUA (" << stack << ") > ";
53                        std::getline( std::cin, input );
54                        if (input == "quit")
55                        {
56                                break;
57                        }
58
59                        if (input.find("=", 0) == std::string::npos &&
60                                input.find("if", 0) == std::string::npos &&
61                                input.find("return", 0) == std::string::npos)
62                        {
63                                input = "return " + input;
64                        }
65
66                        std::cout << "> " << input << std::endl;
67
68                        int code = luaL_loadstring( state.get_raw(), input.c_str() );
69                        if (code == 0) code = lua_pcall( state.get_raw(), 0, LUA_MULTRET, 0);
70                        if (code != 0)
71                        {
72                                std::string error = lua_tostring( state.get_raw(), -1 );
73                                std::cout << "ERROR : " << error << std::endl;
74                                continue;
75                        }
76
77                        if (lua_gettop( state.get_raw() ) > stack)
78                        {
79                                for ( int i = stack+1; i <= lua_gettop( state.get_raw() ); ++i )
80                                {
81                                        std::cout << nlua_typecontent( state.get_raw(), i ) << std::endl;
82                                }
83                        }
84                }
85        }
86        NV_LOG( nv::LOG_NOTICE, "Logging stopped" );
87
88        return 0;
89}
90
Note: See TracBrowser for help on using the repository browser.