The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ldblib.cpp
Go to the documentation of this file.
1 /*
2 ** Interface from Lua to its debug API
3 ** See Copyright Notice in lua.h
4 */
5 
6 
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 
11 #define ldblib_c
12 #define LUA_LIB
13 
14 #include "lua.h"
15 
16 #include "lauxlib.h"
17 #include "lualib.h"
18 
19 
20 #define HOOKKEY "_HKEY"
21 
22 
23 
24 static int db_getregistry (lua_State *L) {
26  return 1;
27 }
28 
29 
30 static int db_getmetatable (lua_State *L) {
31  luaL_checkany(L, 1);
32  if (!lua_getmetatable(L, 1)) {
33  lua_pushnil(L); /* no metatable */
34  }
35  return 1;
36 }
37 
38 
39 static int db_setmetatable (lua_State *L) {
40  int t = lua_type(L, 2);
41  luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
42  "nil or table expected");
43  lua_settop(L, 2);
44  lua_setmetatable(L, 1);
45  return 1; /* return 1st argument */
46 }
47 
48 
49 static int db_getuservalue (lua_State *L) {
50  if (lua_type(L, 1) != LUA_TUSERDATA)
51  lua_pushnil(L);
52  else
53  lua_getuservalue(L, 1);
54  return 1;
55 }
56 
57 
58 static int db_setuservalue (lua_State *L) {
59  if (lua_type(L, 1) == LUA_TLIGHTUSERDATA)
60  luaL_argerror(L, 1, "full userdata expected, got light userdata");
62  if (!lua_isnoneornil(L, 2))
64  lua_settop(L, 2);
65  lua_setuservalue(L, 1);
66  return 1;
67 }
68 
69 
70 static void settabss (lua_State *L, const char *i, const char *v) {
71  lua_pushstring(L, v);
72  lua_setfield(L, -2, i);
73 }
74 
75 
76 static void settabsi (lua_State *L, const char *i, int v) {
77  lua_pushinteger(L, v);
78  lua_setfield(L, -2, i);
79 }
80 
81 
82 static void settabsb (lua_State *L, const char *i, int v) {
83  lua_pushboolean(L, v);
84  lua_setfield(L, -2, i);
85 }
86 
87 
88 static lua_State *getthread (lua_State *L, int *arg) {
89  if (lua_isthread(L, 1)) {
90  *arg = 1;
91  return lua_tothread(L, 1);
92  }
93  else {
94  *arg = 0;
95  return L;
96  }
97 }
98 
99 
100 static void treatstackoption (lua_State *L, lua_State *L1, const char *fname) {
101  if (L == L1) {
102  lua_pushvalue(L, -2);
103  lua_remove(L, -3);
104  }
105  else
106  lua_xmove(L1, L, 1);
107  lua_setfield(L, -2, fname);
108 }
109 
110 
111 static int db_getinfo (lua_State *L) {
112  lua_Debug ar;
113  int arg;
114  lua_State *L1 = getthread(L, &arg);
115  const char *options = luaL_optstring(L, arg+2, "flnStu");
116  if (lua_isnumber(L, arg+1)) {
117  if (!lua_getstack(L1, (int)lua_tointeger(L, arg+1), &ar)) {
118  lua_pushnil(L); /* level out of range */
119  return 1;
120  }
121  }
122  else if (lua_isfunction(L, arg+1)) {
123  lua_pushfstring(L, ">%s", options);
124  options = lua_tostring(L, -1);
125  lua_pushvalue(L, arg+1);
126  lua_xmove(L, L1, 1);
127  }
128  else
129  return luaL_argerror(L, arg+1, "function or level expected");
130  if (!lua_getinfo(L1, options, &ar))
131  return luaL_argerror(L, arg+2, "invalid option");
132  lua_createtable(L, 0, 2);
133  if (strchr(options, 'S')) {
134  settabss(L, "source", ar.source);
135  settabss(L, "short_src", ar.short_src);
136  settabsi(L, "linedefined", ar.linedefined);
137  settabsi(L, "lastlinedefined", ar.lastlinedefined);
138  settabss(L, "what", ar.what);
139  }
140  if (strchr(options, 'l'))
141  settabsi(L, "currentline", ar.currentline);
142  if (strchr(options, 'u')) {
143  settabsi(L, "nups", ar.nups);
144  settabsi(L, "nparams", ar.nparams);
145  settabsb(L, "isvararg", ar.isvararg);
146  }
147  if (strchr(options, 'n')) {
148  settabss(L, "name", ar.name);
149  settabss(L, "namewhat", ar.namewhat);
150  }
151  if (strchr(options, 't'))
152  settabsb(L, "istailcall", ar.istailcall);
153  if (strchr(options, 'L'))
154  treatstackoption(L, L1, "activelines");
155  if (strchr(options, 'f'))
156  treatstackoption(L, L1, "func");
157  return 1; /* return table */
158 }
159 
160 
161 static int db_getlocal (lua_State *L) {
162  int arg;
163  lua_State *L1 = getthread(L, &arg);
164  lua_Debug ar;
165  const char *name;
166  int nvar = luaL_checkint(L, arg+2); /* local-variable index */
167  if (lua_isfunction(L, arg + 1)) { /* function argument? */
168  lua_pushvalue(L, arg + 1); /* push function */
169  lua_pushstring(L, lua_getlocal(L, NULL, nvar)); /* push local name */
170  return 1;
171  }
172  else { /* stack-level argument */
173  if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */
174  return luaL_argerror(L, arg+1, "level out of range");
175  name = lua_getlocal(L1, &ar, nvar);
176  if (name) {
177  lua_xmove(L1, L, 1); /* push local value */
178  lua_pushstring(L, name); /* push name */
179  lua_pushvalue(L, -2); /* re-order */
180  return 2;
181  }
182  else {
183  lua_pushnil(L); /* no name (nor value) */
184  return 1;
185  }
186  }
187 }
188 
189 
190 static int db_setlocal (lua_State *L) {
191  int arg;
192  lua_State *L1 = getthread(L, &arg);
193  lua_Debug ar;
194  if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */
195  return luaL_argerror(L, arg+1, "level out of range");
196  luaL_checkany(L, arg+3);
197  lua_settop(L, arg+3);
198  lua_xmove(L, L1, 1);
199  lua_pushstring(L, lua_setlocal(L1, &ar, luaL_checkint(L, arg+2)));
200  return 1;
201 }
202 
203 
204 static int auxupvalue (lua_State *L, int get) {
205  const char *name;
206  int n = luaL_checkint(L, 2);
208  name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n);
209  if (name == NULL) return 0;
210  lua_pushstring(L, name);
211  lua_insert(L, -(get+1));
212  return get + 1;
213 }
214 
215 
216 static int db_getupvalue (lua_State *L) {
217  return auxupvalue(L, 1);
218 }
219 
220 
221 static int db_setupvalue (lua_State *L) {
222  luaL_checkany(L, 3);
223  return auxupvalue(L, 0);
224 }
225 
226 
227 static int checkupval (lua_State *L, int argf, int argnup) {
228  lua_Debug ar;
229  int nup = luaL_checkint(L, argnup);
230  luaL_checktype(L, argf, LUA_TFUNCTION);
231  lua_pushvalue(L, argf);
232  lua_getinfo(L, ">u", &ar);
233  luaL_argcheck(L, 1 <= nup && nup <= ar.nups, argnup, "invalid upvalue index");
234  return nup;
235 }
236 
237 
238 static int db_upvalueid (lua_State *L) {
239  int n = checkupval(L, 1, 2);
241  return 1;
242 }
243 
244 
245 static int db_upvaluejoin (lua_State *L) {
246  int n1 = checkupval(L, 1, 2);
247  int n2 = checkupval(L, 3, 4);
248  luaL_argcheck(L, !lua_iscfunction(L, 1), 1, "Lua function expected");
249  luaL_argcheck(L, !lua_iscfunction(L, 3), 3, "Lua function expected");
250  lua_upvaluejoin(L, 1, n1, 3, n2);
251  return 0;
252 }
253 
254 
255 #define gethooktable(L) luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY)
256 
257 
258 static void hookf (lua_State *L, lua_Debug *ar) {
259  static const char *const hooknames[] =
260  {"call", "return", "line", "count", "tail call"};
261  gethooktable(L);
262  lua_pushthread(L);
263  lua_rawget(L, -2);
264  if (lua_isfunction(L, -1)) {
265  lua_pushstring(L, hooknames[(int)ar->event]);
266  if (ar->currentline >= 0)
268  else lua_pushnil(L);
269  lua_assert(lua_getinfo(L, "lS", ar));
270  lua_call(L, 2, 0);
271  }
272 }
273 
274 
275 static int makemask (const char *smask, int count) {
276  int mask = 0;
277  if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
278  if (strchr(smask, 'r')) mask |= LUA_MASKRET;
279  if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
280  if (count > 0) mask |= LUA_MASKCOUNT;
281  return mask;
282 }
283 
284 
285 static char *unmakemask (int mask, char *smask) {
286  int i = 0;
287  if (mask & LUA_MASKCALL) smask[i++] = 'c';
288  if (mask & LUA_MASKRET) smask[i++] = 'r';
289  if (mask & LUA_MASKLINE) smask[i++] = 'l';
290  smask[i] = '\0';
291  return smask;
292 }
293 
294 
295 static int db_sethook (lua_State *L) {
296  int arg, mask, count;
297  lua_Hook func;
298  lua_State *L1 = getthread(L, &arg);
299  if (lua_isnoneornil(L, arg+1)) {
300  lua_settop(L, arg+1);
301  func = NULL; mask = 0; count = 0; /* turn off hooks */
302  }
303  else {
304  const char *smask = luaL_checkstring(L, arg+2);
305  luaL_checktype(L, arg+1, LUA_TFUNCTION);
306  count = luaL_optint(L, arg+3, 0);
307  func = hookf; mask = makemask(smask, count);
308  }
309  if (gethooktable(L) == 0) { /* creating hook table? */
310  lua_pushstring(L, "k");
311  lua_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */
312  lua_pushvalue(L, -1);
313  lua_setmetatable(L, -2); /* setmetatable(hooktable) = hooktable */
314  }
315  lua_pushthread(L1); lua_xmove(L1, L, 1);
316  lua_pushvalue(L, arg+1);
317  lua_rawset(L, -3); /* set new hook */
318  lua_sethook(L1, func, mask, count); /* set hooks */
319  return 0;
320 }
321 
322 
323 static int db_gethook (lua_State *L) {
324  int arg;
325  lua_State *L1 = getthread(L, &arg);
326  char buff[5];
327  int mask = lua_gethookmask(L1);
328  lua_Hook hook = lua_gethook(L1);
329  if (hook != NULL && hook != hookf) /* external hook? */
330  lua_pushliteral(L, "external hook");
331  else {
332  gethooktable(L);
333  lua_pushthread(L1); lua_xmove(L1, L, 1);
334  lua_rawget(L, -2); /* get hook */
335  lua_remove(L, -2); /* remove hook table */
336  }
337  lua_pushstring(L, unmakemask(mask, buff));
339  return 3;
340 }
341 
342 
343 static int db_debug (lua_State *L) {
344  for (;;) {
345  char buffer[250];
346  luai_writestringerror("%s", "lua_debug> ");
347  if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
348  strcmp(buffer, "cont\n") == 0)
349  return 0;
350  if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") ||
351  lua_pcall(L, 0, 0, 0))
352  luai_writestringerror("%s\n", lua_tostring(L, -1));
353  lua_settop(L, 0); /* remove eventual returns */
354  }
355 }
356 
357 
358 static int db_traceback (lua_State *L) {
359  int arg;
360  lua_State *L1 = getthread(L, &arg);
361  const char *msg = lua_tostring(L, arg + 1);
362  if (msg == NULL && !lua_isnoneornil(L, arg + 1)) /* non-string 'msg'? */
363  lua_pushvalue(L, arg + 1); /* return it untouched */
364  else {
365  int level = luaL_optint(L, arg + 2, (L == L1) ? 1 : 0);
366  luaL_traceback(L, L1, msg, level);
367  }
368  return 1;
369 }
370 
371 
372 static const luaL_Reg dblib[] = {
373  {"debug", db_debug},
374  {"getuservalue", db_getuservalue},
375  {"gethook", db_gethook},
376  {"getinfo", db_getinfo},
377  {"getlocal", db_getlocal},
378  {"getregistry", db_getregistry},
379  {"getmetatable", db_getmetatable},
380  {"getupvalue", db_getupvalue},
381  {"upvaluejoin", db_upvaluejoin},
382  {"upvalueid", db_upvalueid},
383  {"setuservalue", db_setuservalue},
384  {"sethook", db_sethook},
385  {"setlocal", db_setlocal},
386  {"setmetatable", db_setmetatable},
387  {"setupvalue", db_setupvalue},
388  {"traceback", db_traceback},
389  {NULL, NULL}
390 };
391 
392 
394  luaL_newlib(L, dblib);
395  return 1;
396 }
397 
static void settabsb(lua_State *L, const char *i, int v)
Definition: ldblib.cpp:82
const char * what
Definition: lua.h:402
unsigned char nparams
Definition: lua.h:408
Definition: lua.h:398
#define lua_isnoneornil(L, n)
Definition: lua.h:337
LUA_API void lua_pushlightuserdata(lua_State *L, void *p)
Definition: lapi.cpp:579
LUA_API void lua_createtable(lua_State *L, int narray, int nrec)
Definition: lapi.cpp:667
static int db_setmetatable(lua_State *L)
Definition: ldblib.cpp:39
LUAMOD_API int luaopen_debug(lua_State *L)
Definition: ldblib.cpp:393
LUA_API void lua_settop(lua_State *L, int idx)
Definition: lapi.cpp:159
static int db_setuservalue(lua_State *L)
Definition: ldblib.cpp:58
LUA_API int lua_type(lua_State *L, int idx)
Definition: lapi.cpp:243
GLint level
Definition: glew.h:1220
static int makemask(const char *smask, int count)
Definition: ldblib.cpp:275
static void hookf(lua_State *L, lua_Debug *ar)
Definition: ldblib.cpp:258
#define LUA_TUSERDATA
Definition: lua.h:84
LUA_API void lua_pushboolean(lua_State *L, int b)
Definition: lapi.cpp:571
LUA_API lua_Hook lua_gethook(lua_State *L)
Definition: ldebug.cpp:67
unsigned char nups
Definition: lua.h:407
#define LUA_MASKRET
Definition: lua.h:370
static char * unmakemask(int mask, char *smask)
Definition: ldblib.cpp:285
#define LUA_MASKCOUNT
Definition: lua.h:372
const char * name
Definition: lua.h:400
LUALIB_API void luaL_checktype(lua_State *L, int narg, int t)
Definition: lauxlib.cpp:347
#define LUA_MASKCALL
Definition: lua.h:369
LUA_API void lua_setuservalue(lua_State *L, int idx)
Definition: lapi.cpp:846
void(* lua_Hook)(lua_State *L, lua_Debug *ar)
Definition: lua.h:378
const char * source
Definition: lua.h:403
LUA_API int lua_gethookmask(lua_State *L)
Definition: ldebug.cpp:72
#define lua_tointeger(L, i)
Definition: lua.h:319
int event
Definition: lua.h:399
#define LUA_TLIGHTUSERDATA
Definition: lua.h:79
static int db_setupvalue(lua_State *L)
Definition: ldblib.cpp:221
char short_src[LUA_IDSIZE]
Definition: lua.h:411
#define LUA_TFUNCTION
Definition: lua.h:83
const char * namewhat
Definition: lua.h:401
LUA_API int lua_getstack(lua_State *L, int level, lua_Debug *ar)
Definition: ldebug.cpp:82
#define luaL_argcheck(L, cond, numarg, extramsg)
Definition: lauxlib.h:113
GLdouble GLdouble t
Definition: glew.h:1366
#define luaL_loadbuffer(L, s, sz, n)
Definition: lauxlib.h:134
int lastlinedefined
Definition: lua.h:406
#define lua_isthread(L, n)
Definition: lua.h:335
#define lua_pcall(L, n, r, f)
Definition: lua.h:258
const config & options()
static int db_getinfo(lua_State *L)
Definition: ldblib.cpp:111
static void settabsi(lua_State *L, const char *i, int v)
Definition: ldblib.cpp:76
static int db_traceback(lua_State *L)
Definition: ldblib.cpp:358
char istailcall
Definition: lua.h:410
#define LUA_TNIL
Definition: lua.h:77
static void settabss(lua_State *L, const char *i, const char *v)
Definition: ldblib.cpp:70
const GLdouble * v
Definition: glew.h:1359
LUA_API void lua_upvaluejoin(lua_State *L, int fidx1, int n1, int fidx2, int n2)
Definition: lapi.cpp:1270
LUA_API int lua_sethook(lua_State *L, lua_Hook func, int mask, int count)
Definition: ldebug.cpp:52
LUA_API lua_State * lua_tothread(lua_State *L, int idx)
Definition: lapi.cpp:431
LUA_API int lua_getmetatable(lua_State *L, int objindex)
Definition: lapi.cpp:680
LUA_API void * lua_upvalueid(lua_State *L, int fidx, int n)
Definition: lapi.cpp:1251
LUA_API const char * lua_setlocal(lua_State *L, const lua_Debug *ar, int n)
Definition: ldebug.cpp:165
LUA_API int lua_setmetatable(lua_State *L, int objindex)
Definition: lapi.cpp:806
static int db_gethook(lua_State *L)
Definition: ldblib.cpp:323
static int checkupval(lua_State *L, int argf, int argnup)
Definition: ldblib.cpp:227
char isvararg
Definition: lua.h:409
GLenum GLint GLuint mask
Definition: glew.h:1813
LUA_API void lua_remove(lua_State *L, int idx)
Definition: lapi.cpp:176
static int auxupvalue(lua_State *L, int get)
Definition: ldblib.cpp:204
LUALIB_API void luaL_traceback(lua_State *L, lua_State *L1, const char *msg, int level)
Definition: lauxlib.cpp:115
GLuint GLuint GLsizei count
Definition: glew.h:1221
LUA_API void lua_pushnil(lua_State *L)
Definition: lapi.cpp:459
#define gethooktable(L)
Definition: ldblib.cpp:255
LUA_API const char * lua_setupvalue(lua_State *L, int funcindex, int n)
Definition: lapi.cpp:1221
#define lua_pushliteral(L, s)
Definition: lua.h:339
LUA_API int lua_pushthread(lua_State *L)
Definition: lapi.cpp:587
GLuint buffer
Definition: glew.h:1648
LUA_API void lua_getuservalue(lua_State *L, int idx)
Definition: lapi.cpp:709
#define lua_assert(c)
Definition: llimits.h:65
LUA_API void lua_xmove(lua_State *from, lua_State *to, int n)
Definition: lapi.cpp:106
static int db_debug(lua_State *L)
Definition: ldblib.cpp:343
LUA_API void lua_rawset(lua_State *L, int idx)
Definition: lapi.cpp:764
LUA_API int lua_getinfo(lua_State *L, const char *what, lua_Debug *ar)
Definition: ldebug.cpp:265
static int db_upvaluejoin(lua_State *L)
Definition: ldblib.cpp:245
#define lua_isfunction(L, n)
Definition: lua.h:330
static int db_getuservalue(lua_State *L)
Definition: ldblib.cpp:49
#define LUAMOD_API
Definition: luaconf.h:163
LUALIB_API int luaL_argerror(lua_State *L, int narg, const char *extramsg)
Definition: lauxlib.cpp:152
size_t i
Definition: function.cpp:1057
static int db_getregistry(lua_State *L)
Definition: ldblib.cpp:24
static int db_upvalueid(lua_State *L)
Definition: ldblib.cpp:238
#define lua_tostring(L, i)
Definition: lua.h:345
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:112
static int db_sethook(lua_State *L)
Definition: ldblib.cpp:295
LUA_API int lua_iscfunction(lua_State *L, int idx)
Definition: lapi.cpp:255
LUA_API void lua_insert(lua_State *L, int idx)
Definition: lapi.cpp:187
LUA_API void lua_pushvalue(lua_State *L, int idx)
Definition: lapi.cpp:229
static int db_getmetatable(lua_State *L)
Definition: ldblib.cpp:30
LUA_API int lua_isnumber(lua_State *L, int idx)
Definition: lapi.cpp:261
#define luaL_newlib(L, l)
Definition: lauxlib.h:111
#define lua_call(L, n, r)
Definition: lua.h:252
static void treatstackoption(lua_State *L, lua_State *L1, const char *fname)
Definition: ldblib.cpp:100
LUA_API const char * lua_getlocal(lua_State *L, const lua_Debug *ar, int n)
Definition: ldebug.cpp:143
#define luai_writestringerror(s, p)
Definition: luaconf.h:232
static lua_State * getthread(lua_State *L, int *arg)
Definition: ldblib.cpp:88
GLuint const GLchar * name
Definition: glew.h:1782
LUA_API const char * lua_getupvalue(lua_State *L, int funcindex, int n)
Definition: lapi.cpp:1207
#define LUA_REGISTRYINDEX
Definition: lua.h:39
GLclampd n
Definition: glew.h:5903
#define luaL_checkint(L, n)
Definition: lauxlib.h:117
#define luaL_optint(L, n, d)
Definition: lauxlib.h:118
LUALIB_API void luaL_checkany(lua_State *L, int narg)
Definition: lauxlib.cpp:353
static int db_setlocal(lua_State *L)
Definition: ldblib.cpp:190
LUA_API int lua_gethookcount(lua_State *L)
Definition: ldebug.cpp:77
LUA_API const char * lua_pushfstring(lua_State *L, const char *fmt,...)
Definition: lapi.cpp:536
static int db_getlocal(lua_State *L)
Definition: ldblib.cpp:161
int currentline
Definition: lua.h:404
#define mask(n)
Definition: lbitlib.cpp:28
#define LUA_TTABLE
Definition: lua.h:82
static const luaL_Reg dblib[]
Definition: ldblib.cpp:372
LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)
Definition: lapi.cpp:477
#define luaL_optstring(L, n, d)
Definition: lauxlib.h:116
LUA_API void lua_rawget(lua_State *L, int idx)
Definition: lapi.cpp:633
LUA_API const char * lua_pushstring(lua_State *L, const char *s)
Definition: lapi.cpp:507
LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
Definition: lapi.cpp:752
#define LUA_MASKLINE
Definition: lua.h:371
static int db_getupvalue(lua_State *L)
Definition: ldblib.cpp:216
int linedefined
Definition: lua.h:405
#define luaL_checkstring(L, n)
Definition: lauxlib.h:115