aboutsummaryrefslogtreecommitdiff
path: root/ldo.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2000-08-28 14:57:04 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2000-08-28 14:57:04 -0300
commit9fdf73bc9a6b4c6afbfff1d8181fface6b1c6761 (patch)
treeda8d97d954e5ffabf9ff275df725f1e0a3a5b3e6 /ldo.c
parentf1fd9b5c2c21f24d25d7813f431a3495702ebea6 (diff)
downloadlua-9fdf73bc9a6b4c6afbfff1d8181fface6b1c6761.tar.gz
lua-9fdf73bc9a6b4c6afbfff1d8181fface6b1c6761.tar.bz2
lua-9fdf73bc9a6b4c6afbfff1d8181fface6b1c6761.zip
first version for new API
Diffstat (limited to 'ldo.c')
-rw-r--r--ldo.c87
1 files changed, 35 insertions, 52 deletions
diff --git a/ldo.c b/ldo.c
index 06eb0d2d..074638c3 100644
--- a/ldo.c
+++ b/ldo.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldo.c,v 1.84 2000/08/09 19:16:57 roberto Exp roberto $ 2** $Id: ldo.c,v 1.85 2000/08/10 19:50:47 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*/
@@ -41,8 +41,7 @@ void luaD_init (lua_State *L, int stacksize) {
41 L->stack = luaM_newvector(L, stacksize+EXTRA_STACK, TObject); 41 L->stack = luaM_newvector(L, stacksize+EXTRA_STACK, TObject);
42 L->stack_last = L->stack+(stacksize-1); 42 L->stack_last = L->stack+(stacksize-1);
43 L->stacksize = stacksize; 43 L->stacksize = stacksize;
44 L->Cstack.base = L->Cstack.lua2C = L->top = L->stack; 44 L->Cbase = L->top = L->stack;
45 L->Cstack.num = 0;
46} 45}
47 46
48 47
@@ -68,8 +67,8 @@ void luaD_checkstack (lua_State *L, int n) {
68 67
69 68
70static void restore_stack_limit (lua_State *L) { 69static void restore_stack_limit (lua_State *L) {
71 if (L->top-L->stack < L->stacksize-1) 70 if (L->top - L->stack < L->stacksize - 1)
72 L->stack_last = L->stack+(L->stacksize-1); 71 L->stack_last = L->stack + (L->stacksize-1);
73} 72}
74 73
75 74
@@ -103,9 +102,8 @@ void luaD_openstack (lua_State *L, StkId pos) {
103void luaD_lineHook (lua_State *L, StkId func, int line, lua_Hook linehook) { 102void luaD_lineHook (lua_State *L, StkId func, int line, lua_Hook linehook) {
104 if (L->allowhooks) { 103 if (L->allowhooks) {
105 lua_Debug ar; 104 lua_Debug ar;
106 struct C_Lua_Stack oldCLS = L->Cstack; 105 StkId old_Cbase = L->Cbase;
107 StkId old_top = L->Cstack.lua2C = L->Cstack.base = L->top; 106 StkId old_top = L->Cbase = L->top;
108 L->Cstack.num = 0;
109 ar._func = func; 107 ar._func = func;
110 ar.event = "line"; 108 ar.event = "line";
111 ar.currentline = line; 109 ar.currentline = line;
@@ -113,7 +111,7 @@ void luaD_lineHook (lua_State *L, StkId func, int line, lua_Hook linehook) {
113 (*linehook)(L, &ar); 111 (*linehook)(L, &ar);
114 L->allowhooks = 1; 112 L->allowhooks = 1;
115 L->top = old_top; 113 L->top = old_top;
116 L->Cstack = oldCLS; 114 L->Cbase = old_Cbase;
117 } 115 }
118} 116}
119 117
@@ -122,42 +120,36 @@ static void luaD_callHook (lua_State *L, StkId func, lua_Hook callhook,
122 const char *event) { 120 const char *event) {
123 if (L->allowhooks) { 121 if (L->allowhooks) {
124 lua_Debug ar; 122 lua_Debug ar;
125 struct C_Lua_Stack oldCLS = L->Cstack; 123 StkId old_Cbase = L->Cbase;
126 StkId old_top = L->Cstack.lua2C = L->Cstack.base = L->top; 124 StkId old_top = L->Cbase = L->top;
127 L->Cstack.num = 0;
128 ar._func = func; 125 ar._func = func;
129 ar.event = event; 126 ar.event = event;
130 L->allowhooks = 0; /* cannot call hooks inside a hook */ 127 L->allowhooks = 0; /* cannot call hooks inside a hook */
131 callhook(L, &ar); 128 callhook(L, &ar);
132 L->allowhooks = 1; 129 L->allowhooks = 1;
133 L->top = old_top; 130 L->top = old_top;
134 L->Cstack = oldCLS; 131 L->Cbase = old_Cbase;
135 } 132 }
136} 133}
137 134
138 135
139static StkId callCclosure (lua_State *L, const struct Closure *cl, StkId base) { 136static StkId callCclosure (lua_State *L, const struct Closure *cl, StkId base) {
140 int nup = cl->nupvalues; /* number of upvalues */ 137 int nup = cl->nupvalues; /* number of upvalues */
141 int numarg = L->top-base; 138 StkId old_Cbase = L->Cbase;
142 struct C_Lua_Stack oldCLS = L->Cstack; 139 int nres; /* number of results */
143 StkId firstResult;
144 if (nup > 0) { 140 if (nup > 0) {
145 int n = numarg; 141 int n = L->top - base; /* number of arguments */
146 luaD_checkstack(L, nup); 142 luaD_checkstack(L, nup);
147 /* open space for upvalues as extra arguments */ 143 /* open space for upvalues as extra arguments */
148 while (n--) *(base+nup+n) = *(base+n); 144 while (n--) *(base+nup+n) = *(base+n);
149 L->top += nup; 145 L->top += nup;
150 numarg += nup;
151 /* copy upvalues into stack */ 146 /* copy upvalues into stack */
152 while (nup--) *(base+nup) = cl->upvalue[nup]; 147 while (nup--) *(base+nup) = cl->upvalue[nup];
153 } 148 }
154 L->Cstack.num = numarg; 149 L->Cbase = base; /* new base for C function */
155 L->Cstack.lua2C = base; 150 nres = (*cl->f.c)(L); /* do the actual call */
156 L->Cstack.base = L->top; 151 L->Cbase = old_Cbase; /* restore old C base */
157 (*cl->f.c)(L); /* do the actual call */ 152 return L->top - nres; /* return index of first result */
158 firstResult = L->Cstack.base;
159 L->Cstack = oldCLS;
160 return firstResult;
161} 153}
162 154
163 155
@@ -257,42 +249,36 @@ void lua_error (lua_State *L, const char *s) {
257 249
258 250
259static void chain_longjmp (lua_State *L, struct lua_longjmp *lj) { 251static void chain_longjmp (lua_State *L, struct lua_longjmp *lj) {
260 lj->base = L->Cstack.base; 252 lj->status = 0;
261 lj->numCblocks = L->numCblocks; 253 lj->base = L->Cbase;
262 lj->previous = L->errorJmp; 254 lj->previous = L->errorJmp;
263 L->errorJmp = lj; 255 L->errorJmp = lj;
264} 256}
265 257
266 258
267static void restore_longjmp (lua_State *L, struct lua_longjmp *lj) { 259static int restore_longjmp (lua_State *L, struct lua_longjmp *lj) {
268 L->Cstack.num = 0; /* no results */ 260 L->Cbase = lj->base;
269 L->top = L->Cstack.base = L->Cstack.lua2C = lj->base;
270 L->numCblocks = lj->numCblocks;
271 L->errorJmp = lj->previous; 261 L->errorJmp = lj->previous;
262 return lj->status;
272} 263}
273 264
274 265
275/* 266/*
276** Execute a protected call. Assumes that function is at Cstack.base and 267** Execute a protected call.
277** parameters are on top of it.
278*/ 268*/
279int luaD_protectedrun (lua_State *L) { 269int lua_call (lua_State *L, int nargs, int nresults) {
270 StkId func = L->top - (nargs+1); /* function to be called */
280 struct lua_longjmp myErrorJmp; 271 struct lua_longjmp myErrorJmp;
281 chain_longjmp(L, &myErrorJmp); 272 chain_longjmp(L, &myErrorJmp);
273 if (nresults == LUA_MULTRET) nresults = MULT_RET; /* internal code */
282 if (setjmp(myErrorJmp.b) == 0) { 274 if (setjmp(myErrorJmp.b) == 0) {
283 StkId base = L->Cstack.base; 275 luaD_call(L, func, nresults);
284 luaD_call(L, base, MULT_RET);
285 L->Cstack.lua2C = base; /* position of the new results */
286 L->Cstack.num = L->top - base;
287 L->Cstack.base = base + L->Cstack.num; /* incorporate results on stack */
288 L->errorJmp = myErrorJmp.previous;
289 return 0;
290 } 276 }
291 else { /* an error occurred: restore the stack */ 277 else { /* an error occurred: restore the state */
292 restore_longjmp(L, &myErrorJmp); 278 L->top = func; /* remove garbage from the stack */
293 restore_stack_limit(L); 279 restore_stack_limit(L);
294 return myErrorJmp.status;
295 } 280 }
281 return restore_longjmp(L, &myErrorJmp);
296} 282}
297 283
298 284
@@ -302,20 +288,17 @@ int luaD_protectedrun (lua_State *L) {
302static int protectedparser (lua_State *L, ZIO *z, int bin) { 288static int protectedparser (lua_State *L, ZIO *z, int bin) {
303 struct lua_longjmp myErrorJmp; 289 struct lua_longjmp myErrorJmp;
304 chain_longjmp(L, &myErrorJmp); 290 chain_longjmp(L, &myErrorJmp);
305 L->top = L->Cstack.base; /* clear C2Lua */
306 if (setjmp(myErrorJmp.b) == 0) { 291 if (setjmp(myErrorJmp.b) == 0) {
307 Proto *tf = bin ? luaU_undump1(L, z) : luaY_parser(L, z); 292 Proto *tf = bin ? luaU_undump1(L, z) : luaY_parser(L, z);
308 L->errorJmp = myErrorJmp.previous; 293 if (tf == NULL)
309 if (tf == NULL) return -1; /* `natural' end */ 294 myErrorJmp.status = -1; /* `natural' end */
310 luaV_Lclosure(L, tf, 0); 295 luaV_Lclosure(L, tf, 0);
311 return 0;
312 } 296 }
313 else { /* an error occurred */ 297 else { /* an error occurred: correct error code */
314 restore_longjmp(L, &myErrorJmp);
315 if (myErrorJmp.status == LUA_ERRRUN) 298 if (myErrorJmp.status == LUA_ERRRUN)
316 myErrorJmp.status = LUA_ERRSYNTAX; 299 myErrorJmp.status = LUA_ERRSYNTAX;
317 return myErrorJmp.status; /* error code */
318 } 300 }
301 return restore_longjmp(L, &myErrorJmp); /* error code */
319} 302}
320 303
321 304
@@ -331,7 +314,7 @@ static int do_main (lua_State *L, ZIO *z, int bin) {
331 else { 314 else {
332 unsigned long newelems2 = 2*(L->nblocks-old_blocks); 315 unsigned long newelems2 = 2*(L->nblocks-old_blocks);
333 L->GCthreshold += newelems2; 316 L->GCthreshold += newelems2;
334 status = luaD_protectedrun(L); 317 status = lua_call(L, 0, LUA_MULTRET);
335 L->GCthreshold -= newelems2; 318 L->GCthreshold -= newelems2;
336 } 319 }
337 } while (bin && status == 0); 320 } while (bin && status == 0);