summaryrefslogtreecommitdiff
path: root/ldo.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2001-03-07 15:09:25 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2001-03-07 15:09:25 -0300
commit6048c4f74d7d63d6c2f5a53cd8e4ee01f6702be9 (patch)
tree1f8fd00995288958a32ebd8f97863c5f1cb8f776 /ldo.c
parent5e870f86a255988ca85eda795adc31063ec1ac70 (diff)
downloadlua-6048c4f74d7d63d6c2f5a53cd8e4ee01f6702be9.tar.gz
lua-6048c4f74d7d63d6c2f5a53cd8e4ee01f6702be9.tar.bz2
lua-6048c4f74d7d63d6c2f5a53cd8e4ee01f6702be9.zip
better way to link callinfo's and stack
Diffstat (limited to 'ldo.c')
-rw-r--r--ldo.c47
1 files changed, 20 insertions, 27 deletions
diff --git a/ldo.c b/ldo.c
index fb6233be..95db67a3 100644
--- a/ldo.c
+++ b/ldo.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldo.c,v 1.129 2001/02/23 17:28:12 roberto Exp roberto $ 2** $Id: ldo.c,v 1.130 2001/03/02 17:27:50 roberto Exp roberto $
3** Stack and Call structure of Lua 3** Stack and Call structure of Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -42,7 +42,7 @@ void luaD_init (lua_State *L, int stacksize) {
42 stacksize += EXTRA_STACK; 42 stacksize += EXTRA_STACK;
43 L->stack = luaM_newvector(L, stacksize, TObject); 43 L->stack = luaM_newvector(L, stacksize, TObject);
44 L->stacksize = stacksize; 44 L->stacksize = stacksize;
45 L->Cbase = L->top = L->stack; 45 L->basefunc.base = L->top = L->stack;
46 restore_stack_limit(L); 46 restore_stack_limit(L);
47} 47}
48 48
@@ -90,8 +90,7 @@ static void luaD_openstack (lua_State *L, StkId pos) {
90 90
91 91
92static void dohook (lua_State *L, lua_Debug *ar, lua_Hook hook) { 92static void dohook (lua_State *L, lua_Debug *ar, lua_Hook hook) {
93 StkId old_Cbase = L->Cbase; 93 StkId old_top = L->top;
94 StkId old_top = L->Cbase = L->top;
95 luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ 94 luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
96 L->allowhooks = 0; /* cannot call hooks inside a hook */ 95 L->allowhooks = 0; /* cannot call hooks inside a hook */
97 lua_unlock(L); 96 lua_unlock(L);
@@ -100,45 +99,41 @@ static void dohook (lua_State *L, lua_Debug *ar, lua_Hook hook) {
100 lua_assert(L->allowhooks == 0); 99 lua_assert(L->allowhooks == 0);
101 L->allowhooks = 1; 100 L->allowhooks = 1;
102 L->top = old_top; 101 L->top = old_top;
103 L->Cbase = old_Cbase;
104} 102}
105 103
106 104
107void luaD_lineHook (lua_State *L, StkId func, int line, lua_Hook linehook) { 105void luaD_lineHook (lua_State *L, int line, lua_Hook linehook) {
108 if (L->allowhooks) { 106 if (L->allowhooks) {
109 lua_Debug ar; 107 lua_Debug ar;
110 ar._func = func;
111 ar.event = l_s("line"); 108 ar.event = l_s("line");
109 ar._ci = L->ci;
112 ar.currentline = line; 110 ar.currentline = line;
113 dohook(L, &ar, linehook); 111 dohook(L, &ar, linehook);
114 } 112 }
115} 113}
116 114
117 115
118static void luaD_callHook (lua_State *L, StkId func, lua_Hook callhook, 116static void luaD_callHook (lua_State *L, lua_Hook callhook,
119 const l_char *event) { 117 const l_char *event) {
120 if (L->allowhooks) { 118 if (L->allowhooks) {
121 lua_Debug ar; 119 lua_Debug ar;
122 ar._func = func;
123 ar.event = event; 120 ar.event = event;
124 infovalue(func)->pc = NULL; /* function is not active */ 121 ar._ci = L->ci;
122 L->ci->pc = NULL; /* function is not active */
125 dohook(L, &ar, callhook); 123 dohook(L, &ar, callhook);
126 } 124 }
127} 125}
128 126
129 127
130static StkId callCclosure (lua_State *L, const struct Closure *cl, StkId base) { 128static StkId callCclosure (lua_State *L, const struct Closure *cl) {
131 int nup = cl->nupvalues; /* number of upvalues */ 129 int nup = cl->nupvalues; /* number of upvalues */
132 StkId old_Cbase = L->Cbase;
133 int n; 130 int n;
134 L->Cbase = base; /* new base for C function */
135 luaD_checkstack(L, nup+LUA_MINSTACK); /* ensure minimum stack size */ 131 luaD_checkstack(L, nup+LUA_MINSTACK); /* ensure minimum stack size */
136 for (n=0; n<nup; n++) /* copy upvalues as extra arguments */ 132 for (n=0; n<nup; n++) /* copy upvalues as extra arguments */
137 setobj(L->top++, &cl->upvalue[n]); 133 setobj(L->top++, &cl->upvalue[n]);
138 lua_unlock(L); 134 lua_unlock(L);
139 n = (*cl->f.c)(L); /* do the actual call */ 135 n = (*cl->f.c)(L); /* do the actual call */
140 lua_lock(L); 136 lua_lock(L);
141 L->Cbase = old_Cbase; /* restore old C base */
142 return L->top - n; /* return index of first result */ 137 return L->top - n; /* return index of first result */
143} 138}
144 139
@@ -154,7 +149,6 @@ void luaD_call (lua_State *L, StkId func, int nResults) {
154 lua_Hook callhook; 149 lua_Hook callhook;
155 StkId firstResult; 150 StkId firstResult;
156 CallInfo ci; 151 CallInfo ci;
157 Closure *cl;
158 if (ttype(func) != LUA_TFUNCTION) { 152 if (ttype(func) != LUA_TFUNCTION) {
159 /* `func' is not a function; check the `function' tag method */ 153 /* `func' is not a function; check the `function' tag method */
160 Closure *tm = luaT_gettmbyObj(G(L), func, TM_FUNCTION); 154 Closure *tm = luaT_gettmbyObj(G(L), func, TM_FUNCTION);
@@ -163,18 +157,17 @@ void luaD_call (lua_State *L, StkId func, int nResults) {
163 luaD_openstack(L, func); 157 luaD_openstack(L, func);
164 setclvalue(func, tm); /* tag method is the new function to be called */ 158 setclvalue(func, tm); /* tag method is the new function to be called */
165 } 159 }
166 cl = clvalue(func); 160 ci.prev = L->ci; /* chain new callinfo */
167 ci.func = cl; 161 L->ci = &ci;
168 setivalue(func, &ci); 162 ci.base = func+1;
169 callhook = L->callhook; 163 callhook = L->callhook;
170 if (callhook) 164 if (callhook)
171 luaD_callHook(L, func, callhook, l_s("call")); 165 luaD_callHook(L, callhook, l_s("call"));
172 firstResult = (cl->isC ? callCclosure(L, cl, func+1) : 166 firstResult = (clvalue(func)->isC ? callCclosure(L, clvalue(func)) :
173 luaV_execute(L, cl, func+1)); 167 luaV_execute(L, clvalue(func), func+1));
174 if (callhook) /* same hook that was active at entry */ 168 if (callhook) /* same hook that was active at entry */
175 luaD_callHook(L, func, callhook, l_s("return")); 169 luaD_callHook(L, callhook, l_s("return"));
176 lua_assert(ttype(func) == LUA_TMARK); 170 L->ci = ci.prev; /* unchain callinfo */
177 setnilvalue(func); /* remove callinfo from the stack */
178 /* move results to `func' (to erase parameters and function) */ 171 /* move results to `func' (to erase parameters and function) */
179 if (nResults == LUA_MULTRET) { 172 if (nResults == LUA_MULTRET) {
180 while (firstResult < L->top) /* copy all results */ 173 while (firstResult < L->top) /* copy all results */
@@ -368,7 +361,7 @@ void luaD_breakrun (lua_State *L, int errcode) {
368 361
369 362
370int luaD_runprotected (lua_State *L, void (*f)(lua_State *, void *), void *ud) { 363int luaD_runprotected (lua_State *L, void (*f)(lua_State *, void *), void *ud) {
371 StkId oldCbase = L->Cbase; 364 CallInfo *oldci = L->ci;
372 StkId oldtop = L->top; 365 StkId oldtop = L->top;
373 struct lua_longjmp lj; 366 struct lua_longjmp lj;
374 int allowhooks = L->allowhooks; 367 int allowhooks = L->allowhooks;
@@ -379,7 +372,7 @@ int luaD_runprotected (lua_State *L, void (*f)(lua_State *, void *), void *ud) {
379 (*f)(L, ud); 372 (*f)(L, ud);
380 else { /* an error occurred: restore the state */ 373 else { /* an error occurred: restore the state */
381 L->allowhooks = allowhooks; 374 L->allowhooks = allowhooks;
382 L->Cbase = oldCbase; 375 L->ci = oldci;
383 L->top = oldtop; 376 L->top = oldtop;
384 restore_stack_limit(L); 377 restore_stack_limit(L);
385 } 378 }