aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2002-11-25 15:47:13 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2002-11-25 15:47:13 -0200
commit5f698f8b6f7e5fb18e0a7386dc506b0d5b538e6b (patch)
tree30ae565e9ca884a66c3cca023d24950972e02b0b
parent9b1c586b2f5b86173bf0ceca8a0dd84510af9024 (diff)
downloadlua-5f698f8b6f7e5fb18e0a7386dc506b0d5b538e6b.tar.gz
lua-5f698f8b6f7e5fb18e0a7386dc506b0d5b538e6b.tar.bz2
lua-5f698f8b6f7e5fb18e0a7386dc506b0d5b538e6b.zip
simpler interface to hooks + use of `int' to count hooks
-rw-r--r--ldblib.c20
-rw-r--r--ldebug.c17
-rw-r--r--llimits.h7
-rw-r--r--lstate.h6
-rw-r--r--ltests.c12
-rw-r--r--lua.c11
-rw-r--r--lua.h12
-rw-r--r--lvm.c4
8 files changed, 41 insertions, 48 deletions
diff --git a/ldblib.c b/ldblib.c
index b5027ff1..3ab968c0 100644
--- a/ldblib.c
+++ b/ldblib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldblib.c,v 1.71 2002/11/14 15:41:38 roberto Exp roberto $ 2** $Id: ldblib.c,v 1.72 2002/11/18 15:23:15 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*/
@@ -126,16 +126,17 @@ static void hookf (lua_State *L, lua_Debug *ar) {
126} 126}
127 127
128 128
129static unsigned long makemask (const char *smask, int count) { 129static int makemask (const char *smask, int count) {
130 unsigned long mask = 0; 130 int mask = 0;
131 if (strchr(smask, 'c')) mask |= LUA_MASKCALL; 131 if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
132 if (strchr(smask, 'r')) mask |= LUA_MASKRET; 132 if (strchr(smask, 'r')) mask |= LUA_MASKRET;
133 if (strchr(smask, 'l')) mask |= LUA_MASKLINE; 133 if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
134 return mask | LUA_MASKCOUNT(count); 134 if (count > 0) mask |= LUA_MASKCOUNT;
135 return mask;
135} 136}
136 137
137 138
138static char *unmakemask (unsigned long mask, char *smask) { 139static char *unmakemask (int mask, char *smask) {
139 int i = 0; 140 int i = 0;
140 if (mask & LUA_MASKCALL) smask[i++] = 'c'; 141 if (mask & LUA_MASKCALL) smask[i++] = 'c';
141 if (mask & LUA_MASKRET) smask[i++] = 'r'; 142 if (mask & LUA_MASKRET) smask[i++] = 'r';
@@ -148,14 +149,13 @@ static char *unmakemask (unsigned long mask, char *smask) {
148static int sethook (lua_State *L) { 149static int sethook (lua_State *L) {
149 if (lua_isnoneornil(L, 1)) { 150 if (lua_isnoneornil(L, 1)) {
150 lua_settop(L, 1); 151 lua_settop(L, 1);
151 lua_sethook(L, NULL, 0); /* turn off hooks */ 152 lua_sethook(L, NULL, 0, 0); /* turn off hooks */
152 } 153 }
153 else { 154 else {
154 const char *smask = luaL_checkstring(L, 2); 155 const char *smask = luaL_checkstring(L, 2);
155 lua_Number count = luaL_optnumber(L, 3, 0); 156 lua_Number count = luaL_optnumber(L, 3, 0);
156 luaL_checktype(L, 1, LUA_TFUNCTION); 157 luaL_checktype(L, 1, LUA_TFUNCTION);
157 luaL_argcheck(L, count <= LUA_MAXCOUNT, 2, "count too large (>= 2^24)"); 158 lua_sethook(L, hookf, makemask(smask, count), count);
158 lua_sethook(L, hookf, makemask(smask, (int)count));
159 } 159 }
160 lua_pushlightuserdata(L, (void *)&KEY_HOOK); 160 lua_pushlightuserdata(L, (void *)&KEY_HOOK);
161 lua_pushvalue(L, 1); 161 lua_pushvalue(L, 1);
@@ -166,7 +166,7 @@ static int sethook (lua_State *L) {
166 166
167static int gethook (lua_State *L) { 167static int gethook (lua_State *L) {
168 char buff[5]; 168 char buff[5];
169 unsigned long mask = lua_gethookmask(L); 169 int mask = lua_gethookmask(L);
170 lua_Hook hook = lua_gethook(L); 170 lua_Hook hook = lua_gethook(L);
171 if (hook != NULL && hook != hookf) /* external hook? */ 171 if (hook != NULL && hook != hookf) /* external hook? */
172 lua_pushliteral(L, "external hook"); 172 lua_pushliteral(L, "external hook");
@@ -175,7 +175,7 @@ static int gethook (lua_State *L) {
175 lua_rawget(L, LUA_REGISTRYINDEX); /* get hook */ 175 lua_rawget(L, LUA_REGISTRYINDEX); /* get hook */
176 } 176 }
177 lua_pushstring(L, unmakemask(mask, buff)); 177 lua_pushstring(L, unmakemask(mask, buff));
178 lua_pushnumber(L, lua_getmaskcount(mask)); 178 lua_pushnumber(L, lua_gethookcount(L));
179 return 3; 179 return 3;
180} 180}
181 181
diff --git a/ldebug.c b/ldebug.c
index 8dce4a8f..bf2a2fa9 100644
--- a/ldebug.c
+++ b/ldebug.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldebug.c,v 1.137 2002/11/18 11:01:55 roberto Exp roberto $ 2** $Id: ldebug.c,v 1.138 2002/11/21 15:16:04 roberto Exp roberto $
3** Debug Interface 3** Debug Interface
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -60,17 +60,15 @@ void luaG_inithooks (lua_State *L) {
60/* 60/*
61** this function can be called asynchronous (e.g. during a signal) 61** this function can be called asynchronous (e.g. during a signal)
62*/ 62*/
63LUA_API int lua_sethook (lua_State *L, lua_Hook func, unsigned long mask) { 63LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count) {
64 ls_count count = lua_getmaskcount(mask);
65 if (func == NULL || mask == 0) { /* turn off hooks? */ 64 if (func == NULL || mask == 0) { /* turn off hooks? */
66 mask = 0; 65 mask = 0;
67 func = NULL; 66 func = NULL;
68 } 67 }
69 else if (count > 0) mask |= (1<<LUA_HOOKCOUNT);
70 L->hook = func; 68 L->hook = func;
71 L->basehookcount = count; 69 L->basehookcount = count;
72 resethookcount(L); 70 resethookcount(L);
73 L->hookmask = cast(lu_byte, mask & 0xf); 71 L->hookmask = cast(lu_byte, mask);
74 L->hookinit = 0; 72 L->hookinit = 0;
75 return 1; 73 return 1;
76} 74}
@@ -81,8 +79,13 @@ LUA_API lua_Hook lua_gethook (lua_State *L) {
81} 79}
82 80
83 81
84LUA_API unsigned long lua_gethookmask (lua_State *L) { 82LUA_API int lua_gethookmask (lua_State *L) {
85 return L->hookmask | LUA_MASKCOUNT(L->basehookcount); 83 return L->hookmask;
84}
85
86
87LUA_API int lua_gethookcount (lua_State *L) {
88 return L->basehookcount;
86} 89}
87 90
88 91
diff --git a/llimits.h b/llimits.h
index 96d1fc17..c2fb9589 100644
--- a/llimits.h
+++ b/llimits.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: llimits.h,v 1.49 2002/11/22 17:16:52 roberto Exp roberto $ 2** $Id: llimits.h,v 1.50 2002/11/22 18:01:46 roberto Exp roberto $
3** Limits, basic types, and some other `installation-dependent' definitions 3** Limits, basic types, and some other `installation-dependent' definitions
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -52,11 +52,6 @@ typedef unsigned long lu_mem;
52/* an integer big enough to count the number of strings in use */ 52/* an integer big enough to count the number of strings in use */
53typedef long ls_nstr; 53typedef long ls_nstr;
54 54
55/* an integer big enough to count the number of steps when calling a
56** `count' hook */
57typedef long ls_count;
58
59
60/* chars used as small naturals (so that `char' is reserved for characters) */ 55/* chars used as small naturals (so that `char' is reserved for characters) */
61typedef unsigned char lu_byte; 56typedef unsigned char lu_byte;
62 57
diff --git a/lstate.h b/lstate.h
index 654ec6c1..2e184839 100644
--- a/lstate.h
+++ b/lstate.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstate.h,v 1.106 2002/11/22 17:16:52 roberto Exp roberto $ 2** $Id: lstate.h,v 1.107 2002/11/22 18:01:46 roberto Exp roberto $
3** Global State 3** Global State
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -143,8 +143,8 @@ struct lua_State {
143 lu_byte hookmask; 143 lu_byte hookmask;
144 lu_byte allowhook; 144 lu_byte allowhook;
145 lu_byte hookinit; 145 lu_byte hookinit;
146 ls_count basehookcount; 146 int basehookcount;
147 ls_count hookcount; 147 int hookcount;
148 lua_Hook hook; 148 lua_Hook hook;
149 TObject _gt; /* table of globals */ 149 TObject _gt; /* table of globals */
150 GCObject *openupval; /* list of open upvalues in this stack */ 150 GCObject *openupval; /* list of open upvalues in this stack */
diff --git a/ltests.c b/ltests.c
index e6643a0c..f1f50364 100644
--- a/ltests.c
+++ b/ltests.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltests.c,v 1.144 2002/11/14 16:59:16 roberto Exp roberto $ 2** $Id: ltests.c,v 1.145 2002/11/18 15:24:27 roberto Exp roberto $
3** Internal Module for Debugging of the Lua Implementation 3** Internal Module for Debugging of the Lua Implementation
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -702,12 +702,14 @@ static void yieldf (lua_State *L, lua_Debug *ar) {
702 702
703static int setyhook (lua_State *L) { 703static int setyhook (lua_State *L) {
704 if (lua_isnoneornil(L, 1)) 704 if (lua_isnoneornil(L, 1))
705 lua_sethook(L, NULL, 0); /* turn off hooks */ 705 lua_sethook(L, NULL, 0, 0); /* turn off hooks */
706 else { 706 else {
707 const char *smask = luaL_checkstring(L, 1); 707 const char *smask = luaL_checkstring(L, 1);
708 unsigned long count = LUA_MASKCOUNT(luaL_optint(L, 2, 0)); 708 int count = luaL_optint(L, 2, 0);
709 if (strchr(smask, 'l')) count |= LUA_MASKLINE; 709 int mask = 0;
710 lua_sethook(L, yieldf, count); 710 if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
711 if (count > 0) mask |= LUA_MASKCOUNT;
712 lua_sethook(L, yieldf, mask, count);
711 } 713 }
712 return 0; 714 return 0;
713} 715}
diff --git a/lua.c b/lua.c
index 13657e35..cc03d08f 100644
--- a/lua.c
+++ b/lua.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.c,v 1.108 2002/11/14 15:42:05 roberto Exp roberto $ 2** $Id: lua.c,v 1.109 2002/11/19 13:49:43 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*/
@@ -57,9 +57,6 @@ static lua_State *L = NULL;
57static const char *progname; 57static const char *progname;
58 58
59 59
60static lua_Hook old_hook = NULL;
61static unsigned long old_mask = 0;
62
63 60
64static const luaL_reg lualibs[] = { 61static const luaL_reg lualibs[] = {
65 {"baselib", lua_baselibopen}, 62 {"baselib", lua_baselibopen},
@@ -77,7 +74,7 @@ static const luaL_reg lualibs[] = {
77 74
78static void lstop (lua_State *l, lua_Debug *ar) { 75static void lstop (lua_State *l, lua_Debug *ar) {
79 (void)ar; /* unused arg. */ 76 (void)ar; /* unused arg. */
80 lua_sethook(l, old_hook, old_mask); 77 lua_sethook(l, NULL, 0, 0);
81 luaL_error(l, "interrupted!"); 78 luaL_error(l, "interrupted!");
82} 79}
83 80
@@ -85,9 +82,7 @@ static void lstop (lua_State *l, lua_Debug *ar) {
85static void laction (int i) { 82static void laction (int i) {
86 signal(i, SIG_DFL); /* if another SIGINT happens before lstop, 83 signal(i, SIG_DFL); /* if another SIGINT happens before lstop,
87 terminate process (default action) */ 84 terminate process (default action) */
88 old_hook = lua_gethook(L); 85 lua_sethook(L, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
89 old_mask = lua_gethookmask(L);
90 lua_sethook(L, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT(1));
91} 86}
92 87
93 88
diff --git a/lua.h b/lua.h
index c3abafa7..4b600548 100644
--- a/lua.h
+++ b/lua.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.h,v 1.164 2002/11/14 11:51:50 roberto Exp roberto $ 2** $Id: lua.h,v 1.165 2002/11/18 11:01:55 roberto Exp roberto $
3** Lua - An Extensible Extension Language 3** Lua - An Extensible Extension Language
4** Tecgraf: Computer Graphics Technology Group, PUC-Rio, Brazil 4** Tecgraf: Computer Graphics Technology Group, PUC-Rio, Brazil
5** http://www.lua.org mailto:info@lua.org 5** http://www.lua.org mailto:info@lua.org
@@ -330,10 +330,7 @@ LUA_API int lua_pushupvalues (lua_State *L);
330#define LUA_MASKCALL (1 << LUA_HOOKCALL) 330#define LUA_MASKCALL (1 << LUA_HOOKCALL)
331#define LUA_MASKRET (1 << LUA_HOOKRET) 331#define LUA_MASKRET (1 << LUA_HOOKRET)
332#define LUA_MASKLINE (1 << LUA_HOOKLINE) 332#define LUA_MASKLINE (1 << LUA_HOOKLINE)
333#define LUA_MASKCOUNT(count) ((unsigned long)(count) << 8) 333#define LUA_MASKCOUNT (1 << LUA_HOOKCOUNT)
334#define lua_getmaskcount(mask) ((mask) >> 8)
335
336#define LUA_MAXCOUNT ((~(unsigned long)0) >> 8)
337 334
338typedef struct lua_Debug lua_Debug; /* activation record */ 335typedef struct lua_Debug lua_Debug; /* activation record */
339 336
@@ -345,9 +342,10 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar);
345LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n); 342LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n);
346LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n); 343LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n);
347 344
348LUA_API int lua_sethook (lua_State *L, lua_Hook func, unsigned long mask); 345LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count);
349LUA_API lua_Hook lua_gethook (lua_State *L); 346LUA_API lua_Hook lua_gethook (lua_State *L);
350LUA_API unsigned long lua_gethookmask (lua_State *L); 347LUA_API int lua_gethookmask (lua_State *L);
348LUA_API int lua_gethookcount (lua_State *L);
351 349
352 350
353#define LUA_IDSIZE 60 351#define LUA_IDSIZE 60
diff --git a/lvm.c b/lvm.c
index 82210ac1..a5b07be7 100644
--- a/lvm.c
+++ b/lvm.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lvm.c,v 1.268 2002/11/21 17:19:42 roberto Exp roberto $ 2** $Id: lvm.c,v 1.269 2002/11/25 11:20:29 roberto Exp roberto $
3** Lua virtual machine 3** Lua virtual machine
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -383,7 +383,7 @@ StkId luaV_execute (lua_State *L) {
383 for (;;) { 383 for (;;) {
384 const Instruction i = *pc++; 384 const Instruction i = *pc++;
385 StkId base, ra; 385 StkId base, ra;
386 if (L->hookmask >= LUA_MASKLINE && 386 if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) &&
387 (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) { 387 (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) {
388 traceexec(L); 388 traceexec(L);
389 if (L->ci->state & CI_YIELD) { /* did hook yield? */ 389 if (L->ci->state & CI_YIELD) { /* did hook yield? */