[52] | 1 | #include <nv/lib/lua.hh>
|
---|
[74] | 2 | #include <nv/lua/lua_state.hh>
|
---|
[52] | 3 | #include <nv/lua/lua_raw.hh>
|
---|
[53] | 4 | #include <nv/lua/lua_glm.hh>
|
---|
[52] | 5 | #include <nv/logger.hh>
|
---|
[62] | 6 | #include <nv/types.hh>
|
---|
| 7 | #include <nv/object.hh>
|
---|
[52] | 8 | #include <string>
|
---|
| 9 | #include <iostream>
|
---|
[62] | 10 | #include <functional>
|
---|
[69] | 11 | #include <nv/gui/gui_element.hh>
|
---|
[52] | 12 |
|
---|
[62] | 13 | struct test_struct
|
---|
| 14 | {
|
---|
| 15 | std::string f;
|
---|
| 16 | int i;
|
---|
| 17 | };
|
---|
| 18 |
|
---|
| 19 | NV_REGISTER_NAME( test_struct )
|
---|
| 20 |
|
---|
[52] | 21 | int main(int, char* [])
|
---|
| 22 | {
|
---|
[62] | 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 |
|
---|
[52] | 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 );
|
---|
[62] | 37 |
|
---|
[52] | 38 | NV_LOG( nv::LOG_NOTICE, "Logging started" );
|
---|
| 39 |
|
---|
| 40 | // create new Lua state
|
---|
| 41 | {
|
---|
[74] | 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" );
|
---|
[52] | 47 |
|
---|
[74] | 48 | for (;;)
|
---|
[52] | 49 | {
|
---|
[74] | 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 | }
|
---|
[52] | 59 |
|
---|
[74] | 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 | }
|
---|
[52] | 66 |
|
---|
[74] | 67 | std::cout << "> " << input << std::endl;
|
---|
[52] | 68 |
|
---|
[74] | 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 | }
|
---|
[52] | 77 |
|
---|
[74] | 78 | if (lua_gettop( state.get_raw() ) > stack)
|
---|
[52] | 79 | {
|
---|
[74] | 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 | }
|
---|
[52] | 84 | }
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 | NV_LOG( nv::LOG_NOTICE, "Logging stopped" );
|
---|
| 88 |
|
---|
| 89 | return 0;
|
---|
| 90 | }
|
---|
| 91 |
|
---|