simpler interface to hooks + use of `int' to count hooks

This commit is contained in:
Roberto Ierusalimschy
2002-11-25 15:47:13 -02:00
parent 9b1c586b2f
commit 5f698f8b6f
8 changed files with 41 additions and 48 deletions

View File

@@ -1,5 +1,5 @@
/* /*
** $Id: ldblib.c,v 1.71 2002/11/14 15:41:38 roberto Exp roberto $ ** $Id: ldblib.c,v 1.72 2002/11/18 15:23:15 roberto Exp roberto $
** Interface from Lua to its debug API ** Interface from Lua to its debug API
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -126,16 +126,17 @@ static void hookf (lua_State *L, lua_Debug *ar) {
} }
static unsigned long makemask (const char *smask, int count) { static int makemask (const char *smask, int count) {
unsigned long mask = 0; int mask = 0;
if (strchr(smask, 'c')) mask |= LUA_MASKCALL; if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
if (strchr(smask, 'r')) mask |= LUA_MASKRET; if (strchr(smask, 'r')) mask |= LUA_MASKRET;
if (strchr(smask, 'l')) mask |= LUA_MASKLINE; if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
return mask | LUA_MASKCOUNT(count); if (count > 0) mask |= LUA_MASKCOUNT;
return mask;
} }
static char *unmakemask (unsigned long mask, char *smask) { static char *unmakemask (int mask, char *smask) {
int i = 0; int i = 0;
if (mask & LUA_MASKCALL) smask[i++] = 'c'; if (mask & LUA_MASKCALL) smask[i++] = 'c';
if (mask & LUA_MASKRET) smask[i++] = 'r'; if (mask & LUA_MASKRET) smask[i++] = 'r';
@@ -148,14 +149,13 @@ static char *unmakemask (unsigned long mask, char *smask) {
static int sethook (lua_State *L) { static int sethook (lua_State *L) {
if (lua_isnoneornil(L, 1)) { if (lua_isnoneornil(L, 1)) {
lua_settop(L, 1); lua_settop(L, 1);
lua_sethook(L, NULL, 0); /* turn off hooks */ lua_sethook(L, NULL, 0, 0); /* turn off hooks */
} }
else { else {
const char *smask = luaL_checkstring(L, 2); const char *smask = luaL_checkstring(L, 2);
lua_Number count = luaL_optnumber(L, 3, 0); lua_Number count = luaL_optnumber(L, 3, 0);
luaL_checktype(L, 1, LUA_TFUNCTION); luaL_checktype(L, 1, LUA_TFUNCTION);
luaL_argcheck(L, count <= LUA_MAXCOUNT, 2, "count too large (>= 2^24)"); lua_sethook(L, hookf, makemask(smask, count), count);
lua_sethook(L, hookf, makemask(smask, (int)count));
} }
lua_pushlightuserdata(L, (void *)&KEY_HOOK); lua_pushlightuserdata(L, (void *)&KEY_HOOK);
lua_pushvalue(L, 1); lua_pushvalue(L, 1);
@@ -166,7 +166,7 @@ static int sethook (lua_State *L) {
static int gethook (lua_State *L) { static int gethook (lua_State *L) {
char buff[5]; char buff[5];
unsigned long mask = lua_gethookmask(L); int mask = lua_gethookmask(L);
lua_Hook hook = lua_gethook(L); lua_Hook hook = lua_gethook(L);
if (hook != NULL && hook != hookf) /* external hook? */ if (hook != NULL && hook != hookf) /* external hook? */
lua_pushliteral(L, "external hook"); lua_pushliteral(L, "external hook");
@@ -175,7 +175,7 @@ static int gethook (lua_State *L) {
lua_rawget(L, LUA_REGISTRYINDEX); /* get hook */ lua_rawget(L, LUA_REGISTRYINDEX); /* get hook */
} }
lua_pushstring(L, unmakemask(mask, buff)); lua_pushstring(L, unmakemask(mask, buff));
lua_pushnumber(L, lua_getmaskcount(mask)); lua_pushnumber(L, lua_gethookcount(L));
return 3; return 3;
} }

View File

@@ -1,5 +1,5 @@
/* /*
** $Id: ldebug.c,v 1.137 2002/11/18 11:01:55 roberto Exp roberto $ ** $Id: ldebug.c,v 1.138 2002/11/21 15:16:04 roberto Exp roberto $
** Debug Interface ** Debug Interface
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -60,17 +60,15 @@ void luaG_inithooks (lua_State *L) {
/* /*
** this function can be called asynchronous (e.g. during a signal) ** this function can be called asynchronous (e.g. during a signal)
*/ */
LUA_API int lua_sethook (lua_State *L, lua_Hook func, unsigned long mask) { LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count) {
ls_count count = lua_getmaskcount(mask);
if (func == NULL || mask == 0) { /* turn off hooks? */ if (func == NULL || mask == 0) { /* turn off hooks? */
mask = 0; mask = 0;
func = NULL; func = NULL;
} }
else if (count > 0) mask |= (1<<LUA_HOOKCOUNT);
L->hook = func; L->hook = func;
L->basehookcount = count; L->basehookcount = count;
resethookcount(L); resethookcount(L);
L->hookmask = cast(lu_byte, mask & 0xf); L->hookmask = cast(lu_byte, mask);
L->hookinit = 0; L->hookinit = 0;
return 1; return 1;
} }
@@ -81,8 +79,13 @@ LUA_API lua_Hook lua_gethook (lua_State *L) {
} }
LUA_API unsigned long lua_gethookmask (lua_State *L) { LUA_API int lua_gethookmask (lua_State *L) {
return L->hookmask | LUA_MASKCOUNT(L->basehookcount); return L->hookmask;
}
LUA_API int lua_gethookcount (lua_State *L) {
return L->basehookcount;
} }

View File

@@ -1,5 +1,5 @@
/* /*
** $Id: llimits.h,v 1.49 2002/11/22 17:16:52 roberto Exp roberto $ ** $Id: llimits.h,v 1.50 2002/11/22 18:01:46 roberto Exp roberto $
** Limits, basic types, and some other `installation-dependent' definitions ** Limits, basic types, and some other `installation-dependent' definitions
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -52,11 +52,6 @@ typedef unsigned long lu_mem;
/* an integer big enough to count the number of strings in use */ /* an integer big enough to count the number of strings in use */
typedef long ls_nstr; typedef long ls_nstr;
/* an integer big enough to count the number of steps when calling a
** `count' hook */
typedef long ls_count;
/* chars used as small naturals (so that `char' is reserved for characters) */ /* chars used as small naturals (so that `char' is reserved for characters) */
typedef unsigned char lu_byte; typedef unsigned char lu_byte;

