summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1999-01-08 14:47:44 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1999-01-08 14:47:44 -0200
commit01772cefa5217a7840254d1e7ec340dea8f0747a (patch)
tree01fcf83242542a8ad171a358994b7c4c0cc835dd
parentdc90d4bce340f987a5048c17699a653f47360711 (diff)
downloadlua-01772cefa5217a7840254d1e7ec340dea8f0747a.tar.gz
lua-01772cefa5217a7840254d1e7ec340dea8f0747a.tar.bz2
lua-01772cefa5217a7840254d1e7ec340dea8f0747a.zip
new library for debbuging
-rw-r--r--ldblib.c213
-rw-r--r--lua.c9
-rw-r--r--lualib.h5
-rw-r--r--makefile12
4 files changed, 228 insertions, 11 deletions
diff --git a/ldblib.c b/ldblib.c
new file mode 100644
index 00000000..952f3cb3
--- /dev/null
+++ b/ldblib.c
@@ -0,0 +1,213 @@
1/*
2** $Id: $
3** Interface from Lua to its debug API
4** See Copyright Notice in lua.h
5*/
6
7
8#include <stdlib.h>
9#include <string.h>
10
11#include "lauxlib.h"
12#include "lua.h"
13#include "luadebug.h"
14
15
16
17static void settabss (lua_Object t, char *i, char *v) {
18 lua_pushobject(t);
19 lua_pushstring(i);
20 lua_pushstring(v);
21 lua_settable();
22}
23
24
25static void settabsi (lua_Object t, char *i, int v) {
26 lua_pushobject(t);
27 lua_pushstring(i);
28 lua_pushnumber(v);
29 lua_settable();
30}
31
32
33static lua_Object getfuncinfo (lua_Object func) {
34 lua_Object result = lua_createtable();
35 char *name;
36 int line;
37 lua_funcinfo(func, &name, &line);
38 if (line == -1) /* C function? */
39 settabss(result, "kind", "C");
40 else if (line == 0) { /* "main"? */
41 settabss(result, "kind", "chunk");
42 settabss(result, "name", name);
43 }
44 else { /* Lua function */
45 settabss(result, "kind", "Lua");
46 settabsi(result, "def_line", line);
47 settabss(result, "def_chunk", name);
48 }
49 if (line != 0) { /* is it not a "main"? */
50 char *kind = lua_getobjname(func, &name);
51 if (*kind) {
52 settabss(result, "name", name);
53 settabss(result, "where", kind);
54 }
55 }
56 return result;
57}
58
59
60static void getstack (void) {
61 lua_Object func = lua_stackedfunction(luaL_check_int(1));
62 if (func == LUA_NOOBJECT) /* level out of range? */
63 return;
64 else {
65 lua_Object result = getfuncinfo(func);
66 int currline = lua_currentline(func);
67 if (currline > 0)
68 settabsi(result, "current", currline);
69 lua_pushobject(result);
70 }
71}
72
73
74static void funcinfo (void) {
75 lua_pushobject(getfuncinfo(luaL_functionarg(1)));
76}
77
78
79static int findlocal (lua_Object func, int arg) {
80 lua_Object v = lua_getparam(arg);
81 if (lua_isnumber(v))
82 return (int)lua_getnumber(v);
83 else {
84 char *name = luaL_check_string(arg);
85 int i = 0;
86 int result = -1;
87 char *vname;
88 while (lua_getlocal(func, ++i, &vname) != LUA_NOOBJECT) {
89 if (strcmp(name, vname) == 0)
90 result = i; /* keep looping to get the last var with this name */
91 }
92 if (result == -1)
93 luaL_verror("no local variable `%.50s' at given level", name);
94 return result;
95 }
96}
97
98
99static void getlocal (void) {
100 lua_Object func = lua_stackedfunction(luaL_check_int(1));
101 lua_Object val;
102 char *name;
103 if (func == LUA_NOOBJECT) /* level out of range? */
104 return; /* return nil */
105 else if (lua_getparam(2) != LUA_NOOBJECT) { /* 2nd argument? */
106 if ((val = lua_getlocal(func, findlocal(func, 2), &name)) != LUA_NOOBJECT) {
107 lua_pushobject(val);
108 lua_pushstring(name);
109 }
110 /* else return nil */
111 }
112 else { /* collect all locals in a table */
113 lua_Object result = lua_createtable();
114 int i;
115 for (i=1; ;i++) {
116 if ((val = lua_getlocal(func, i, &name)) == LUA_NOOBJECT)
117 break;
118 lua_pushobject(result);
119 lua_pushstring(name);
120 lua_pushobject(val);
121 lua_settable(); /* result[name] = value */
122 }
123 lua_pushobject(result);
124 }
125}
126
127
128static void setlocal (void) {
129 lua_Object func = lua_stackedfunction(luaL_check_int(1));
130 int numvar;
131 luaL_arg_check(func != LUA_NOOBJECT, 1, "level out of range");
132 numvar = findlocal(func, 2);
133 lua_pushobject(luaL_nonnullarg(3));
134 if (!lua_setlocal(func, numvar))
135 lua_error("no such local variable");
136}
137
138
139
140static int linehook = -1; /* Lua reference to line hook function */
141static int callhook = -1; /* Lua reference to call hook function */
142
143
144static void dohook (int ref) {
145 lua_LHFunction oldlinehook = lua_linehook; /* save old hooks */
146 lua_CHFunction oldcallhook = lua_callhook;
147 lua_linehook = NULL; lua_callhook = NULL; /* to avoid recusive calls */
148 lua_callfunction(lua_getref(ref));
149 lua_linehook = oldlinehook; /* restore old hooks */
150 lua_callhook = oldcallhook;
151}
152
153
154static void linef (int line) {
155 lua_pushnumber(line);
156 dohook(linehook);
157}
158
159
160static void callf (lua_Function func, char *file, int line) {
161 if (func != LUA_NOOBJECT) {
162 lua_pushobject(func);
163 lua_pushstring(file);
164 lua_pushnumber(line);
165 }
166 dohook(callhook);
167}
168
169
170static void setcallhook (void) {
171 lua_Object f = lua_getparam(1);
172 lua_unref(callhook);
173 if (f == LUA_NOOBJECT) {
174 callhook = -1;
175 lua_callhook = NULL;
176 }
177 else {
178 lua_pushobject(f);
179 callhook = lua_ref(1);
180 lua_callhook = callf;
181 }
182}
183
184
185static void setlinehook (void) {
186 lua_Object f = lua_getparam(1);
187 lua_unref(linehook);
188 if (f == LUA_NOOBJECT) {
189 linehook = -1;
190 lua_linehook = NULL;
191 }
192 else {
193 lua_pushobject(f);
194 linehook = lua_ref(1);
195 lua_linehook = linef;
196 }
197}
198
199
200static struct luaL_reg dblib[] = {
201 {"funcinfo", funcinfo},
202 {"getlocal", getlocal},
203 {"getstack", getstack},
204 {"setcallhook", setcallhook},
205 {"setlinehook", setlinehook},
206 {"setlocal", setlocal}
207};
208
209
210void lua_dblibopen (void) {
211 luaL_openlib(dblib, (sizeof(dblib)/sizeof(dblib[0])));
212}
213
diff --git a/lua.c b/lua.c
index 576d34fe..1ab0ac44 100644
--- a/lua.c
+++ b/lua.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.c,v 1.15 1998/12/28 13:44:54 roberto Exp $ 2** $Id: lua.c,v 1.16 1999/01/06 13:12:41 roberto Exp roberto $
3** Lua stand-alone interpreter 3** Lua stand-alone interpreter
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -131,11 +131,10 @@ static void manual_input (int prompt) {
131int main (int argc, char *argv[]) 131int main (int argc, char *argv[])
132{ 132{
133 int i; 133 int i;
134 setlocale(LC_ALL, ""); 134 lua_open();
135 lua_iolibopen();
136 lua_strlibopen();
137 lua_mathlibopen();
138 lua_pushstring("> "); lua_setglobal("_PROMPT"); 135 lua_pushstring("> "); lua_setglobal("_PROMPT");
136 lua_userinit();
137 setlocale(LC_ALL, "");
139 if (argc < 2) { /* no arguments? */ 138 if (argc < 2) { /* no arguments? */
140 if (isatty(0)) { 139 if (isatty(0)) {
141 printf("%s %s\n", LUA_VERSION, LUA_COPYRIGHT); 140 printf("%s %s\n", LUA_VERSION, LUA_COPYRIGHT);
diff --git a/lualib.h b/lualib.h
index cf17599a..c6e34b52 100644
--- a/lualib.h
+++ b/lualib.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lualib.h,v 1.3 1997/12/17 20:48:58 roberto Exp roberto $ 2** $Id: lualib.h,v 1.4 1998/06/19 16:14:09 roberto Exp roberto $
3** Lua standard libraries 3** Lua standard libraries
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -10,12 +10,13 @@
10 10
11#include "lua.h" 11#include "lua.h"
12 12
13
14void lua_iolibopen (void); 13void lua_iolibopen (void);
15void lua_strlibopen (void); 14void lua_strlibopen (void);
16void lua_mathlibopen (void); 15void lua_mathlibopen (void);
16void lua_dblibopen (void);
17 17
18 18
19void lua_userinit (void);
19 20
20 21
21/* To keep compatibility with old versions */ 22/* To keep compatibility with old versions */
diff --git a/makefile b/makefile
index 8fed9268..a1640b3d 100644
--- a/makefile
+++ b/makefile
@@ -1,5 +1,5 @@
1# 1#
2## $Id: makefile,v 1.12 1998/05/27 13:03:40 roberto Exp roberto $ 2## $Id: makefile,v 1.13 1998/06/19 18:52:27 roberto Exp roberto $
3## Makefile 3## Makefile
4## See Copyright Notice in lua.h 4## See Copyright Notice in lua.h
5# 5#
@@ -58,7 +58,9 @@ LUAOBJS = \
58LIBOBJS = \ 58LIBOBJS = \
59 liolib.o \ 59 liolib.o \
60 lmathlib.o \ 60 lmathlib.o \
61 lstrlib.o 61 lstrlib.o \
62 ldblib.o \
63 linit.o
62 64
63 65
64lua : lua.o liblua.a liblualib.a 66lua : lua.o liblua.a liblualib.a
@@ -96,12 +98,14 @@ lauxlib.o: lauxlib.c lauxlib.h lua.h luadebug.h
96lbuffer.o: lbuffer.c lauxlib.h lua.h lmem.h lstate.h lobject.h 98lbuffer.o: lbuffer.c lauxlib.h lua.h lmem.h lstate.h lobject.h
97lbuiltin.o: lbuiltin.c lapi.h lua.h lobject.h lauxlib.h lbuiltin.h \ 99lbuiltin.o: lbuiltin.c lapi.h lua.h lobject.h lauxlib.h lbuiltin.h \
98 ldo.h lstate.h lfunc.h lmem.h lstring.h ltable.h ltm.h lundump.h \ 100 ldo.h lstate.h lfunc.h lmem.h lstring.h ltable.h ltm.h lundump.h \
99 lzio.h 101 lzio.h lvm.h
102ldblib.o: ldblib.c lauxlib.h lua.h luadebug.h
100ldo.o: ldo.c ldo.h lobject.h lua.h lstate.h lfunc.h lgc.h lmem.h \ 103ldo.o: ldo.c ldo.h lobject.h lua.h lstate.h lfunc.h lgc.h lmem.h \
101 lparser.h lzio.h ltm.h luadebug.h lundump.h lvm.h 104 lparser.h lzio.h lstring.h ltm.h luadebug.h lundump.h lvm.h
102lfunc.o: lfunc.c lfunc.h lobject.h lua.h lmem.h lstate.h 105lfunc.o: lfunc.c lfunc.h lobject.h lua.h lmem.h lstate.h
103lgc.o: lgc.c ldo.h lobject.h lua.h lstate.h lfunc.h lgc.h lmem.h \ 106lgc.o: lgc.c ldo.h lobject.h lua.h lstate.h lfunc.h lgc.h lmem.h \
104 lstring.h ltable.h ltm.h 107 lstring.h ltable.h ltm.h
108linit.o: linit.c lua.h lualib.h
105liolib.o: liolib.c lauxlib.h lua.h luadebug.h lualib.h 109liolib.o: liolib.c lauxlib.h lua.h luadebug.h lualib.h
106llex.o: llex.c lauxlib.h lua.h llex.h lobject.h lzio.h lmem.h \ 110llex.o: llex.c lauxlib.h lua.h llex.h lobject.h lzio.h lmem.h \
107 lparser.h lstate.h lstring.h luadebug.h 111 lparser.h lstate.h lstring.h luadebug.h