Mercurial > hg > orthanc
comparison OrthancFramework/Sources/Lua/LuaFunctionCall.cpp @ 4044:d25f4c0fa160 framework
splitting code into OrthancFramework and OrthancServer
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 10 Jun 2020 20:30:34 +0200 |
parents | Core/Lua/LuaFunctionCall.cpp@94f4a18a79cc |
children | c02a2d9efbc2 |
comparison
equal
deleted
inserted
replaced
4043:6c6239aec462 | 4044:d25f4c0fa160 |
---|---|
1 /** | |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium | |
6 * | |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU General Public License as | |
9 * published by the Free Software Foundation, either version 3 of the | |
10 * License, or (at your option) any later version. | |
11 * | |
12 * In addition, as a special exception, the copyright holders of this | |
13 * program give permission to link the code of its release with the | |
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
15 * that use the same license as the "OpenSSL" library), and distribute | |
16 * the linked executables. You must obey the GNU General Public License | |
17 * in all respects for all of the code used other than "OpenSSL". If you | |
18 * modify file(s) with this exception, you may extend this exception to | |
19 * your version of the file(s), but you are not obligated to do so. If | |
20 * you do not wish to do so, delete this exception statement from your | |
21 * version. If you delete this exception statement from all source files | |
22 * in the program, then also delete it here. | |
23 * | |
24 * This program is distributed in the hope that it will be useful, but | |
25 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
27 * General Public License for more details. | |
28 * | |
29 * You should have received a copy of the GNU General Public License | |
30 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
31 **/ | |
32 | |
33 | |
34 #include "../PrecompiledHeaders.h" | |
35 #include "LuaFunctionCall.h" | |
36 | |
37 #include "../OrthancException.h" | |
38 #include "../Logging.h" | |
39 | |
40 #include <cassert> | |
41 #include <stdio.h> | |
42 #include <boost/lexical_cast.hpp> | |
43 | |
44 namespace Orthanc | |
45 { | |
46 void LuaFunctionCall::CheckAlreadyExecuted() | |
47 { | |
48 if (isExecuted_) | |
49 { | |
50 throw OrthancException(ErrorCode_LuaAlreadyExecuted); | |
51 } | |
52 } | |
53 | |
54 LuaFunctionCall::LuaFunctionCall(LuaContext& context, | |
55 const char* functionName) : | |
56 context_(context), | |
57 isExecuted_(false) | |
58 { | |
59 // Clear the stack to fulfill the invariant | |
60 lua_settop(context_.lua_, 0); | |
61 lua_getglobal(context_.lua_, functionName); | |
62 } | |
63 | |
64 void LuaFunctionCall::PushString(const std::string& value) | |
65 { | |
66 CheckAlreadyExecuted(); | |
67 lua_pushlstring(context_.lua_, value.c_str(), value.size()); | |
68 } | |
69 | |
70 void LuaFunctionCall::PushBoolean(bool value) | |
71 { | |
72 CheckAlreadyExecuted(); | |
73 lua_pushboolean(context_.lua_, value); | |
74 } | |
75 | |
76 void LuaFunctionCall::PushInteger(int value) | |
77 { | |
78 CheckAlreadyExecuted(); | |
79 lua_pushinteger(context_.lua_, value); | |
80 } | |
81 | |
82 void LuaFunctionCall::PushDouble(double value) | |
83 { | |
84 CheckAlreadyExecuted(); | |
85 lua_pushnumber(context_.lua_, value); | |
86 } | |
87 | |
88 void LuaFunctionCall::PushJson(const Json::Value& value) | |
89 { | |
90 CheckAlreadyExecuted(); | |
91 context_.PushJson(value); | |
92 } | |
93 | |
94 void LuaFunctionCall::ExecuteInternal(int numOutputs) | |
95 { | |
96 CheckAlreadyExecuted(); | |
97 | |
98 assert(lua_gettop(context_.lua_) >= 1); | |
99 int nargs = lua_gettop(context_.lua_) - 1; | |
100 int error = lua_pcall(context_.lua_, nargs, numOutputs, 0); | |
101 | |
102 if (error) | |
103 { | |
104 assert(lua_gettop(context_.lua_) >= 1); | |
105 | |
106 std::string description(lua_tostring(context_.lua_, -1)); | |
107 lua_pop(context_.lua_, 1); /* pop error message from the stack */ | |
108 | |
109 throw OrthancException(ErrorCode_CannotExecuteLua, description); | |
110 } | |
111 | |
112 if (lua_gettop(context_.lua_) < numOutputs) | |
113 { | |
114 throw OrthancException(ErrorCode_LuaBadOutput); | |
115 } | |
116 | |
117 isExecuted_ = true; | |
118 } | |
119 | |
120 bool LuaFunctionCall::ExecutePredicate() | |
121 { | |
122 ExecuteInternal(1); | |
123 | |
124 if (!lua_isboolean(context_.lua_, 1)) | |
125 { | |
126 throw OrthancException(ErrorCode_NotLuaPredicate); | |
127 } | |
128 | |
129 return lua_toboolean(context_.lua_, 1) != 0; | |
130 } | |
131 | |
132 | |
133 void LuaFunctionCall::ExecuteToJson(Json::Value& result, | |
134 bool keepStrings) | |
135 { | |
136 ExecuteInternal(1); | |
137 context_.GetJson(result, context_.lua_, lua_gettop(context_.lua_), keepStrings); | |
138 } | |
139 | |
140 | |
141 void LuaFunctionCall::ExecuteToString(std::string& result) | |
142 { | |
143 ExecuteInternal(1); | |
144 | |
145 int top = lua_gettop(context_.lua_); | |
146 if (lua_isstring(context_.lua_, top)) | |
147 { | |
148 result = lua_tostring(context_.lua_, top); | |
149 } | |
150 else | |
151 { | |
152 throw OrthancException(ErrorCode_LuaReturnsNoString); | |
153 } | |
154 } | |
155 | |
156 | |
157 void LuaFunctionCall::PushStringMap(const std::map<std::string, std::string>& value) | |
158 { | |
159 Json::Value json = Json::objectValue; | |
160 | |
161 for (std::map<std::string, std::string>::const_iterator | |
162 it = value.begin(); it != value.end(); ++it) | |
163 { | |
164 json[it->first] = it->second; | |
165 } | |
166 | |
167 PushJson(json); | |
168 } | |
169 | |
170 | |
171 void LuaFunctionCall::PushDicom(const DicomMap& dicom) | |
172 { | |
173 DicomArray a(dicom); | |
174 PushDicom(a); | |
175 } | |
176 | |
177 | |
178 void LuaFunctionCall::PushDicom(const DicomArray& dicom) | |
179 { | |
180 Json::Value value = Json::objectValue; | |
181 | |
182 for (size_t i = 0; i < dicom.GetSize(); i++) | |
183 { | |
184 const DicomValue& v = dicom.GetElement(i).GetValue(); | |
185 std::string s = (v.IsNull() || v.IsBinary()) ? "" : v.GetContent(); | |
186 value[dicom.GetElement(i).GetTag().Format()] = s; | |
187 } | |
188 | |
189 PushJson(value); | |
190 } | |
191 } |