| 1 |
// ____ _ __ |
| 2 |
// / __ )____ _____ | | / /___ ___________ |
| 3 |
// / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/ |
| 4 |
// / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ ) |
| 5 |
// /_____/\____/____/ |__/|__/\__,_/_/ /____/ |
| 6 |
// |
| 7 |
// A futuristic real-time strategy game. |
| 8 |
// This file is part of Bos Wars. |
| 9 |
// |
| 10 |
/**@name luacallback.cpp - Lua callback. */ |
| 11 |
// |
| 12 |
// (c) Copyright 2008 by Francois Beerten |
| 13 |
// |
| 14 |
// This program is free software; you can redistribute it and/or modify |
| 15 |
// it under the terms of the GNU General Public License as published by |
| 16 |
// the Free Software Foundation; only version 2 of the License. |
| 17 |
// |
| 18 |
// This program is distributed in the hope that it will be useful, |
| 19 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 |
// GNU General Public License for more details. |
| 22 |
// |
| 23 |
// You should have received a copy of the GNU General Public License |
| 24 |
// along with this program; if not, write to the Free Software |
| 25 |
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 26 |
// 02111-1307, USA. |
| 27 |
// |
| 28 |
|
| 29 |
//@{ |
| 30 |
|
| 31 |
|
| 32 |
#include "luacallback.h" |
| 33 |
#include "stratagus.h" |
| 34 |
#include "script.h" |
| 35 |
|
| 36 |
|
| 37 |
/** |
| 38 |
** LuaCallback constructor |
| 39 |
** |
| 40 |
** @param l Lua state |
| 41 |
** @param f Listener function |
| 42 |
*/ |
| 43 |
LuaCallback::LuaCallback(lua_State *l, lua_Object f) : |
| 44 |
luastate(l), arguments(0) |
| 45 |
{ |
| 46 |
if (!lua_isfunction(l, f)) { |
| 47 |
LuaError(l, "Argument isn't a function"); |
| 48 |
Assert(0); |
| 49 |
} |
| 50 |
lua_pushvalue(l, f); |
| 51 |
luaref = luaL_ref(l, LUA_REGISTRYINDEX); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
** Push the preamble on the stack to call the callback. |
| 56 |
** Call this function before pushing the arguments on the lua stack. |
| 57 |
*/ |
| 58 |
void LuaCallback::pushPreamble() |
| 59 |
{ |
| 60 |
base = lua_gettop(luastate); |
| 61 |
lua_pushcfunction(Lua, luatraceback); // at base+1 |
| 62 |
lua_rawgeti(luastate, LUA_REGISTRYINDEX, luaref); // at base+2 |
| 63 |
arguments = 0; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
/** |
| 68 |
** Push a string argument for the callback on the stack. |
| 69 |
** |
| 70 |
** @param value the integer to push on the stack |
| 71 |
*/ |
| 72 |
void LuaCallback::pushInteger(int value) |
| 73 |
{ |
| 74 |
lua_pushnumber(luastate, value); |
| 75 |
arguments++; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
/** |
| 80 |
** Push a string argument for the callback on the stack. |
| 81 |
** |
| 82 |
** @param s the string to push on the stack |
| 83 |
*/ |
| 84 |
void LuaCallback::pushString(const std::string &s) |
| 85 |
{ |
| 86 |
lua_pushstring(luastate, s.c_str()); |
| 87 |
arguments++; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
/** |
| 92 |
** Called when an action is recieved from a Widget. It is used |
| 93 |
** to be able to recieve a notification that an action has |
| 94 |
** occured. |
| 95 |
*/ |
| 96 |
void LuaCallback::run() |
| 97 |
{ |
| 98 |
int status; |
| 99 |
|
| 100 |
//FIXME call error reporting function |
| 101 |
status = lua_pcall(luastate, arguments, 0, base + 1); |
| 102 |
if (status) { |
| 103 |
const char *msg; |
| 104 |
msg = lua_tostring(luastate, -1); |
| 105 |
if (msg == NULL) { |
| 106 |
msg = "(error with no message)"; |
| 107 |
} |
| 108 |
fprintf(stderr, "%s\n", msg); |
| 109 |
lua_pop(luastate, 1); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
** LuaActionListener destructor |
| 115 |
*/ |
| 116 |
LuaCallback::~LuaCallback() |
| 117 |
{ |
| 118 |
luaL_unref(luastate, LUA_REGISTRYINDEX, luaref); |
| 119 |
} |
| 120 |
|
| 121 |
//@} |