]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/common/c_internal.cpp
Omnicleanup: header cleanup, add ModApiUtil shared between game and mainmenu
[dragonfireclient.git] / src / script / common / c_internal.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "common/c_internal.h"
21 #include "debug.h"
22
23 std::string script_get_backtrace(lua_State *L)
24 {
25         std::string s;
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)){
30                         lua_call(L, 0, 1);
31                         if(lua_isstring(L, -1)){
32                                 s += lua_tostring(L, -1);
33                         }
34                         lua_pop(L, 1);
35                 }
36                 else{
37                         lua_pop(L, 1);
38                 }
39         }
40         lua_pop(L, 1);
41         return s;
42 }
43
44 void script_error(lua_State *L, const char *fmt, ...)
45 {
46         va_list argp;
47         va_start(argp, fmt);
48         char buf[10000];
49         vsnprintf(buf, 10000, fmt, argp);
50         va_end(argp);
51         throw LuaError(L, buf);
52 }
53
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)
61 {
62         // Insert the return value into the lua stack, below the table
63         assert(lua_gettop(L) >= nargs + 1);
64         lua_pushnil(L);
65         lua_insert(L, -(nargs + 1) - 1);
66         // Stack now looks like this:
67         // ... <return value = nil> <table> <arg#1> <arg#2> ... <arg#n>
68
69         int rv = lua_gettop(L) - nargs - 1;
70         int table = rv + 1;
71         int arg = table + 1;
72
73         luaL_checktype(L, table, LUA_TTABLE);
74
75         // Foreach
76         lua_pushnil(L);
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);
81                 // Call function
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));
86
87                 // Move return value to designated space in stack
88                 // Or pop it
89                 if(first_loop){
90                         // Result of first callback is always moved
91                         lua_replace(L, rv);
92                         first_loop = false;
93                 } else {
94                         // Otherwise, what happens depends on the mode
95                         if(mode == RUN_CALLBACKS_MODE_FIRST)
96                                 lua_pop(L, 1);
97                         else if(mode == RUN_CALLBACKS_MODE_LAST)
98                                 lua_replace(L, rv);
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)
103                                         lua_replace(L, rv);
104                                 else
105                                         lua_pop(L, 1);
106                         }
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)
111                                         lua_replace(L, rv);
112                                 else
113                                         lua_pop(L, 1);
114                         }
115                         else
116                                 assert(0);
117                 }
118
119                 // Handle short circuit modes
120                 if(mode == RUN_CALLBACKS_MODE_AND_SC &&
121                                 (bool)lua_toboolean(L, rv) == false)
122                         break;
123                 else if(mode == RUN_CALLBACKS_MODE_OR_SC &&
124                                 (bool)lua_toboolean(L, rv) == true)
125                         break;
126
127                 // value removed, keep key for next iteration
128         }
129
130         // Remove stuff from stack, leaving only the return value
131         lua_settop(L, rv);
132
133         // Fix return value in case no callbacks were called
134         if(first_loop){
135                 if(mode == RUN_CALLBACKS_MODE_AND ||
136                                 mode == RUN_CALLBACKS_MODE_AND_SC){
137                         lua_pop(L, 1);
138                         lua_pushboolean(L, true);
139                 }
140                 else if(mode == RUN_CALLBACKS_MODE_OR ||
141                                 mode == RUN_CALLBACKS_MODE_OR_SC){
142                         lua_pop(L, 1);
143                         lua_pushboolean(L, false);
144                 }
145         }
146 }
147
148