xoreos  0.0.5
function.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #include "lua/lauxlib.h"
26 
27 #include "src/common/error.h"
28 
30 #include "src/aurora/lua/stack.h"
33 
34 namespace Aurora {
35 
36 namespace Lua {
37 
38 FunctionRef::FunctionRef() : _luaState(0), _ref(LUA_REFNIL) {
39 
40 }
41 
42 FunctionRef::FunctionRef(const Stack &stack, int index) : _luaState(&stack.getLuaState()), _ref(LUA_REFNIL) {
43  lua_pushvalue(_luaState, index);
44  _ref = lua_ref(_luaState, true);
45 }
46 
47 FunctionRef::FunctionRef(lua_State &state, int index) : _luaState(&state), _ref(LUA_REFNIL) {
48  lua_pushvalue(_luaState, index);
49  _ref = lua_ref(_luaState, true);
50 }
51 
52 FunctionRef::FunctionRef(const FunctionRef &fn) : _luaState(fn._luaState), _ref(LUA_REFNIL) {
53  assert(fn._luaState && fn._ref != LUA_REFNIL);
54 
55  lua_getref(_luaState, fn._ref);
56  _ref = lua_ref(_luaState, true);
57 }
58 
60  assert(_ref != LUA_REFNIL);
61 
62  lua_unref(_luaState, _ref);
63 }
64 
66  if (this == &fn) {
67  return *this;
68  }
69 
70  assert(fn._luaState && fn._ref != LUA_REFNIL);
71 
72  lua_State *oldState = _luaState;
73  const int oldRef = _ref;
74 
75  lua_getref(fn._luaState, fn._ref);
76  _luaState = fn._luaState;
77  _ref = lua_ref(_luaState, true);
78 
79  if (oldState && oldRef != LUA_REFNIL) {
80  lua_unref(oldState, oldRef);
81  }
82  return *this;
83 }
84 
85 Variables FunctionRef::call(const Variables &params) const {
86  StackGuard guard(*_luaState);
87 
88  const int savedTop = lua_gettop(_luaState);
89  pushSelf();
90 
91  Stack stack(*_luaState);
92  stack.pushVariables(params);
93 
94  if (lua_pcall(&stack.getLuaState(), params.size(), LUA_MULTRET, 0) != 0) {
95  throw Common::Exception("Failed to call Lua function:\n\t%s", lua_tostring(&stack.getLuaState(), -1));
96  }
97 
98  const int retsCount = stack.getSize() - savedTop;
99  return stack.getVariablesFromTop(retsCount);
100 }
101 
103  return call(Variables());
104 }
105 
107  Variables params;
108  params.push_back(v);
109  return call(params);
110 }
111 
112 Variables FunctionRef::call(const Variable &v1, const Variable &v2) const {
113  Variables params;
114  params.push_back(v1);
115  params.push_back(v2);
116  return call(params);
117 }
118 
119 Variables FunctionRef::call(const Variable &v1, const Variable &v2, const Variable &v3) const {
120  Variables params;
121  params.push_back(v1);
122  params.push_back(v2);
123  params.push_back(v3);
124  return call(params);
125 }
126 
127 Variables FunctionRef::call(const Variable &v1, const Variable &v2, const Variable &v3, const Variable &v4) const {
128  Variables params;
129  params.push_back(v1);
130  params.push_back(v2);
131  params.push_back(v3);
132  params.push_back(v4);
133  return call(params);
134 }
135 
136 lua_State &FunctionRef::getLuaState() const {
137  assert(_luaState);
138 
139  return *_luaState;
140 }
141 
142 int FunctionRef::getRef() const {
143  return _ref;
144 }
145 
146 void FunctionRef::pushSelf() const {
147  assert(_luaState && _ref != LUA_REFNIL);
148 
149  lua_getref(_luaState, _ref);
150 }
151 
152 } // End of namespace Lua
153 
154 } // End of namespace Aurora
Variables getVariablesFromTop(int count) const
Definition: stack.cpp:254
void pushVariables(const Variables &vars)
Definition: stack.cpp:128
A reference to a Lua function.
int getSize() const
Return the number of elements in the stack.
Definition: stack.cpp:48
Variables call() const
Definition: function.cpp:102
Lua stack guard.
Basic exceptions to throw.
lua_State & getLuaState() const
Definition: function.cpp:136
StackException Exception
Definition: error.h:59
Lua stack guard.
Definition: stackguard.h:37
A Lua stack wrapper.
Definition: stack.h:41
std::vector< Variable > Variables
Definition: types.h:50
A reference to a Lua function.
Definition: function.h:35
lua_State * _luaState
Definition: function.h:57
Lua variable.
A Lua stack wrapper.
const FunctionRef & operator=(const FunctionRef &fn)
Definition: function.cpp:65
lua_State & getLuaState() const
Return the underlying Lua state.
Definition: stack.cpp:301