Mercurial > hg > orthanc
annotate Core/Lua/LuaFunctionCall.cpp @ 877:e8fde84805a6 Orthanc-0.7.6
Orthanc-0.7.6
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 11 Jun 2014 15:03:18 +0200 |
parents | a811bdf8b8eb |
children | cf52f3bcb2b3 |
rev | line source |
---|---|
386 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
689 | 3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege, |
386 | 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 | |
824
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
33 #include "../PrecompiledHeaders.h" |
386 | 34 #include "LuaFunctionCall.h" |
35 | |
36 | |
37 namespace Orthanc | |
38 { | |
39 void LuaFunctionCall::CheckAlreadyExecuted() | |
40 { | |
41 if (isExecuted_) | |
42 { | |
43 throw LuaException("Arguments cannot be pushed after the function is executed"); | |
44 } | |
45 } | |
46 | |
47 LuaFunctionCall::LuaFunctionCall(LuaContext& context, | |
48 const char* functionName) : | |
49 context_(context), | |
50 lock_(context.mutex_), | |
51 isExecuted_(false) | |
52 { | |
53 // Clear the stack to fulfill the invariant | |
54 lua_settop(context_.lua_, 0); | |
55 lua_getglobal(context_.lua_, functionName); | |
56 } | |
57 | |
58 void LuaFunctionCall::PushString(const std::string& value) | |
59 { | |
60 CheckAlreadyExecuted(); | |
61 lua_pushstring(context_.lua_, value.c_str()); | |
62 } | |
63 | |
64 void LuaFunctionCall::PushBoolean(bool value) | |
65 { | |
66 CheckAlreadyExecuted(); | |
67 lua_pushboolean(context_.lua_, value); | |
68 } | |
69 | |
70 void LuaFunctionCall::PushInteger(int value) | |
71 { | |
72 CheckAlreadyExecuted(); | |
73 lua_pushinteger(context_.lua_, value); | |
74 } | |
75 | |
76 void LuaFunctionCall::PushDouble(double value) | |
77 { | |
78 CheckAlreadyExecuted(); | |
79 lua_pushnumber(context_.lua_, value); | |
80 } | |
81 | |
82 void LuaFunctionCall::PushJSON(const Json::Value& value) | |
83 { | |
84 CheckAlreadyExecuted(); | |
85 | |
86 if (value.isString()) | |
87 { | |
88 lua_pushstring(context_.lua_, value.asCString()); | |
89 } | |
90 else if (value.isDouble()) | |
91 { | |
92 lua_pushnumber(context_.lua_, value.asDouble()); | |
93 } | |
94 else if (value.isInt()) | |
95 { | |
96 lua_pushinteger(context_.lua_, value.asInt()); | |
97 } | |
98 else if (value.isUInt()) | |
99 { | |
100 lua_pushinteger(context_.lua_, value.asUInt()); | |
101 } | |
102 else if (value.isBool()) | |
103 { | |
104 lua_pushboolean(context_.lua_, value.asBool()); | |
105 } | |
106 else if (value.isNull()) | |
107 { | |
108 lua_pushnil(context_.lua_); | |
109 } | |
110 else if (value.isArray()) | |
111 { | |
112 lua_newtable(context_.lua_); | |
113 | |
114 // http://lua-users.org/wiki/SimpleLuaApiExample | |
115 for (Json::Value::ArrayIndex i = 0; i < value.size(); i++) | |
116 { | |
117 // Push the table index (note the "+1" because of Lua conventions) | |
118 lua_pushnumber(context_.lua_, i + 1); | |
119 | |
120 // Push the value of the cell | |
121 PushJSON(value[i]); | |
122 | |
123 // Stores the pair in the table | |
124 lua_rawset(context_.lua_, -3); | |
125 } | |
126 } | |
127 else if (value.isObject()) | |
128 { | |
129 lua_newtable(context_.lua_); | |
130 | |
131 Json::Value::Members members = value.getMemberNames(); | |
132 | |
133 for (Json::Value::Members::const_iterator | |
656 | 134 it = members.begin(); it != members.end(); ++it) |
386 | 135 { |
136 // Push the index of the cell | |
137 lua_pushstring(context_.lua_, it->c_str()); | |
138 | |
139 // Push the value of the cell | |
140 PushJSON(value[*it]); | |
141 | |
142 // Stores the pair in the table | |
143 lua_rawset(context_.lua_, -3); | |
144 } | |
145 } | |
146 else | |
147 { | |
148 throw LuaException("Unsupported JSON conversion"); | |
149 } | |
150 } | |
151 | |
152 void LuaFunctionCall::Execute(int numOutputs) | |
153 { | |
154 CheckAlreadyExecuted(); | |
155 | |
156 assert(lua_gettop(context_.lua_) >= 1); | |
157 int nargs = lua_gettop(context_.lua_) - 1; | |
158 int error = lua_pcall(context_.lua_, nargs, numOutputs, 0); | |
159 | |
160 if (error) | |
161 { | |
162 assert(lua_gettop(context_.lua_) >= 1); | |
163 | |
164 std::string description(lua_tostring(context_.lua_, -1)); | |
165 lua_pop(context_.lua_, 1); /* pop error message from the stack */ | |
166 throw LuaException(description); | |
167 } | |
168 | |
169 if (lua_gettop(context_.lua_) < numOutputs) | |
170 { | |
171 throw LuaException("The function does not give the expected number of outputs"); | |
172 } | |
173 | |
174 isExecuted_ = true; | |
175 } | |
176 | |
177 bool LuaFunctionCall::ExecutePredicate() | |
178 { | |
179 Execute(1); | |
180 | |
181 if (lua_gettop(context_.lua_) == 0) | |
182 { | |
183 throw LuaException("No output was provided by the function"); | |
184 } | |
185 | |
186 if (!lua_isboolean(context_.lua_, 1)) | |
187 { | |
188 throw LuaException("The function is not a predicate (only true/false outputs allowed)"); | |
189 } | |
190 | |
191 return lua_toboolean(context_.lua_, 1) != 0; | |
192 } | |
193 } |