[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>
|
---|
[52] | 10 |
|
---|
[62] | 11 | struct test_struct
|
---|
| 12 | {
|
---|
| 13 | std::string f;
|
---|
| 14 | int i;
|
---|
| 15 | };
|
---|
| 16 |
|
---|
| 17 | NV_REGISTER_NAME( test_struct )
|
---|
| 18 |
|
---|
[52] | 19 | int main(int, char* [])
|
---|
| 20 | {
|
---|
[62] | 21 | nv::type_database db;
|
---|
| 22 | nv::object::register_type( &db );
|
---|
| 23 |
|
---|
| 24 | db.create_type<int>();
|
---|
| 25 | db.create_type<std::string>();
|
---|
| 26 | nv::type_field fields[] = {
|
---|
| 27 | nv::type_field("f", &test_struct::f ),
|
---|
| 28 | nv::type_field("i", &test_struct::i ),
|
---|
| 29 | };
|
---|
| 30 | db.create_type<test_struct>().fields( fields );
|
---|
| 31 |
|
---|
| 32 |
|
---|
[52] | 33 | nv::logger log(nv::LOG_TRACE);
|
---|
| 34 | log.add_sink( new nv::log_file_sink("log.txt"), nv::LOG_TRACE );
|
---|
| 35 | log.add_sink( new nv::log_console_sink(), nv::LOG_TRACE );
|
---|
| 36 | nv::load_lua_library();
|
---|
[62] | 37 |
|
---|
[52] | 38 | NV_LOG( nv::LOG_NOTICE, "Logging started" );
|
---|
| 39 |
|
---|
| 40 | // create new Lua state
|
---|
| 41 | lua_State *lua_state;
|
---|
| 42 | lua_state = luaL_newstate();
|
---|
| 43 |
|
---|
| 44 | // load Lua libraries
|
---|
| 45 | static const luaL_Reg lualibs[] =
|
---|
| 46 | {
|
---|
| 47 | { "base", luaopen_base },
|
---|
| 48 | { NULL, NULL}
|
---|
| 49 | };
|
---|
| 50 |
|
---|
[53] | 51 | nlua_register_glm( lua_state );
|
---|
| 52 |
|
---|
[52] | 53 | const luaL_Reg *lib = lualibs;
|
---|
| 54 | for(; lib->func != NULL; lib++)
|
---|
| 55 | {
|
---|
| 56 | lib->func(lua_state);
|
---|
| 57 | lua_settop(lua_state, 0);
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | // run the Lua script
|
---|
| 61 | luaL_dofile(lua_state, "init.lua");
|
---|
| 62 |
|
---|
[62] | 63 | for (;;)
|
---|
[52] | 64 | {
|
---|
| 65 | std::string input;
|
---|
| 66 | int stack = lua_gettop( lua_state );
|
---|
| 67 | std::cout << "LUA (" << stack << ") > ";
|
---|
| 68 | std::getline( std::cin, input );
|
---|
| 69 | if (input == "quit")
|
---|
| 70 | {
|
---|
| 71 | break;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | if (input.find("=", 0) == std::string::npos &&
|
---|
| 75 | input.find("if", 0) == std::string::npos &&
|
---|
| 76 | input.find("return", 0) == std::string::npos)
|
---|
| 77 | {
|
---|
| 78 | input = "return " + input;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | std::cout << "> " << input << std::endl;
|
---|
| 82 |
|
---|
| 83 | int code = luaL_loadstring( lua_state, input.c_str() );
|
---|
| 84 | if (code == 0) code = lua_pcall( lua_state, 0, LUA_MULTRET, 0);
|
---|
| 85 | if (code != 0)
|
---|
| 86 | {
|
---|
| 87 | std::string error = lua_tostring( lua_state, -1 );
|
---|
| 88 | std::cout << "ERROR : " << error << std::endl;
|
---|
| 89 | lua_settop( lua_state, stack );
|
---|
| 90 | continue;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | if (lua_gettop( lua_state ) > stack)
|
---|
| 94 | {
|
---|
| 95 | for ( int i = stack+1; i <= lua_gettop( lua_state ); ++i )
|
---|
| 96 | {
|
---|
| 97 | std::cout << nlua_typecontent( lua_state, i ) << std::endl;
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
| 100 | lua_settop( lua_state, stack );
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 |
|
---|
| 104 | // close the Lua state
|
---|
| 105 | lua_close(lua_state);
|
---|
| 106 | NV_LOG( nv::LOG_NOTICE, "Logging stopped" );
|
---|
| 107 |
|
---|
| 108 | return 0;
|
---|
| 109 | }
|
---|
| 110 |
|
---|