Fixed issue #15 that caused lua stack to grow indefinitely

This commit is contained in:
Eduardo Bart 2012-07-04 09:08:40 -03:00
parent 46e3fbe045
commit 71bed49f80
1 changed files with 13 additions and 3 deletions

View File

@ -37,9 +37,12 @@ public:
/// if any lua error occurs, it will be reported to stdout and return 0 results /// if any lua error occurs, it will be reported to stdout and return 0 results
/// @return the number of results /// @return the number of results
template<typename... T> template<typename... T>
int callLuaField(const std::string& field, const T&... args); int luaCallField(const std::string& field, const T&... args);
template<typename R, typename... T> template<typename R, typename... T>
R callLuaField(const std::string& field, const T&... args); R callLuaField(const std::string& field, const T&... args);
template<typename... T>
void callLuaField(const std::string& field, const T&... args);
/// Returns true if the lua field exists /// Returns true if the lua field exists
bool hasLuaField(const std::string& field); bool hasLuaField(const std::string& field);
@ -87,7 +90,7 @@ private:
#include "luainterface.h" #include "luainterface.h"
template<typename... T> template<typename... T>
int LuaObject::callLuaField(const std::string& field, const T&... args) { int LuaObject::luaCallField(const std::string& field, const T&... args) {
// note that the field must be retrieved from this object lua value // note that the field must be retrieved from this object lua value
// to force using the __index metamethod of it's metatable // to force using the __index metamethod of it's metatable
// so cannot use LuaObject::getField here // so cannot use LuaObject::getField here
@ -109,7 +112,7 @@ int LuaObject::callLuaField(const std::string& field, const T&... args) {
template<typename R, typename... T> template<typename R, typename... T>
R LuaObject::callLuaField(const std::string& field, const T&... args) { R LuaObject::callLuaField(const std::string& field, const T&... args) {
R result; R result;
int rets = callLuaField(field, args...); int rets = luaCallField(field, args...);
if(rets > 0) { if(rets > 0) {
assert(rets == 1); assert(rets == 1);
result = g_lua.polymorphicPop<R>(); result = g_lua.polymorphicPop<R>();
@ -118,6 +121,13 @@ R LuaObject::callLuaField(const std::string& field, const T&... args) {
return result; return result;
} }
template<typename... T>
void LuaObject::callLuaField(const std::string& field, const T&... args) {
int rets = luaCallField(field, args...);
if(rets > 0)
g_lua.pop(rets);
}
template<typename T> template<typename T>
void LuaObject::setLuaField(const std::string& key, const T& value) { void LuaObject::setLuaField(const std::string& key, const T& value) {
g_lua.polymorphicPush(value); g_lua.polymorphicPush(value);