aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2000-10-24 17:12:06 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2000-10-24 17:12:06 -0200
commitcdc8139e295a768ac581e0a7c784c1adfee0668b (patch)
treee1c4bd9160e5877ee6b61857d7ddbc33f78cff18
parente833bd47c9c48f3235097b85f0e45f79ab5c00dd (diff)
downloadlua-cdc8139e295a768ac581e0a7c784c1adfee0668b.tar.gz
lua-cdc8139e295a768ac581e0a7c784c1adfee0668b.tar.bz2
lua-cdc8139e295a768ac581e0a7c784c1adfee0668b.zip
registry mechanism
-rw-r--r--ldblib.c73
-rw-r--r--lstate.c7
-rw-r--r--lua.h8
3 files changed, 50 insertions, 38 deletions
diff --git a/ldblib.c b/ldblib.c
index f016fcde..f3b732e9 100644
--- a/ldblib.c
+++ b/ldblib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldblib.c,v 1.22 2000/10/02 20:10:55 roberto Exp roberto $ 2** $Id: ldblib.c,v 1.23 2000/10/20 16:39:03 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*/
@@ -109,58 +109,61 @@ static int setlocal (lua_State *L) {
109} 109}
110 110
111 111
112/*
113** because of these variables, this module is not reentrant, and should
114** not be used in multiple states
115*/
116
117static int linehook = LUA_NOREF; /* Lua reference to line hook function */
118static int callhook = LUA_NOREF; /* Lua reference to call hook function */
119 112
113#define KEY_CALLHOOK "dblib_callhook"
114#define KEY_LINEHOOK "dblib_linehook"
120 115
121 116
122static void linef (lua_State *L, lua_Debug *ar) { 117static void hookf (lua_State *L, const char *key) {
123 if (linehook != LUA_NOREF) { 118 lua_getregistry(L);
124 lua_getref(L, linehook); 119 lua_pushstring(L, key);
125 lua_pushnumber(L, ar->currentline); 120 lua_gettable(L, -2);
121 if (lua_isfunction(L, -1)) {
122 lua_pushvalue(L, 1);
126 lua_call(L, 1, 0); 123 lua_call(L, 1, 0);
127 } 124 }
125 else
126 lua_pop(L, 1); /* pop result from gettable */
127 lua_pop(L, 1); /* pop table */
128} 128}
129 129
130 130
131static void callf (lua_State *L, lua_Debug *ar) { 131static void callf (lua_State *L, lua_Debug *ar) {
132 if (callhook != LUA_NOREF) { 132 lua_pushstring(L, ar->event);
133 lua_getref(L, callhook); 133 hookf(L, KEY_CALLHOOK);
134 lua_pushstring(L, ar->event); 134}
135 lua_call(L, 1, 0); 135
136 } 136
137static void linef (lua_State *L, lua_Debug *ar) {
138 lua_pushnumber(L, ar->currentline);
139 hookf(L, KEY_LINEHOOK);
140}
141
142
143static void sethook (lua_State *L, const char *key, lua_Hook hook,
144 lua_Hook (*sethookf)(lua_State * L, lua_Hook h)) {
145 lua_settop(L, 1);
146 lua_getregistry(L);
147 lua_pushstring(L, key);
148 if (lua_isnil(L, 1))
149 (*sethookf)(L, NULL);
150 else if (lua_isfunction(L, 1))
151 (*sethookf)(L, hook);
152 else
153 luaL_argerror(L, 1, "function expected");
154 lua_pushvalue(L, 1);
155 lua_settable(L, -3);
137} 156}
138 157
139 158
140static int setcallhook (lua_State *L) { 159static int setcallhook (lua_State *L) {
141 lua_unref(L, callhook); 160 sethook(L, KEY_CALLHOOK, callf, lua_setcallhook);
142 if (lua_isnull(L, 1)) {
143 callhook = LUA_NOREF;
144 lua_setcallhook(L, NULL);
145 }
146 else {
147 callhook = lua_ref(L, 1);
148 lua_setcallhook(L, callf);
149 }
150 return 0; 161 return 0;
151} 162}
152 163
153 164
154static int setlinehook (lua_State *L) { 165static int setlinehook (lua_State *L) {
155 lua_unref(L, linehook); 166 sethook(L, KEY_LINEHOOK, linef, lua_setlinehook);
156 if (lua_isnull(L, 1)) {
157 linehook = LUA_NOREF;
158 lua_setlinehook(L, NULL);
159 }
160 else {
161 linehook = lua_ref(L, 1);
162 lua_setlinehook(L, linef);
163 }
164 return 0; 167 return 0;
165} 168}
166 169
diff --git a/lstate.c b/lstate.c
index bd07488a..ba7ed328 100644
--- a/lstate.c
+++ b/lstate.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstate.c,v 1.44 2000/10/06 19:28:47 roberto Exp roberto $ 2** $Id: lstate.c,v 1.45 2000/10/20 16:39:03 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*/
@@ -47,16 +47,19 @@ static void f_luaopen (lua_State *L, void *ud) {
47 stacksize = DEFAULT_STACK_SIZE; 47 stacksize = DEFAULT_STACK_SIZE;
48 else 48 else
49 stacksize += LUA_MINSTACK; 49 stacksize += LUA_MINSTACK;
50 L->gt = luaH_new(L, 10); 50 L->gt = luaH_new(L, 10); /* table of globals */
51 luaD_init(L, stacksize); 51 luaD_init(L, stacksize);
52 luaS_init(L); 52 luaS_init(L);
53 luaX_init(L); 53 luaX_init(L);
54 luaT_init(L); 54 luaT_init(L);
55 lua_newtable(L);
56 lua_ref(L, 1); /* create registry */
55 lua_register(L, LUA_ERRORMESSAGE, errormessage); 57 lua_register(L, LUA_ERRORMESSAGE, errormessage);
56#ifdef DEBUG 58#ifdef DEBUG
57 luaB_opentests(L); 59 luaB_opentests(L);
58 if (lua_state == NULL) lua_state = L; /* keep first state to be opened */ 60 if (lua_state == NULL) lua_state = L; /* keep first state to be opened */
59#endif 61#endif
62 LUA_ASSERT(lua_gettop(L) == 0, "wrong API stack");
60} 63}
61 64
62 65
diff --git a/lua.h b/lua.h
index f2cc880a..c2c77a80 100644
--- a/lua.h
+++ b/lua.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.h,v 1.74 2000/10/09 15:46:43 roberto Exp roberto $ 2** $Id: lua.h,v 1.75 2000/10/20 16:39:03 roberto Exp roberto $
3** Lua - An Extensible Extension Language 3** Lua - An Extensible Extension Language
4** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil 4** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
5** e-mail: lua@tecgraf.puc-rio.br 5** e-mail: lua@tecgraf.puc-rio.br
@@ -29,12 +29,16 @@
29#define LUA_ERRORMESSAGE "_ERRORMESSAGE" 29#define LUA_ERRORMESSAGE "_ERRORMESSAGE"
30 30
31 31
32/* pre-defined references */
32#define LUA_NOREF (-2) 33#define LUA_NOREF (-2)
33#define LUA_REFNIL (-1) 34#define LUA_REFNIL (-1)
35#define LUA_REFREGISTRY 0
34 36
37/* pre-defined tags */
35#define LUA_ANYTAG (-1) 38#define LUA_ANYTAG (-1)
36#define LUA_NOTAG (-2) 39#define LUA_NOTAG (-2)
37 40
41
38#define LUA_MULTRET (-1) 42#define LUA_MULTRET (-1)
39 43
40 44
@@ -198,6 +202,8 @@ LUA_API void lua_concat (lua_State *L, int n);
198#define lua_isnil(L,n) (lua_type(L,n) == LUA_TNIL) 202#define lua_isnil(L,n) (lua_type(L,n) == LUA_TNIL)
199#define lua_isnull(L,n) (lua_type(L,n) == LUA_TNONE) 203#define lua_isnull(L,n) (lua_type(L,n) == LUA_TNONE)
200 204
205#define lua_getregistry(L) lua_getref(L, LUA_REFREGISTRY)
206
201#endif 207#endif
202 208
203 209