Mercurial > hg > orthanc
comparison Core/Lua/LuaFunctionCall.cpp @ 386:7dec4f3c922c lua-scripting
lua wrapper
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 29 Apr 2013 17:14:10 +0200 |
parents | |
children | 08eca5d86aad |
comparison
equal
deleted
inserted
replaced
384:18fe778eeb95 | 386:7dec4f3c922c |
---|---|
1 /** | |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2013 Medical Physics Department, CHU of Liege, | |
4 * Belgium | |
5 * | |
6 * This program is free software: you can redistribute it and/or | |
7 * modify it under the terms of the GNU General Public License as | |
8 * published by the Free Software Foundation, either version 3 of the | |
9 * License, or (at your option) any later version. | |
10 * | |
11 * In addition, as a special exception, the copyright holders of this | |
12 * program give permission to link the code of its release with the | |
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
14 * that use the same license as the "OpenSSL" library), and distribute | |
15 * the linked executables. You must obey the GNU General Public License | |
16 * in all respects for all of the code used other than "OpenSSL". If you | |
17 * modify file(s) with this exception, you may extend this exception to | |
18 * your version of the file(s), but you are not obligated to do so. If | |
19 * you do not wish to do so, delete this exception statement from your | |
20 * version. If you delete this exception statement from all source files | |
21 * in the program, then also delete it here. | |
22 * | |
23 * This program is distributed in the hope that it will be useful, but | |
24 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
26 * General Public License for more details. | |
27 * | |
28 * You should have received a copy of the GNU General Public License | |
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
30 **/ | |
31 | |
32 | |
33 #include "LuaFunctionCall.h" | |
34 | |
35 | |
36 namespace Orthanc | |
37 { | |
38 void LuaFunctionCall::CheckAlreadyExecuted() | |
39 { | |
40 if (isExecuted_) | |
41 { | |
42 throw LuaException("Arguments cannot be pushed after the function is executed"); | |
43 } | |
44 } | |
45 | |
46 LuaFunctionCall::LuaFunctionCall(LuaContext& context, | |
47 const char* functionName) : | |
48 context_(context), | |
49 lock_(context.mutex_), | |
50 isExecuted_(false) | |
51 { | |
52 // Clear the stack to fulfill the invariant | |
53 lua_settop(context_.lua_, 0); | |
54 lua_getglobal(context_.lua_, functionName); | |
55 } | |
56 | |
57 void LuaFunctionCall::PushString(const std::string& value) | |
58 { | |
59 CheckAlreadyExecuted(); | |
60 lua_pushstring(context_.lua_, value.c_str()); | |
61 } | |
62 | |
63 void LuaFunctionCall::PushBoolean(bool value) | |
64 { | |
65 CheckAlreadyExecuted(); | |
66 lua_pushboolean(context_.lua_, value); | |
67 } | |
68 | |
69 void LuaFunctionCall::PushInteger(int value) | |
70 { | |
71 CheckAlreadyExecuted(); | |
72 lua_pushinteger(context_.lua_, value); | |
73 } | |
74 | |
75 void LuaFunctionCall::PushDouble(double value) | |
76 { | |
77 CheckAlreadyExecuted(); | |
78 lua_pushnumber(context_.lua_, value); | |
79 } | |
80 | |
81 void LuaFunctionCall::PushJSON(const Json::Value& value) | |
82 { | |
83 CheckAlreadyExecuted(); | |
84 | |
85 if (value.isString()) | |
86 { | |
87 lua_pushstring(context_.lua_, value.asCString()); | |
88 } | |
89 else if (value.isDouble()) | |
90 { | |
91 lua_pushnumber(context_.lua_, value.asDouble()); | |
92 } | |
93 else if (value.isInt()) | |
94 { | |
95 lua_pushinteger(context_.lua_, value.asInt()); | |
96 } | |
97 else if (value.isUInt()) | |
98 { | |
99 lua_pushinteger(context_.lua_, value.asUInt()); | |
100 } | |
101 else if (value.isBool()) | |
102 { | |
103 lua_pushboolean(context_.lua_, value.asBool()); | |
104 } | |
105 else if (value.isNull()) | |
106 { | |
107 lua_pushnil(context_.lua_); | |
108 } | |
109 else if (value.isArray()) | |
110 { | |
111 lua_newtable(context_.lua_); | |
112 | |
113 // http://lua-users.org/wiki/SimpleLuaApiExample | |
114 for (Json::Value::ArrayIndex i = 0; i < value.size(); i++) | |
115 { | |
116 // Push the table index (note the "+1" because of Lua conventions) | |
117 lua_pushnumber(context_.lua_, i + 1); | |
118 | |
119 // Push the value of the cell | |
120 PushJSON(value[i]); | |
121 | |
122 // Stores the pair in the table | |
123 lua_rawset(context_.lua_, -3); | |
124 } | |
125 } | |
126 else if (value.isObject()) | |
127 { | |
128 lua_newtable(context_.lua_); | |
129 | |
130 Json::Value::Members members = value.getMemberNames(); | |
131 | |
132 for (Json::Value::Members::const_iterator | |
133 it = members.begin(); it != members.end(); it++) | |
134 { | |
135 // Push the index of the cell | |
136 lua_pushstring(context_.lua_, it->c_str()); | |
137 | |
138 // Push the value of the cell | |
139 PushJSON(value[*it]); | |
140 | |
141 // Stores the pair in the table | |
142 lua_rawset(context_.lua_, -3); | |
143 } | |
144 } | |
145 else | |
146 { | |
147 throw LuaException("Unsupported JSON conversion"); | |
148 } | |
149 } | |
150 | |
151 void LuaFunctionCall::Execute(int numOutputs) | |
152 { | |
153 CheckAlreadyExecuted(); | |
154 | |
155 assert(lua_gettop(context_.lua_) >= 1); | |
156 int nargs = lua_gettop(context_.lua_) - 1; | |
157 int error = lua_pcall(context_.lua_, nargs, numOutputs, 0); | |
158 | |
159 if (error) | |
160 { | |
161 assert(lua_gettop(context_.lua_) >= 1); | |
162 | |
163 std::string description(lua_tostring(context_.lua_, -1)); | |
164 lua_pop(context_.lua_, 1); /* pop error message from the stack */ | |
165 throw LuaException(description); | |
166 } | |
167 | |
168 if (lua_gettop(context_.lua_) < numOutputs) | |
169 { | |
170 throw LuaException("The function does not give the expected number of outputs"); | |
171 } | |
172 | |
173 isExecuted_ = true; | |
174 } | |
175 | |
176 bool LuaFunctionCall::ExecutePredicate() | |
177 { | |
178 Execute(1); | |
179 | |
180 if (lua_gettop(context_.lua_) == 0) | |
181 { | |
182 throw LuaException("No output was provided by the function"); | |
183 } | |
184 | |
185 if (!lua_isboolean(context_.lua_, 1)) | |
186 { | |
187 throw LuaException("The function is not a predicate (only true/false outputs allowed)"); | |
188 } | |
189 | |
190 return lua_toboolean(context_.lua_, 1) != 0; | |
191 } | |
192 } |