aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1996-01-09 18:22:44 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1996-01-09 18:22:44 -0200
commitb2afc410fa395900a0e620cfa7abc73754ac974f (patch)
tree7c84cdcdf09fc86ec0582c93835c1863aa7e4b31
parent19cfa3239399d6d382cfdda3da8f50396630849a (diff)
downloadlua-b2afc410fa395900a0e620cfa7abc73754ac974f.tar.gz
lua-b2afc410fa395900a0e620cfa7abc73754ac974f.tar.bz2
lua-b2afc410fa395900a0e620cfa7abc73754ac974f.zip
hooks for line change and function calls; first version.
-rw-r--r--luadebug.h10
-rw-r--r--opcode.c88
2 files changed, 88 insertions, 10 deletions
diff --git a/luadebug.h b/luadebug.h
index 0a303702..9a702040 100644
--- a/luadebug.h
+++ b/luadebug.h
@@ -2,7 +2,7 @@
2** LUA - Linguagem para Usuarios de Aplicacao 2** LUA - Linguagem para Usuarios de Aplicacao
3** Grupo de Tecnologia em Computacao Grafica 3** Grupo de Tecnologia em Computacao Grafica
4** TeCGraf - PUC-Rio 4** TeCGraf - PUC-Rio
5** $Id: luadebug.h,v 1.1 1995/10/17 14:12:45 roberto Exp roberto $ 5** $Id: luadebug.h,v 1.2 1995/10/26 14:21:56 roberto Exp $
6*/ 6*/
7 7
8 8
@@ -11,10 +11,14 @@
11 11
12#include "lua.h" 12#include "lua.h"
13 13
14typedef void (*lua_LHFunction) (int line);
15typedef void (*lua_CHFunction) (lua_Object func, char *file, int line);
16
14lua_Object lua_stackedfunction(int level); 17lua_Object lua_stackedfunction(int level);
15void lua_funcinfo (lua_Object func, char **filename, int *linedefined); 18void lua_funcinfo (lua_Object func, char **filename, int *linedefined);
16int lua_currentline (lua_Object func); 19int lua_currentline (lua_Object func);
17char *getobjname (lua_Object o, char **name); 20char *lua_getobjname (lua_Object o, char **name);
18 21lua_LHFunction lua_setlinehook (lua_LHFunction hook);
22lua_CHFunction lua_setcallhook (lua_CHFunction hook);
19 23
20#endif 24#endif
diff --git a/opcode.c b/opcode.c
index 3d1b4372..d946df96 100644
--- a/opcode.c
+++ b/opcode.c
@@ -3,7 +3,7 @@
3** TecCGraf - PUC-Rio 3** TecCGraf - PUC-Rio
4*/ 4*/
5 5
6char *rcs_opcode="$Id: opcode.c,v 3.50 1995/11/16 20:46:24 roberto Exp roberto $"; 6char *rcs_opcode="$Id: opcode.c,v 3.50 1995/11/16 20:46:24 roberto Exp $";
7 7
8#include <setjmp.h> 8#include <setjmp.h>
9#include <stdlib.h> 9#include <stdlib.h>
@@ -53,6 +53,11 @@ static int CnResults = 0; /* when Lua calls C, has the number of parameters; */
53static jmp_buf *errorJmp = NULL; /* current error recover point */ 53static jmp_buf *errorJmp = NULL; /* current error recover point */
54 54
55 55
56/* Hooks */
57static lua_LHFunction line_hook = NULL;
58static lua_CHFunction call_hook = NULL;
59
60
56static StkId lua_execute (Byte *pc, StkId base); 61static StkId lua_execute (Byte *pc, StkId base);
57static void do_call (StkId base, int nResults); 62static void do_call (StkId base, int nResults);
58 63
@@ -64,6 +69,23 @@ Object *luaI_Address (lua_Object o)
64} 69}
65 70
66 71
72/*
73** Functions to change hook functions.
74*/
75lua_LHFunction lua_setlinehook (lua_LHFunction hook)
76{
77 lua_LHFunction temp = line_hook;
78 line_hook = hook;
79 return temp;
80}
81
82lua_CHFunction lua_setcallhook (lua_CHFunction hook)
83{
84 lua_CHFunction temp = call_hook;
85 call_hook = hook;
86 return temp;
87}
88
67 89
68/* 90/*
69** Init stack 91** Init stack
@@ -193,6 +215,48 @@ static void open_stack (int nelems)
193 215
194 216
195/* 217/*
218** call Line hook
219*/
220static void lineHook (int line)
221{
222 StkId oldBase = CBase;
223 int oldCnResults = CnResults;
224 StkId old_top = CBase = top-stack;
225 CnResults = 0;
226 (*line_hook)(line);
227 top = stack+old_top;
228 CnResults = oldCnResults;
229 CBase = oldBase;
230}
231
232
233/*
234** Call hook
235** The function being called is in [stack+base-1]
236*/
237static void callHook (StkId base, lua_Type type, int isreturn)
238{
239 StkId oldBase = CBase;
240 int oldCnResults = CnResults;
241 StkId old_top = CBase = top-stack;
242 CnResults = 0;
243 if (isreturn)
244 (*call_hook)(LUA_NOOBJECT, "(return)", 0);
245 else
246 {
247 Object *f = stack+base-1;
248 if (type == LUA_T_MARK)
249 (*call_hook)(Ref(f), f->value.tf->fileName, f->value.tf->lineDefined);
250 else
251 (*call_hook)(Ref(f), "(C)", -1);
252 }
253 top = stack+old_top;
254 CnResults = oldCnResults;
255 CBase = oldBase;
256}
257
258
259/*
196** Call a C function. CBase will point to the top of the stack, 260** Call a C function. CBase will point to the top of the stack,
197** and CnResults is the number of parameters. Returns an index 261** and CnResults is the number of parameters. Returns an index
198** to the first result from C. 262** to the first result from C.
@@ -204,8 +268,15 @@ static StkId callC (lua_CFunction func, StkId base)
204 StkId firstResult; 268 StkId firstResult;
205 CnResults = (top-stack) - base; 269 CnResults = (top-stack) - base;
206 /* incorporate parameters on the stack */ 270 /* incorporate parameters on the stack */
207 CBase = base+CnResults; 271 CBase = base+CnResults; /* == top-stack */
208 (*func)(); 272 if (call_hook)
273 {
274 callHook (base, LUA_T_CMARK, 0);
275 (*func)();
276 callHook (base, LUA_T_CMARK, 1);
277 }
278 else
279 (*func)();
209 firstResult = CBase; 280 firstResult = CBase;
210 CBase = oldBase; 281 CBase = oldBase;
211 CnResults = oldCnResults; 282 CnResults = oldCnResults;
@@ -784,6 +855,8 @@ static void comparison (lua_Type tag_less, lua_Type tag_equal,
784*/ 855*/
785static StkId lua_execute (Byte *pc, StkId base) 856static StkId lua_execute (Byte *pc, StkId base)
786{ 857{
858 if (call_hook)
859 callHook (base, LUA_T_MARK, 0);
787 while (1) 860 while (1)
788 { 861 {
789 OpCode opcode; 862 OpCode opcode;
@@ -1144,10 +1217,10 @@ static StkId lua_execute (Byte *pc, StkId base)
1144 break; 1217 break;
1145 1218
1146 case RETCODE0: 1219 case RETCODE0:
1147 return base;
1148
1149 case RETCODE: 1220 case RETCODE:
1150 return base+*pc; 1221 if (call_hook)
1222 callHook (base, LUA_T_MARK, 1);
1223 return (base + ((opcode==RETCODE0) ? 0 : *pc));
1151 1224
1152 case SETLINE: 1225 case SETLINE:
1153 { 1226 {
@@ -1161,6 +1234,8 @@ static StkId lua_execute (Byte *pc, StkId base)
1161 (stack+base-1)->tag = LUA_T_LINE; 1234 (stack+base-1)->tag = LUA_T_LINE;
1162 } 1235 }
1163 (stack+base-1)->value.i = code.w; 1236 (stack+base-1)->value.i = code.w;
1237 if (line_hook)
1238 lineHook (code.w);
1164 break; 1239 break;
1165 } 1240 }
1166 1241
@@ -1170,4 +1245,3 @@ static StkId lua_execute (Byte *pc, StkId base)
1170 } 1245 }
1171} 1246}
1172 1247
1173