View File

@@ -1,5 +1,5 @@
/* /*
** $Id: lstate.h,v 1.106 2002/11/22 17:16:52 roberto Exp roberto $ ** $Id: lstate.h,v 1.107 2002/11/22 18:01:46 roberto Exp roberto $
** Global State ** Global State
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -143,8 +143,8 @@ struct lua_State {
lu_byte hookmask; lu_byte hookmask;
lu_byte allowhook; lu_byte allowhook;
lu_byte hookinit; lu_byte hookinit;
ls_count basehookcount; int basehookcount;
ls_count hookcount; int hookcount;
lua_Hook hook; lua_Hook hook;
TObject _gt; /* table of globals */ TObject _gt; /* table of globals */
GCObject *openupval; /* list of open upvalues in this stack */ GCObject *openupval; /* list of open upvalues in this stack */

View File

@@ -1,5 +1,5 @@
/* /*
** $Id: ltests.c,v 1.144 2002/11/14 16:59:16 roberto Exp roberto $ ** $Id: ltests.c,v 1.145 2002/11/18 15:24:27 roberto Exp roberto $
** Internal Module for Debugging of the Lua Implementation ** Internal Module for Debugging of the Lua Implementation
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -702,12 +702,14 @@ static void yieldf (lua_State *L, lua_Debug *ar) {
static int setyhook (lua_State *L) { static int setyhook (lua_State *L) {
if (lua_isnoneornil(L, 1)) if (lua_isnoneornil(L, 1))
lua_sethook(L, NULL, 0); /* turn off hooks */ lua_sethook(L, NULL, 0, 0); /* turn off hooks */
else { else {
const char *smask = luaL_checkstring(L, 1); const char *smask = luaL_checkstring(L, 1);
unsigned long count = LUA_MASKCOUNT(luaL_optint(L, 2, 0)); int count = luaL_optint(L, 2, 0);
if (strchr(smask, 'l')) count |= LUA_MASKLINE; int mask = 0;
lua_sethook(L, yieldf, count); if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
if (count > 0) mask |= LUA_MASKCOUNT;
lua_sethook(L, yieldf, mask, count);
} }
return 0; return 0;
} }

11
lua.c
View File

