diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-08-06 12:32:22 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-08-06 12:32:22 -0300 |
commit | a2fa48a570b01b2a2cd37f01799f08f693fc5892 (patch) | |
tree | b41fc7e88f7f941ae1589921f8d7c0ff57146f4d /ldo.c | |
parent | 8b2b8790b5c419282f4fa0c7faa168379647b3b9 (diff) | |
download | lua-a2fa48a570b01b2a2cd37f01799f08f693fc5892.tar.gz lua-a2fa48a570b01b2a2cd37f01799f08f693fc5892.tar.bz2 lua-a2fa48a570b01b2a2cd37f01799f08f693fc5892.zip |
new (old?) error handling scheme
Diffstat (limited to 'ldo.c')
-rw-r--r-- | ldo.c | 63 |
1 files changed, 17 insertions, 46 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ldo.c,v 1.188 2002/07/16 14:26:56 roberto Exp roberto $ | 2 | ** $Id: ldo.c,v 1.189 2002/08/05 17:36:24 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 | */ |
@@ -64,9 +64,6 @@ static void seterrorobj (lua_State *L, int errcode) { | |||
64 | 64 | ||
65 | 65 | ||
66 | void luaD_throw (lua_State *L, int errcode) { | 66 | void luaD_throw (lua_State *L, int errcode) { |
67 | if (errcode == LUA_ERRRUN) | ||
68 | luaD_checkstack(L, LUA_MINSTACK); /* ensure stack space to handle error */ | ||
69 | luaG_saveallpcs(L); /* C stack will disapear */ | ||
70 | if (L->errorJmp) { | 67 | if (L->errorJmp) { |
71 | L->errorJmp->status = errcode; | 68 | L->errorJmp->status = errcode; |
72 | longjmp(L->errorJmp->b, 1); | 69 | longjmp(L->errorJmp->b, 1); |
@@ -99,30 +96,6 @@ static void restore_stack_limit (lua_State *L) { | |||
99 | } | 96 | } |
100 | } | 97 | } |
101 | 98 | ||
102 | |||
103 | void luaD_resetprotection (lua_State *L) { | ||
104 | Protection *p; | ||
105 | StkId err = L->top - 1; /* error msg. position (if there is one) */ | ||
106 | lua_assert(L->number_toreset > 0); | ||
107 | p = &L->toreset[--L->number_toreset]; | ||
108 | L->ci = restoreci(L, p->ci); | ||
109 | L->top = restorestack(L, p->top); | ||
110 | L->ci->top = L->top + LUA_MINSTACK; | ||
111 | setallowhook(L, p->allowhooks); | ||
112 | restore_stack_limit(L); | ||
113 | setobj(L->top++, err); /* copy error message to corrected top */ | ||
114 | } | ||
115 | |||
116 | |||
117 | /* | ||
118 | ** invalidate all pc pointers from stack part that becomes inactive | ||
119 | */ | ||
120 | static void deactivateinfo (lua_State *L, CallInfo *p_ci) { | ||
121 | CallInfo *ci; | ||
122 | for (ci = L->ci; ci > p_ci; ci--) | ||
123 | ci->pc = NULL; | ||
124 | } | ||
125 | |||
126 | /* }====================================================== */ | 99 | /* }====================================================== */ |
127 | 100 | ||
128 | 101 | ||
@@ -168,7 +141,6 @@ void luaD_growstack (lua_State *L, int n) { | |||
168 | 141 | ||
169 | 142 | ||
170 | static void luaD_growCI (lua_State *L) { | 143 | static void luaD_growCI (lua_State *L) { |
171 | L->ci--; | ||
172 | if (L->size_ci > LUA_MAXCALLS) /* overflow while handling overflow? */ | 144 | if (L->size_ci > LUA_MAXCALLS) /* overflow while handling overflow? */ |
173 | luaD_throw(L, LUA_ERRERR); | 145 | luaD_throw(L, LUA_ERRERR); |
174 | else { | 146 | else { |
@@ -176,7 +148,6 @@ static void luaD_growCI (lua_State *L) { | |||
176 | if (L->size_ci > LUA_MAXCALLS) | 148 | if (L->size_ci > LUA_MAXCALLS) |
177 | luaG_runerror(L, "stack overflow"); | 149 | luaG_runerror(L, "stack overflow"); |
178 | } | 150 | } |
179 | L->ci++; | ||
180 | } | 151 | } |
181 | 152 | ||
182 | 153 | ||
@@ -412,34 +383,34 @@ struct CallS { /* data to `f_call' */ | |||
412 | 383 | ||
413 | static void f_call (lua_State *L, void *ud) { | 384 | static void f_call (lua_State *L, void *ud) { |
414 | struct CallS *c = cast(struct CallS *, ud); | 385 | struct CallS *c = cast(struct CallS *, ud); |
415 | luaM_growvector(L, L->toreset, L->number_toreset, L->size_toreset, | ||
416 | Protection, MAX_INT, ""); | ||
417 | luaD_call(L, c->func, c->nresults); | 386 | luaD_call(L, c->func, c->nresults); |
418 | } | 387 | } |
419 | 388 | ||
420 | 389 | ||
421 | int luaD_pcall (lua_State *L, int nargs, int nresults) { | 390 | int luaD_pcall (lua_State *L, int nargs, int nresults, ptrdiff_t errfunc) { |
422 | struct CallS c; | 391 | struct CallS c; |
423 | int status; | 392 | int status; |
424 | int protectionlevel = L->number_toreset; | 393 | ptrdiff_t old_top = savestack(L, L->top); |
425 | Protection protection; | 394 | ptrdiff_t old_ci = saveci(L, L->ci); |
426 | protection.top = savestack(L, L->top); | 395 | int old_allowhooks = allowhook(L); |
427 | protection.ci = saveci(L, L->ci); | 396 | ptrdiff_t old_errfunc = L->errfunc; |
428 | protection.allowhooks = allowhook(L); | 397 | L->errfunc = errfunc; |
429 | c.func = L->top - (nargs+1); /* function to be called */ | 398 | c.func = L->top - (nargs+1); /* function to be called */ |
430 | c.nresults = nresults; | 399 | c.nresults = nresults; |
431 | status = luaD_rawrunprotected(L, &f_call, &c); | 400 | status = luaD_rawrunprotected(L, &f_call, &c); |
432 | if (status != 0) { /* an error occurred? */ | 401 | if (status != 0) { /* an error occurred? */ |
433 | /* remove parameters and func from the stack */ | 402 | StkId err; /* error msg. position */ |
434 | protection.top = savestack(L, restorestack(L, protection.top) - (nargs+1)); | ||
435 | /* close eventual pending closures */ | ||
436 | luaF_close(L, restorestack(L, protection.top)); | ||
437 | L->ci->top = L->top + LUA_MINSTACK; /* extra space to handle error */ | ||
438 | seterrorobj(L, status); | 403 | seterrorobj(L, status); |
439 | deactivateinfo(L, restoreci(L, protection.ci)); | 404 | err = L->top - 1; |
440 | L->number_toreset = protectionlevel + 1; | 405 | /* remove parameters and func from the stack */ |
441 | L->toreset[L->number_toreset - 1] = protection; | 406 | L->top = restorestack(L, old_top) - (nargs+1); |
407 | setobj(L->top++, err); /* copy error message to corrected top */ | ||
408 | luaF_close(L, L->top); /* close eventual pending closures */ | ||
409 | L->ci = restoreci(L, old_ci); | ||
410 | setallowhook(L, old_allowhooks); | ||
411 | restore_stack_limit(L); | ||
442 | } | 412 | } |
413 | L->errfunc = old_errfunc; | ||
443 | return status; | 414 | return status; |
444 | } | 415 | } |
445 | 416 | ||