3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "common/c_internal.h"
23 std::string script_get_backtrace(lua_State *L)
26 lua_getfield(L, LUA_GLOBALSINDEX, "debug");
27 if(lua_istable(L, -1)){
28 lua_getfield(L, -1, "traceback");
29 if(lua_isfunction(L, -1)){
31 if(lua_isstring(L, -1)){
32 s += lua_tostring(L, -1);
44 void script_error(lua_State *L, const char *fmt, ...)
49 vsnprintf(buf, 10000, fmt, argp);
51 throw LuaError(L, buf);
54 // Push the list of callbacks (a lua table).
55 // Then push nargs arguments.
56 // Then call this function, which
57 // - runs the callbacks
58 // - removes the table and arguments from the lua stack
59 // - pushes the return value, computed depending on mode
60 void script_run_callbacks(lua_State *L, int nargs, RunCallbacksMode mode)
62 // Insert the return value into the lua stack, below the table
63 assert(lua_gettop(L) >= nargs + 1);
65 lua_insert(L, -(nargs + 1) - 1);
66 // Stack now looks like this:
67 // ... <return value = nil> <table> <arg#1> <arg#2> ... <arg#n>
69 int rv = lua_gettop(L) - nargs - 1;
73 luaL_checktype(L, table, LUA_TTABLE);
77 bool first_loop = true;
78 while(lua_next(L, table) != 0){
79 // key at index -2 and value at index -1
80 luaL_checktype(L, -1, LUA_TFUNCTION);
82 for(int i = 0; i < nargs; i++)
83 lua_pushvalue(L, arg+i);
84 if(lua_pcall(L, nargs, 1, 0))
85 script_error(L, "error: %s", lua_tostring(L, -1));
87 // Move return value to designated space in stack
90 // Result of first callback is always moved
94 // Otherwise, what happens depends on the mode
95 if(mode == RUN_CALLBACKS_MODE_FIRST)
97 else if(mode == RUN_CALLBACKS_MODE_LAST)
99 else if(mode == RUN_CALLBACKS_MODE_AND ||
100 mode == RUN_CALLBACKS_MODE_AND_SC){
101 if((bool)lua_toboolean(L, rv) == true &&
102 (bool)lua_toboolean(L, -1) == false)
107 else if(mode == RUN_CALLBACKS_MODE_OR ||
108 mode == RUN_CALLBACKS_MODE_OR_SC){
109 if((bool)lua_toboolean(L, rv) == false &&
110 (bool)lua_toboolean(L, -1) == true)
119 // Handle short circuit modes
120 if(mode == RUN_CALLBACKS_MODE_AND_SC &&
121 (bool)lua_toboolean(L, rv) == false)
123 else if(mode == RUN_CALLBACKS_MODE_OR_SC &&
124 (bool)lua_toboolean(L, rv) == true)
127 // value removed, keep key for next iteration
130 // Remove stuff from stack, leaving only the return value
133 // Fix return value in case no callbacks were called
135 if(mode == RUN_CALLBACKS_MODE_AND ||
136 mode == RUN_CALLBACKS_MODE_AND_SC){
138 lua_pushboolean(L, true);
140 else if(mode == RUN_CALLBACKS_MODE_OR ||
141 mode == RUN_CALLBACKS_MODE_OR_SC){
143 lua_pushboolean(L, false);