@@ -1,5 +1,5 @@
/* /*
** $Id: lua.c,v 1.108 2002/11/14 15:42:05 roberto Exp roberto $ ** $Id: lua.c,v 1.109 2002/11/19 13:49:43 roberto Exp roberto $
** Lua stand-alone interpreter ** Lua stand-alone interpreter
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -57,9 +57,6 @@ static lua_State *L = NULL;
static const char *progname; static const char *progname;
static lua_Hook old_hook = NULL;
static unsigned long old_mask = 0;
static const luaL_reg lualibs[] = { static const luaL_reg lualibs[] = {
{"baselib", lua_baselibopen}, {"baselib", lua_baselibopen},
@@ -77,7 +74,7 @@ static const luaL_reg lualibs[] = {
static void lstop (lua_State *l, lua_Debug *ar) { static void lstop (lua_State *l, lua_Debug *ar) {
(void)ar; /* unused arg. */ (void)ar; /* unused arg. */
lua_sethook(l, old_hook, old_mask); lua_sethook(l, NULL, 0, 0);
luaL_error(l, "interrupted!"); luaL_error(l, "interrupted!");
} }
@@ -85,9 +82,7 @@ static void lstop (lua_State *l, lua_Debug *ar) {
static void laction (int i) { static void laction (int i) {
signal(i, SIG_DFL); /* if another SIGINT happens before lstop, signal(i, SIG_DFL); /* if another SIGINT happens before lstop,
terminate process (default action) */ terminate process (default action) */
old_hook = lua_gethook(L); lua_sethook(L, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
old_mask = lua_gethookmask(L);
lua_sethook(L, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT(1));
} }

12
lua.h
View File

@@ -1,5 +1,5 @@
/* /*
** $Id: lua.h,v 1.164 2002/11/14 11:51:50 roberto Exp roberto $ ** $Id: lua.h,v 1.165 2002/11/18 11:01:55 roberto Exp roberto $
** Lua - An Extensible Extension Language ** Lua - An Extensible Extension Language
** Tecgraf: Computer Graphics Technology Group, PUC-Rio, Brazil ** Tecgraf: Computer Graphics Technology Group, PUC-Rio, Brazil
** http://www.lua.org mailto:info@lua.org ** http://www.lua.org mailto:info@lua.org
@@ -330,10 +330,7 @@ LUA_API int lua_pushupvalues (lua_State *L);
#define LUA_MASKCALL (1 << LUA_HOOKCALL) #define LUA_MASKCALL (1 << LUA_HOOKCALL)
#define LUA_MASKRET (1 << LUA_HOOKRET) #define LUA_MASKRET (1 << LUA_HOOKRET)
#define LUA_MASKLINE (1 << LUA_HOOKLINE) #define LUA_MASKLINE (1 << LUA_HOOKLINE)
#define LUA_MASKCOUNT(count) ((unsigned long)(count) << 8) #define LUA_MASKCOUNT (1 << LUA_HOOKCOUNT)
#define lua_getmaskcount(mask) ((mask) >> 8)
#define LUA_MAXCOUNT ((~(unsigned long)0) >> 8)
typedef struct lua_Debug lua_Debug; /* activation record */ typedef struct lua_Debug lua_Debug; /* activation record */
@@ -345,9 +342,10 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar);
LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n); LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n);
LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n); LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n);
LUA_API int lua_sethook (lua_State *L, lua_Hook func, unsigned long mask); LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count);
LUA_API lua_Hook lua_gethook (lua_State *L); LUA_API lua_Hook lua_gethook (lua_State *L);
LUA_API unsigned long lua_gethookmask (lua_State *L); LUA_API int lua_gethookmask (lua_State *L);
LUA_API int lua_gethookcount (lua_State *L);
#define LUA_IDSIZE 60 #define LUA_IDSIZE 60

4
lvm.c
View File

@@ -1,5 +1,5 @@
/* /*
** $Id: lvm.c,v 1.268 2002/11/21 17:19:42 roberto Exp roberto $ ** $Id: lvm.c,v 1.269 2002/11/25 11:20:29 roberto Exp roberto $
** Lua virtual machine ** Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -383,7 +383,7 @@ StkId luaV_execute (lua_State *L) {
for (;;) { for (;;) {
const Instruction i = *pc++; const Instruction i = *pc++;
StkId base, ra; StkId base, ra;
if (L->hookmask >= LUA_MASKLINE && if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) &&
(--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) { (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) {
traceexec(L); traceexec(L);
if (L->ci->state & CI_YIELD) { /* did hook yield? */ if (L->ci->state & CI_YIELD) { /* did hook yield? */