[52] | 1 | #include <nv/lib/lua.hh>
|
---|
| 2 | #include <nv/lua/lua_raw.hh>
|
---|
[53] | 3 | #include <nv/lua/lua_glm.hh>
|
---|
[52] | 4 | #include <nv/logger.hh>
|
---|
[62] | 5 | #include <nv/types.hh>
|
---|
| 6 | #include <nv/object.hh>
|
---|
[52] | 7 | #include <string>
|
---|
| 8 | #include <iostream>
|
---|
[62] | 9 | #include <functional>
|
---|
[69] | 10 | #include <nv/gui/gui_element.hh>
|
---|
[52] | 11 |
|
---|
[62] | 12 | struct test_struct
|
---|
| 13 | {
|
---|
| 14 | std::string f;
|
---|
| 15 | int i;
|
---|
| 16 | };
|
---|
| 17 |
|
---|
| 18 | NV_REGISTER_NAME( test_struct )
|
---|
| 19 |
|
---|
[52] | 20 | int main(int, char* [])
|
---|
| 21 | {
|
---|
[62] | 22 | nv::type_database db;
|
---|
| 23 | nv::object::register_type( &db );
|
---|
| 24 |
|
---|
| 25 | db.create_type<int>();
|
---|
| 26 | db.create_type<std::string>();
|
---|
| 27 | nv::type_field fields[] = {
|
---|
| 28 | nv::type_field("f", &test_struct::f ),
|
---|
| 29 | nv::type_field("i", &test_struct::i ),
|
---|
| 30 | };
|
---|
| 31 | db.create_type<test_struct>().fields( fields );
|
---|
| 32 |
|
---|
| 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 );
|
---|
| 37 | nv::load_lua_library();
|
---|
[62] | 38 |
|
---|
[52] | 39 | NV_LOG( nv::LOG_NOTICE, "Logging started" );
|
---|
| 40 |
|
---|
| 41 | // create new Lua state
|
---|
| 42 | lua_State *lua_state;
|
---|
| 43 | lua_state = luaL_newstate();
|
---|
| 44 |
|
---|
| 45 | // load Lua libraries
|
---|
| 46 | static const luaL_Reg lualibs[] =
|
---|
| 47 | {
|
---|
| 48 | { "base", luaopen_base },
|
---|
| 49 | { NULL, NULL}
|
---|
| 50 | };
|
---|
| 51 |
|
---|
[53] | 52 | nlua_register_glm( lua_state );
|
---|
| 53 |
|
---|
[52] | 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 | }
|
---|
| 60 |
|
---|
| 61 | // run the Lua script
|
---|
| 62 | luaL_dofile(lua_state, "init.lua");
|
---|
| 63 |
|
---|
[62] | 64 | for (;;)
|
---|
[52] | 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 )
|
---|
| 97 | {
|
---|
| 98 | std::cout << nlua_typecontent( lua_state, i ) << std::endl;
|
---|
| 99 | }
|
---|
| 100 | }
|
---|
| 101 | lua_settop( lua_state, stack );
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 |
|
---|
| 105 | // close the Lua state
|
---|
| 106 | lua_close(lua_state);
|
---|
| 107 | NV_LOG( nv::LOG_NOTICE, "Logging stopped" );
|
---|
| 108 |
|
---|
| 109 | return 0;
|
---|
| 110 | }
|
---|
| 111 |
|
---|