annotate Core/Lua/LuaFunctionCall.cpp @ 1205:bb1ad2fbf914

news
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 03 Nov 2014 13:40:14 +0100
parents 92f4bf2c5d73
children 6e7e5ed91c2d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
386
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
689
2d0a347e8cfc switch to 2014
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 656
diff changeset
3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege,
386
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Belgium
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 *
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 * This program is free software: you can redistribute it and/or
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * modify it under the terms of the GNU General Public License as
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * published by the Free Software Foundation, either version 3 of the
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * License, or (at your option) any later version.
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 *
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 * In addition, as a special exception, the copyright holders of this
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * program give permission to link the code of its release with the
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * that use the same license as the "OpenSSL" library), and distribute
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * the linked executables. You must obey the GNU General Public License
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 * in all respects for all of the code used other than "OpenSSL". If you
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * modify file(s) with this exception, you may extend this exception to
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * your version of the file(s), but you are not obligated to do so. If
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 * you do not wish to do so, delete this exception statement from your
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 * version. If you delete this exception statement from all source files
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 * in the program, then also delete it here.
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 *
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 * This program is distributed in the hope that it will be useful, but
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 * General Public License for more details.
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 *
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 * You should have received a copy of the GNU General Public License
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 **/
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32
824
a811bdf8b8eb precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 689
diff changeset
33 #include "../PrecompiledHeaders.h"
386
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 #include "LuaFunctionCall.h"
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35
996
cf52f3bcb2b3 clarification of Lua classes wrt multithreading
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
36 #include <cassert>
997
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
37 #include <stdio.h>
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
38 #include <boost/lexical_cast.hpp>
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
39 #include <glog/logging.h>
386
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 namespace Orthanc
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 {
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 void LuaFunctionCall::CheckAlreadyExecuted()
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 {
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 if (isExecuted_)
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 {
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 throw LuaException("Arguments cannot be pushed after the function is executed");
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 }
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 }
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 LuaFunctionCall::LuaFunctionCall(LuaContext& context,
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 const char* functionName) :
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 context_(context),
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 isExecuted_(false)
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 {
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 // Clear the stack to fulfill the invariant
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 lua_settop(context_.lua_, 0);
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 lua_getglobal(context_.lua_, functionName);
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 }
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 void LuaFunctionCall::PushString(const std::string& value)
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 {
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 CheckAlreadyExecuted();
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 lua_pushstring(context_.lua_, value.c_str());
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 }
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 void LuaFunctionCall::PushBoolean(bool value)
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 {
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 CheckAlreadyExecuted();
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 lua_pushboolean(context_.lua_, value);
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 }
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 void LuaFunctionCall::PushInteger(int value)
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 {
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 CheckAlreadyExecuted();
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 lua_pushinteger(context_.lua_, value);
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 }
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 void LuaFunctionCall::PushDouble(double value)
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 {
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 CheckAlreadyExecuted();
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 lua_pushnumber(context_.lua_, value);
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 }
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84
997
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
85 void LuaFunctionCall::PushJson(const Json::Value& value)
386
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 {
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 CheckAlreadyExecuted();
1051
92f4bf2c5d73 HTTP GET in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1010
diff changeset
88 context_.PushJson(value);
386
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 }
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90
997
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
91 void LuaFunctionCall::ExecuteInternal(int numOutputs)
386
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 {
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 CheckAlreadyExecuted();
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 assert(lua_gettop(context_.lua_) >= 1);
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 int nargs = lua_gettop(context_.lua_) - 1;
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 int error = lua_pcall(context_.lua_, nargs, numOutputs, 0);
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 if (error)
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 {
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 assert(lua_gettop(context_.lua_) >= 1);
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 std::string description(lua_tostring(context_.lua_, -1));
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 lua_pop(context_.lua_, 1); /* pop error message from the stack */
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 throw LuaException(description);
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 }
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 if (lua_gettop(context_.lua_) < numOutputs)
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 {
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 throw LuaException("The function does not give the expected number of outputs");
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 }
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 isExecuted_ = true;
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 }
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 bool LuaFunctionCall::ExecutePredicate()
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 {
997
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
118 ExecuteInternal(1);
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
119
386
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 if (!lua_isboolean(context_.lua_, 1))
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 {
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 throw LuaException("The function is not a predicate (only true/false outputs allowed)");
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 }
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 return lua_toboolean(context_.lua_, 1) != 0;
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 }
997
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
127
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
128
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
129 static void PopJson(Json::Value& result,
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
130 lua_State* lua,
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
131 int top)
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
132 {
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
133 if (lua_istable(lua, top))
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
134 {
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
135 Json::Value tmp = Json::objectValue;
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
136 bool isArray = true;
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
137 size_t size = 0;
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
138
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
139 // http://stackoverflow.com/a/6142700/881731
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
140
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
141 // Push another reference to the table on top of the stack (so we know
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
142 // where it is, and this function can work for negative, positive and
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
143 // pseudo indices
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
144 lua_pushvalue(lua, top);
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
145 // stack now contains: -1 => table
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
146 lua_pushnil(lua);
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
147 // stack now contains: -1 => nil; -2 => table
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
148 while (lua_next(lua, -2))
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
149 {
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
150 // stack now contains: -1 => value; -2 => key; -3 => table
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
151 // copy the key so that lua_tostring does not modify the original
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
152 lua_pushvalue(lua, -2);
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
153 // stack now contains: -1 => key; -2 => value; -3 => key; -4 => table
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
154 std::string key(lua_tostring(lua, -1));
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
155 Json::Value v;
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
156 PopJson(v, lua, -2);
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
157
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
158 tmp[key] = v;
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
159
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
160 size += 1;
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
161 try
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
162 {
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
163 if (boost::lexical_cast<size_t>(key) != size)
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
164 {
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
165 isArray = false;
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
166 }
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
167 }
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
168 catch (boost::bad_lexical_cast&)
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
169 {
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
170 isArray = false;
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
171 }
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
172
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
173 // pop value + copy of key, leaving original key
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
174 lua_pop(lua, 2);
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
175 // stack now contains: -1 => key; -2 => table
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
176 }
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
177 // stack now contains: -1 => table (when lua_next returns 0 it pops the key
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
178 // but does not push anything.)
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
179 // Pop table
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
180 lua_pop(lua, 1);
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
181
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
182 // Stack is now the same as it was on entry to this function
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
183
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
184 if (isArray)
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
185 {
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
186 result = Json::arrayValue;
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
187 for (size_t i = 0; i < size; i++)
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
188 {
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
189 result.append(tmp[boost::lexical_cast<std::string>(i + 1)]);
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
190 }
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
191 }
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
192 else
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
193 {
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
194 result = tmp;
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
195 }
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
196 }
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
197 else if (lua_isnumber(lua, top))
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
198 {
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
199 result = static_cast<float>(lua_tonumber(lua, top));
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
200 }
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
201 else if (lua_isstring(lua, top))
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
202 {
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
203 result = std::string(lua_tostring(lua, top));
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
204 }
1010
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 997
diff changeset
205 else if (lua_isboolean(lua, top))
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 997
diff changeset
206 {
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 997
diff changeset
207 result = static_cast<bool>(lua_toboolean(lua, top));
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 997
diff changeset
208 }
997
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
209 else
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
210 {
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
211 LOG(WARNING) << "Unsupported Lua type when returning Json";
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
212 result = Json::nullValue;
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
213 }
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
214 }
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
215
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
216
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
217 void LuaFunctionCall::ExecuteToJson(Json::Value& result)
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
218 {
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
219 ExecuteInternal(1);
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
220 PopJson(result, context_.lua_, lua_gettop(context_.lua_));
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
221 }
386
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
222 }