summaryrefslogtreecommitdiff
path: root/ldblib.c
diff options
context:
space:
mode:
Diffstat (limited to 'ldblib.c')
-rw-r--r--ldblib.c186
1 files changed, 94 insertions, 92 deletions
diff --git a/ldblib.c b/ldblib.c
index 432f8426..0bd3cc52 100644
--- a/ldblib.c
+++ b/ldblib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldblib.c,v 1.5 1999/03/04 21:17:26 roberto Exp roberto $ 2** $Id: ldblib.c,v 1.6 1999/08/16 20:52:00 roberto Exp roberto $
3** Interface from Lua to its debug API 3** Interface from Lua to its debug API
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -8,6 +8,8 @@
8#include <stdlib.h> 8#include <stdlib.h>
9#include <string.h> 9#include <string.h>
10 10
11#define LUA_REENTRANT
12
11#include "lauxlib.h" 13#include "lauxlib.h"
12#include "lua.h" 14#include "lua.h"
13#include "luadebug.h" 15#include "luadebug.h"
@@ -15,129 +17,129 @@
15 17
16 18
17 19
18static void settabss (lua_Object t, const char *i, const char *v) { 20static void settabss (lua_State *L, lua_Object t, const char *i, const char *v) {
19 lua_pushobject(t); 21 lua_pushobject(L, t);
20 lua_pushstring(i); 22 lua_pushstring(L, i);
21 lua_pushstring(v); 23 lua_pushstring(L, v);
22 lua_settable(); 24 lua_settable(L);
23} 25}
24 26
25 27
26static void settabsi (lua_Object t, const char *i, int v) { 28static void settabsi (lua_State *L, lua_Object t, const char *i, int v) {
27 lua_pushobject(t); 29 lua_pushobject(L, t);
28 lua_pushstring(i); 30 lua_pushstring(L, i);
29 lua_pushnumber(v); 31 lua_pushnumber(L, v);
30 lua_settable(); 32 lua_settable(L);
31} 33}
32 34
33 35
34static lua_Object getfuncinfo (lua_Object func) { 36static lua_Object getfuncinfo (lua_State *L, lua_Object func) {
35 lua_Object result = lua_createtable(); 37 lua_Object result = lua_createtable(L);
36 const char *str; 38 const char *str;
37 int line; 39 int line;
38 lua_funcinfo(func, &str, &line); 40 lua_funcinfo(L, func, &str, &line);
39 if (line == -1) /* C function? */ 41 if (line == -1) /* C function? */
40 settabss(result, "kind", "C"); 42 settabss(L, result, "kind", "C");
41 else if (line == 0) { /* "main"? */ 43 else if (line == 0) { /* "main"? */
42 settabss(result, "kind", "chunk"); 44 settabss(L, result, "kind", "chunk");
43 settabss(result, "source", str); 45 settabss(L, result, "source", str);
44 } 46 }
45 else { /* Lua function */ 47 else { /* Lua function */
46 settabss(result, "kind", "Lua"); 48 settabss(L, result, "kind", "Lua");
47 settabsi(result, "def_line", line); 49 settabsi(L, result, "def_line", line);
48 settabss(result, "source", str); 50 settabss(L, result, "source", str);
49 } 51 }
50 if (line != 0) { /* is it not a "main"? */ 52 if (line != 0) { /* is it not a "main"? */
51 const char *kind = lua_getobjname(func, &str); 53 const char *kind = lua_getobjname(L, func, &str);
52 if (*kind) { 54 if (*kind) {
53 settabss(result, "name", str); 55 settabss(L, result, "name", str);
54 settabss(result, "where", kind); 56 settabss(L, result, "where", kind);
55 } 57 }
56 } 58 }
57 return result; 59 return result;
58} 60}
59 61
60 62
61static void getstack (void) { 63static void getstack (lua_State *L) {
62 lua_Object func = lua_stackedfunction(luaL_check_int(1)); 64 lua_Object func = lua_stackedfunction(L, luaL_check_int(L, 1));
63 if (func == LUA_NOOBJECT) /* level out of range? */ 65 if (func == LUA_NOOBJECT) /* level out of range? */
64 return; 66 return;
65 else { 67 else {
66 lua_Object result = getfuncinfo(func); 68 lua_Object result = getfuncinfo(L, func);
67 int currline = lua_currentline(func); 69 int currline = lua_currentline(L, func);
68 if (currline > 0) 70 if (currline > 0)
69 settabsi(result, "current", currline); 71 settabsi(L, result, "current", currline);
70 lua_pushobject(result); 72 lua_pushobject(L, result);
71 lua_pushstring("func"); 73 lua_pushstring(L, "func");
72 lua_pushobject(func); 74 lua_pushobject(L, func);
73 lua_settable(); /* result.func = func */ 75 lua_settable(L); /* result.func = func */
74 lua_pushobject(result); 76 lua_pushobject(L, result);
75 } 77 }
76} 78}
77 79
78 80
79static void funcinfo (void) { 81static void funcinfo (lua_State *L) {
80 lua_pushobject(getfuncinfo(luaL_functionarg(1))); 82 lua_pushobject(L, getfuncinfo(L, luaL_functionarg(L, 1)));
81} 83}
82 84
83 85
84static int findlocal (lua_Object func, int arg) { 86static int findlocal (lua_State *L, lua_Object func, int arg) {
85 lua_Object v = lua_getparam(arg); 87 lua_Object v = lua_getparam(L, arg);
86 if (lua_isnumber(v)) 88 if (lua_isnumber(L, v))
87 return (int)lua_getnumber(v); 89 return (int)lua_getnumber(L, v);
88 else { 90 else {
89 const char *name = luaL_check_string(arg); 91 const char *name = luaL_check_string(L, arg);
90 int i = 0; 92 int i = 0;
91 int result = -1; 93 int result = -1;
92 const char *vname; 94 const char *vname;
93 while (lua_getlocal(func, ++i, &vname) != LUA_NOOBJECT) { 95 while (lua_getlocal(L, func, ++i, &vname) != LUA_NOOBJECT) {
94 if (strcmp(name, vname) == 0) 96 if (strcmp(name, vname) == 0)
95 result = i; /* keep looping to get the last var with this name */ 97 result = i; /* keep looping to get the last var with this name */
96 } 98 }
97 if (result == -1) 99 if (result == -1)
98 luaL_verror("no local variable `%.50s' at given level", name); 100 luaL_verror(L, "no local variable `%.50s' at given level", name);
99 return result; 101 return result;
100 } 102 }
101} 103}
102 104
103 105
104static void getlocal (void) { 106static void getlocal (lua_State *L) {
105 lua_Object func = lua_stackedfunction(luaL_check_int(1)); 107 lua_Object func = lua_stackedfunction(L, luaL_check_int(L, 1));
106 lua_Object val; 108 lua_Object val;
107 const char *name; 109 const char *name;
108 if (func == LUA_NOOBJECT) /* level out of range? */ 110 if (func == LUA_NOOBJECT) /* level out of range? */
109 return; /* return nil */ 111 return; /* return nil */
110 else if (lua_getparam(2) != LUA_NOOBJECT) { /* 2nd argument? */ 112 else if (lua_getparam(L, 2) != LUA_NOOBJECT) { /* 2nd argument? */
111 if ((val = lua_getlocal(func, findlocal(func, 2), &name)) != LUA_NOOBJECT) { 113 if ((val = lua_getlocal(L, func, findlocal(L, func, 2), &name)) != LUA_NOOBJECT) {
112 lua_pushobject(val); 114 lua_pushobject(L, val);
113 lua_pushstring(name); 115 lua_pushstring(L, name);
114 } 116 }
115 /* else return nil */ 117 /* else return nil */
116 } 118 }
117 else { /* collect all locals in a table */ 119 else { /* collect all locals in a table */
118 lua_Object result = lua_createtable(); 120 lua_Object result = lua_createtable(L);
119 int i; 121 int i;
120 for (i=1; ;i++) { 122 for (i=1; ;i++) {
121 if ((val = lua_getlocal(func, i, &name)) == LUA_NOOBJECT) 123 if ((val = lua_getlocal(L, func, i, &name)) == LUA_NOOBJECT)
122 break; 124 break;
123 lua_pushobject(result); 125 lua_pushobject(L, result);
124 lua_pushstring(name); 126 lua_pushstring(L, name);
125 lua_pushobject(val); 127 lua_pushobject(L, val);
126 lua_settable(); /* result[name] = value */ 128 lua_settable(L); /* result[name] = value */
127 } 129 }
128 lua_pushobject(result); 130 lua_pushobject(L, result);
129 } 131 }
130} 132}
131 133
132 134
133static void setlocal (void) { 135static void setlocal (lua_State *L) {
134 lua_Object func = lua_stackedfunction(luaL_check_int(1)); 136 lua_Object func = lua_stackedfunction(L, luaL_check_int(L, 1));
135 int numvar; 137 int numvar;
136 luaL_arg_check(func != LUA_NOOBJECT, 1, "level out of range"); 138 luaL_arg_check(L, func != LUA_NOOBJECT, 1, "level out of range");
137 numvar = findlocal(func, 2); 139 numvar = findlocal(L, func, 2);
138 lua_pushobject(luaL_nonnullarg(3)); 140 lua_pushobject(L, luaL_nonnullarg(L, 3));
139 if (!lua_setlocal(func, numvar)) 141 if (!lua_setlocal(L, func, numvar))
140 lua_error("no such local variable"); 142 lua_error(L, "no such local variable");
141} 143}
142 144
143 145
@@ -146,57 +148,57 @@ static int linehook = -1; /* Lua reference to line hook function */
146static int callhook = -1; /* Lua reference to call hook function */ 148static int callhook = -1; /* Lua reference to call hook function */
147 149
148 150
149static void dohook (int ref) { 151static void dohook (lua_State *L, int ref) {
150 lua_LHFunction oldlinehook = lua_setlinehook(NULL); 152 lua_LHFunction oldlinehook = lua_setlinehook(L, NULL);
151 lua_CHFunction oldcallhook = lua_setcallhook(NULL); 153 lua_CHFunction oldcallhook = lua_setcallhook(L, NULL);
152 lua_callfunction(lua_getref(ref)); 154 lua_callfunction(L, lua_getref(L, ref));
153 lua_setlinehook(oldlinehook); 155 lua_setlinehook(L, oldlinehook);
154 lua_setcallhook(oldcallhook); 156 lua_setcallhook(L, oldcallhook);
155} 157}
156 158
157 159
158static void linef (int line) { 160static void linef (lua_State *L, int line) {
159 lua_pushnumber(line); 161 lua_pushnumber(L, line);
160 dohook(linehook); 162 dohook(L, linehook);
161} 163}
162 164
163 165
164static void callf (lua_Function func, const char *file, int line) { 166static void callf (lua_State *L, lua_Function func, const char *file, int line) {
165 if (func != LUA_NOOBJECT) { 167 if (func != LUA_NOOBJECT) {
166 lua_pushobject(func); 168 lua_pushobject(L, func);
167 lua_pushstring(file); 169 lua_pushstring(L, file);
168 lua_pushnumber(line); 170 lua_pushnumber(L, line);
169 } 171 }
170 dohook(callhook); 172 dohook(L, callhook);
171} 173}
172 174
173 175
174static void setcallhook (void) { 176static void setcallhook (lua_State *L) {
175 lua_Object f = lua_getparam(1); 177 lua_Object f = lua_getparam(L, 1);
176 lua_unref(callhook); 178 lua_unref(L, callhook);
177 if (f == LUA_NOOBJECT) { 179 if (f == LUA_NOOBJECT) {
178 callhook = -1; 180 callhook = -1;
179 lua_setcallhook(NULL); 181 lua_setcallhook(L, NULL);
180 } 182 }
181 else { 183 else {
182 lua_pushobject(f); 184 lua_pushobject(L, f);
183 callhook = lua_ref(1); 185 callhook = lua_ref(L, 1);
184 lua_setcallhook(callf); 186 lua_setcallhook(L, callf);
185 } 187 }
186} 188}
187 189
188 190
189static void setlinehook (void) { 191static void setlinehook (lua_State *L) {
190 lua_Object f = lua_getparam(1); 192 lua_Object f = lua_getparam(L, 1);
191 lua_unref(linehook); 193 lua_unref(L, linehook);
192 if (f == LUA_NOOBJECT) { 194 if (f == LUA_NOOBJECT) {
193 linehook = -1; 195 linehook = -1;
194 lua_setlinehook(NULL); 196 lua_setlinehook(L, NULL);
195 } 197 }
196 else { 198 else {
197 lua_pushobject(f); 199 lua_pushobject(L, f);
198 linehook = lua_ref(1); 200 linehook = lua_ref(L, 1);
199 lua_setlinehook(linef); 201 lua_setlinehook(L, linef);
200 } 202 }
201} 203}
202 204
@@ -211,7 +213,7 @@ static const struct luaL_reg dblib[] = {
211}; 213};
212 214
213 215
214void lua_dblibopen (void) { 216void lua_dblibopen (lua_State *L) {
215 luaL_openlib(dblib, (sizeof(dblib)/sizeof(dblib[0]))); 217 luaL_openlib(L, dblib, (sizeof(dblib)/sizeof(dblib[0])));
216} 218}
217 219