Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ceguilua/src/lua-5.0.3/lua/ldblib.c @ 1808

Last change on this file since 1808 was 1803, checked in by rgrieder, 16 years ago

added files for lua 5.1.3, lua 5.0.3, CEGUILua-0.6.1 and CEGUILua-0.5.0b

  • Property svn:eol-style set to native
File size: 7.5 KB
Line 
1/*
2** $Id: ldblib.c,v 1.80 2003/04/03 13:35:34 roberto Exp $
3** Interface from Lua to its debug API
4** See Copyright Notice in lua.h
5*/
6
7
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11
12#define ldblib_c
13
14#include "lua.h"
15
16#include "lauxlib.h"
17#include "lualib.h"
18
19
20
21static void settabss (lua_State *L, const char *i, const char *v) {
22  lua_pushstring(L, i);
23  lua_pushstring(L, v);
24  lua_rawset(L, -3);
25}
26
27
28static void settabsi (lua_State *L, const char *i, int v) {
29  lua_pushstring(L, i);
30  lua_pushnumber(L, (lua_Number)v);
31  lua_rawset(L, -3);
32}
33
34
35static int getinfo (lua_State *L) {
36  lua_Debug ar;
37  const char *options = luaL_optstring(L, 2, "flnSu");
38  if (lua_isnumber(L, 1)) {
39    if (!lua_getstack(L, (int)(lua_tonumber(L, 1)), &ar)) {
40      lua_pushnil(L);  /* level out of range */
41      return 1;
42    }
43  }
44  else if (lua_isfunction(L, 1)) {
45    lua_pushfstring(L, ">%s", options);
46    options = lua_tostring(L, -1);
47    lua_pushvalue(L, 1);
48  }
49  else
50    return luaL_argerror(L, 1, "function or level expected");
51  if (!lua_getinfo(L, options, &ar))
52    return luaL_argerror(L, 2, "invalid option");
53  lua_newtable(L);
54  for (; *options; options++) {
55    switch (*options) {
56      case 'S':
57        settabss(L, "source", ar.source);
58        settabss(L, "short_src", ar.short_src);
59        settabsi(L, "linedefined", ar.linedefined);
60        settabss(L, "what", ar.what);
61        break;
62      case 'l':
63        settabsi(L, "currentline", ar.currentline);
64        break;
65      case 'u':
66        settabsi(L, "nups", ar.nups);
67        break;
68      case 'n':
69        settabss(L, "name", ar.name);
70        settabss(L, "namewhat", ar.namewhat);
71        break;
72      case 'f':
73        lua_pushliteral(L, "func");
74        lua_pushvalue(L, -3);
75        lua_rawset(L, -3);
76        break;
77    }
78  }
79  return 1;  /* return table */
80}
81   
82
83static int getlocal (lua_State *L) {
84  lua_Debug ar;
85  const char *name;
86  if (!lua_getstack(L, luaL_checkint(L, 1), &ar))  /* level out of range? */
87    return luaL_argerror(L, 1, "level out of range");
88  name = lua_getlocal(L, &ar, luaL_checkint(L, 2));
89  if (name) {
90    lua_pushstring(L, name);
91    lua_pushvalue(L, -2);
92    return 2;
93  }
94  else {
95    lua_pushnil(L);
96    return 1;
97  }
98}
99
100
101static int setlocal (lua_State *L) {
102  lua_Debug ar;
103  if (!lua_getstack(L, luaL_checkint(L, 1), &ar))  /* level out of range? */
104    return luaL_argerror(L, 1, "level out of range");
105  luaL_checkany(L, 3);
106  lua_pushstring(L, lua_setlocal(L, &ar, luaL_checkint(L, 2)));
107  return 1;
108}
109
110
111static int auxupvalue (lua_State *L, int get) {
112  const char *name;
113  int n = luaL_checkint(L, 2);
114  luaL_checktype(L, 1, LUA_TFUNCTION);
115  if (lua_iscfunction(L, 1)) return 0;  /* cannot touch C upvalues from Lua */
116  name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n);
117  if (name == NULL) return 0;
118  lua_pushstring(L, name);
119  lua_insert(L, -(get+1));
120  return get + 1;
121}
122
123
124static int getupvalue (lua_State *L) {
125  return auxupvalue(L, 1);
126}
127
128
129static int setupvalue (lua_State *L) {
130  luaL_checkany(L, 3);
131  return auxupvalue(L, 0);
132}
133
134
135
136static const char KEY_HOOK = 'h';
137
138
139static void hookf (lua_State *L, lua_Debug *ar) {
140  static const char *const hooknames[] =
141    {"call", "return", "line", "count", "tail return"};
142  lua_pushlightuserdata(L, (void *)&KEY_HOOK);
143  lua_rawget(L, LUA_REGISTRYINDEX);
144  if (lua_isfunction(L, -1)) {
145    lua_pushstring(L, hooknames[(int)ar->event]);
146    if (ar->currentline >= 0)
147      lua_pushnumber(L, (lua_Number)ar->currentline);
148    else lua_pushnil(L);
149    lua_assert(lua_getinfo(L, "lS", ar));
150    lua_call(L, 2, 0);
151  }
152  else
153    lua_pop(L, 1);  /* pop result from gettable */
154}
155
156
157static int makemask (const char *smask, int count) {
158  int mask = 0;
159  if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
160  if (strchr(smask, 'r')) mask |= LUA_MASKRET;
161  if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
162  if (count > 0) mask |= LUA_MASKCOUNT;
163  return mask;
164}
165
166
167static char *unmakemask (int mask, char *smask) {
168  int i = 0;
169  if (mask & LUA_MASKCALL) smask[i++] = 'c';
170  if (mask & LUA_MASKRET) smask[i++] = 'r';
171  if (mask & LUA_MASKLINE) smask[i++] = 'l';
172  smask[i] = '\0';
173  return smask;
174}
175
176
177static int sethook (lua_State *L) {
178  if (lua_isnoneornil(L, 1)) {
179    lua_settop(L, 1);
180    lua_sethook(L, NULL, 0, 0);  /* turn off hooks */
181  }
182  else {
183    const char *smask = luaL_checkstring(L, 2);
184    int count = luaL_optint(L, 3, 0);
185    luaL_checktype(L, 1, LUA_TFUNCTION);
186    lua_sethook(L, hookf, makemask(smask, count), count);
187  }
188  lua_pushlightuserdata(L, (void *)&KEY_HOOK);
189  lua_pushvalue(L, 1);
190  lua_rawset(L, LUA_REGISTRYINDEX);  /* set new hook */
191  return 0;
192}
193
194
195static int gethook (lua_State *L) {
196  char buff[5];
197  int mask = lua_gethookmask(L);
198  lua_Hook hook = lua_gethook(L);
199  if (hook != NULL && hook != hookf)  /* external hook? */
200    lua_pushliteral(L, "external hook");
201  else {
202    lua_pushlightuserdata(L, (void *)&KEY_HOOK);
203    lua_rawget(L, LUA_REGISTRYINDEX);   /* get hook */
204  }
205  lua_pushstring(L, unmakemask(mask, buff));
206  lua_pushnumber(L, (lua_Number)lua_gethookcount(L));
207  return 3;
208}
209
210
211static int debug (lua_State *L) {
212  for (;;) {
213    char buffer[250];
214    fputs("lua_debug> ", stderr);
215    if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
216        strcmp(buffer, "cont\n") == 0)
217      return 0;
218    lua_dostring(L, buffer);
219    lua_settop(L, 0);  /* remove eventual returns */
220  }
221}
222
223
224#define LEVELS1 12      /* size of the first part of the stack */
225#define LEVELS2 10      /* size of the second part of the stack */
226
227static int errorfb (lua_State *L) {
228  int level = 1;  /* skip level 0 (it's this function) */
229  int firstpart = 1;  /* still before eventual `...' */
230  lua_Debug ar;
231  if (lua_gettop(L) == 0)
232    lua_pushliteral(L, "");
233  else if (!lua_isstring(L, 1)) return 1;  /* no string message */
234  else lua_pushliteral(L, "\n");
235  lua_pushliteral(L, "stack traceback:");
236  while (lua_getstack(L, level++, &ar)) {
237    if (level > LEVELS1 && firstpart) {
238      /* no more than `LEVELS2' more levels? */
239      if (!lua_getstack(L, level+LEVELS2, &ar))
240        level--;  /* keep going */
241      else {
242        lua_pushliteral(L, "\n\t...");  /* too many levels */
243        while (lua_getstack(L, level+LEVELS2, &ar))  /* find last levels */
244          level++;
245      }
246      firstpart = 0;
247      continue;
248    }
249    lua_pushliteral(L, "\n\t");
250    lua_getinfo(L, "Snl", &ar);
251    lua_pushfstring(L, "%s:", ar.short_src);
252    if (ar.currentline > 0)
253      lua_pushfstring(L, "%d:", ar.currentline);
254    switch (*ar.namewhat) {
255      case 'g':  /* global */ 
256      case 'l':  /* local */
257      case 'f':  /* field */
258      case 'm':  /* method */
259        lua_pushfstring(L, " in function `%s'", ar.name);
260        break;
261      default: {
262        if (*ar.what == 'm')  /* main? */
263          lua_pushfstring(L, " in main chunk");
264        else if (*ar.what == 'C' || *ar.what == 't')
265          lua_pushliteral(L, " ?");  /* C function or tail call */
266        else
267          lua_pushfstring(L, " in function <%s:%d>",
268                             ar.short_src, ar.linedefined);
269      }
270    }
271    lua_concat(L, lua_gettop(L));
272  }
273  lua_concat(L, lua_gettop(L));
274  return 1;
275}
276
277
278static const luaL_reg dblib[] = {
279  {"getlocal", getlocal},
280  {"getinfo", getinfo},
281  {"gethook", gethook},
282  {"getupvalue", getupvalue},
283  {"sethook", sethook},
284  {"setlocal", setlocal},
285  {"setupvalue", setupvalue},
286  {"debug", debug},
287  {"traceback", errorfb},
288  {NULL, NULL}
289};
290
291
292LUALIB_API int luaopen_debug (lua_State *L) {
293  luaL_openlib(L, LUA_DBLIBNAME, dblib, 0);
294  lua_pushliteral(L, "_TRACEBACK");
295  lua_pushcfunction(L, errorfb);
296  lua_settable(L, LUA_GLOBALSINDEX);
297  return 1;
298}
299
Note: See TracBrowser for help on using the repository browser.