comparison Core/Lua/LuaContext.cpp @ 418:b79bf2f4ab2e

execution of lua through REST
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 07 May 2013 12:03:25 +0200
parents 941ea46e9e26
children 2d0a347e8cfc
comparison
equal deleted inserted replaced
417:7441037663cd 418:b79bf2f4ab2e
40 #include <lauxlib.h> 40 #include <lauxlib.h>
41 } 41 }
42 42
43 namespace Orthanc 43 namespace Orthanc
44 { 44 {
45 int LuaContext::PrintToLog(lua_State *L) 45 int LuaContext::PrintToLog(lua_State *state)
46 { 46 {
47 // Get the pointer to the "LuaContext" underlying object
48 lua_getglobal(state, "_LuaContext");
49 assert(lua_type(state, -1) == LUA_TLIGHTUSERDATA);
50 LuaContext* that = const_cast<LuaContext*>(reinterpret_cast<const LuaContext*>(lua_topointer(state, -1)));
51 assert(that != NULL);
52 lua_pop(state, 1);
53
47 // http://medek.wordpress.com/2009/02/03/wrapping-lua-errors-and-print-function/ 54 // http://medek.wordpress.com/2009/02/03/wrapping-lua-errors-and-print-function/
48 int nArgs = lua_gettop(L); 55 int nArgs = lua_gettop(state);
49 lua_getglobal(L, "tostring"); 56 lua_getglobal(state, "tostring");
50 57
51 // Make sure you start at 1 *NOT* 0 for arrays in Lua. 58 // Make sure you start at 1 *NOT* 0 for arrays in Lua.
52 std::string result; 59 std::string result;
53 60
54 for (int i = 1; i <= nArgs; i++) 61 for (int i = 1; i <= nArgs; i++)
55 { 62 {
56 const char *s; 63 const char *s;
57 lua_pushvalue(L, -1); 64 lua_pushvalue(state, -1);
58 lua_pushvalue(L, i); 65 lua_pushvalue(state, i);
59 lua_call(L, 1, 1); 66 lua_call(state, 1, 1);
60 s = lua_tostring(L, -1); 67 s = lua_tostring(state, -1);
61 68
62 if (result.size() > 0) 69 if (result.size() > 0)
63 result.append(", "); 70 result.append(", ");
64 71
65 if (s == NULL) 72 if (s == NULL)
66 result.append("<No conversion to string>"); 73 result.append("<No conversion to string>");
67 else 74 else
68 result.append(s); 75 result.append(s);
69 76
70 lua_pop(L, 1); 77 lua_pop(state, 1);
71 } 78 }
72 79
73 LOG(INFO) << "Lua says: " << result; 80 LOG(INFO) << "Lua says: " << result;
81 that->log_.append(result);
82 that->log_.append("\n");
74 83
75 return 0; 84 return 0;
76 } 85 }
77 86
78 87
84 throw LuaException("Unable to create the Lua context"); 93 throw LuaException("Unable to create the Lua context");
85 } 94 }
86 95
87 luaL_openlibs(lua_); 96 luaL_openlibs(lua_);
88 lua_register(lua_, "print", PrintToLog); 97 lua_register(lua_, "print", PrintToLog);
98
99 lua_pushlightuserdata(lua_, this);
100 lua_setglobal(lua_, "_LuaContext");
89 } 101 }
90 102
91 103
92 LuaContext::~LuaContext() 104 LuaContext::~LuaContext()
93 { 105 {
94 lua_close(lua_); 106 lua_close(lua_);
95 } 107 }
96 108
97 109
98 void LuaContext::Execute(const std::string& command) 110 void LuaContext::Execute(std::string* output,
111 const std::string& command)
99 { 112 {
100 boost::mutex::scoped_lock lock(mutex_); 113 boost::mutex::scoped_lock lock(mutex_);
101 114
102 lua_settop(lua_, 0); 115 log_.clear();
103
104 int error = (luaL_loadbuffer(lua_, command.c_str(), command.size(), "line") || 116 int error = (luaL_loadbuffer(lua_, command.c_str(), command.size(), "line") ||
105 lua_pcall(lua_, 0, 0, 0)); 117 lua_pcall(lua_, 0, 0, 0));
106 118
107 if (error) 119 if (error)
108 { 120 {
109 assert(lua_gettop(lua_) >= 1); 121 assert(lua_gettop(lua_) >= 1);
110 122
111 std::string description(lua_tostring(lua_, -1)); 123 std::string description(lua_tostring(lua_, -1));
112 lua_pop(lua_, 1); /* pop error message from the stack */ 124 lua_pop(lua_, 1); /* pop error message from the stack */
113 throw LuaException(description); 125 throw LuaException(description);
126 }
127
128 if (output != NULL)
129 {
130 *output = log_;
114 } 131 }
115 } 132 }
116 133
117 134
118 void LuaContext::Execute(EmbeddedResources::FileResourceId resource) 135 void LuaContext::Execute(EmbeddedResources::FileResourceId resource)