lua/ldo.c

495 lines
13 KiB
C
Raw Normal View History

1997-09-17 03:25:59 +08:00
/*
2002-07-10 02:19:19 +08:00
** $Id: ldo.c,v 1.186 2002/07/08 18:21:33 roberto Exp roberto $
1997-09-17 03:25:59 +08:00
** Stack and Call structure of Lua
** See Copyright Notice in lua.h
*/
#include <setjmp.h>
1997-11-07 23:09:49 +08:00
#include <stdlib.h>
1997-09-17 03:25:59 +08:00
#include <string.h>
#include "lua.h"
#include "ldebug.h"
1997-09-17 03:25:59 +08:00
#include "ldo.h"
#include "lfunc.h"
1997-09-17 03:25:59 +08:00
#include "lgc.h"
#include "lmem.h"
#include "lobject.h"
2002-01-10 06:02:47 +08:00
#include "lopcodes.h"
1997-09-17 03:25:59 +08:00
#include "lparser.h"
#include "lstate.h"
#include "lstring.h"
#include "ltable.h"
1997-09-17 03:25:59 +08:00
#include "ltm.h"
#include "lundump.h"
#include "lvm.h"
#include "lzio.h"
2002-01-26 06:14:54 +08:00
/* chain list of long jump buffers */
struct lua_longjmp {
struct lua_longjmp *previous;
CallInfo *ci; /* index of call info of active function that set protection */
StkId top; /* top stack when protection was set */
jmp_buf b;
2002-01-26 06:14:54 +08:00
int allowhooks; /* `allowhook' state when protection was set */
volatile int status; /* error code */
2002-06-18 23:19:27 +08:00
TObject *err; /* error messages (start of `ud') */
2002-01-26 06:14:54 +08:00
};
2002-06-18 23:19:27 +08:00
static void pusherrormsg (lua_State *L, int status, TObject *err) {
setobj(L->top++, &err[0]);
if (status == LUA_ERRRUN)
setobj(L->top++, &err[1]);
}
2002-01-26 06:14:54 +08:00
static void correctstack (lua_State *L, TObject *oldstack) {
struct lua_longjmp *lj;
CallInfo *ci;
UpVal *up;
L->top = (L->top - oldstack) + L->stack;
2002-02-06 06:38:37 +08:00
for (lj = L->errorJmp; lj != NULL; lj = lj->previous)
2002-01-26 06:14:54 +08:00
lj->top = (lj->top - oldstack) + L->stack;
for (up = L->openupval; up != NULL; up = up->next)
up->v = (up->v - oldstack) + L->stack;
for (ci = L->base_ci; ci <= L->ci; ci++) {
ci->base = (ci->base - oldstack) + L->stack;
ci->top = (ci->top - oldstack) + L->stack;
if (ci->pc) { /* entry is of an active Lua function? */
if (ci->pc != (ci-1)->pc)
*ci->pb = (*ci->pb - oldstack) + L->stack;
}
}
}
void luaD_reallocstack (lua_State *L, int newsize) {
TObject *oldstack = L->stack;
luaM_reallocvector(L, L->stack, L->stacksize, newsize, TObject);
L->stacksize = newsize;
L->stack_last = L->stack+(newsize-EXTRA_STACK)-1;
correctstack(L, oldstack);
}
2002-03-27 04:46:10 +08:00
static void correctCI (lua_State *L, CallInfo *oldci) {
struct lua_longjmp *lj;
for (lj = L->errorJmp; lj != NULL; lj = lj->previous) {
lj->ci = (lj->ci - oldci) + L->base_ci;
}
}
void luaD_reallocCI (lua_State *L, int newsize) {
CallInfo *oldci = L->base_ci;
luaM_reallocvector(L, L->base_ci, L->size_ci, newsize, CallInfo);
L->size_ci = newsize;
L->ci = (L->ci - oldci) + L->base_ci;
L->end_ci = L->base_ci + L->size_ci;
correctCI(L, oldci);
}
static void restore_stack_limit (lua_State *L) {
2002-03-27 04:46:10 +08:00
if (L->size_ci > LUA_MAXCALLS) { /* there was an overflow? */
int inuse = (L->ci - L->base_ci);
if (inuse + 1 < LUA_MAXCALLS) /* can `undo' overflow? */
luaD_reallocCI(L, LUA_MAXCALLS);
2002-01-26 06:14:54 +08:00
}
}
2002-01-26 06:14:54 +08:00
void luaD_growstack (lua_State *L, int n) {
2002-03-27 04:46:10 +08:00
if (n <= L->stacksize) /* double size is enough? */
luaD_reallocstack(L, 2*L->stacksize);
else
luaD_reallocstack(L, L->stacksize + n + EXTRA_STACK);
}
static void luaD_growCI (lua_State *L) {
L->ci--;
if (L->size_ci > LUA_MAXCALLS) /* overflow while handling overflow? */
2002-06-18 23:19:27 +08:00
luaD_throw(L, LUA_ERRERR);
2001-06-06 03:41:24 +08:00
else {
2002-03-27 04:46:10 +08:00
luaD_reallocCI(L, 2*L->size_ci);
if (L->size_ci > LUA_MAXCALLS)
2002-05-16 02:57:44 +08:00
luaG_runerror(L, "stack overflow");
1997-09-17 03:25:59 +08:00
}
2002-03-27 04:46:10 +08:00
L->ci++;
1997-09-17 03:25:59 +08:00
}
2002-07-09 02:21:33 +08:00
void luaD_callhook (lua_State *L, lua_Hookevent event, int line) {
lua_Hook hook = L->hook;
if (hook && allowhook(L)) {
ptrdiff_t top = savestack(L, L->top);
ptrdiff_t ci_top = savestack(L, L->ci->top);
2000-03-31 01:19:48 +08:00
lua_Debug ar;
2000-01-19 20:00:45 +08:00
ar.event = event;
2002-07-09 02:21:33 +08:00
ar.currentline = line;
ar.i_ci = L->ci - L->base_ci;
if (event <= LUA_HOOKRET) { /* `call' or `return' event? */
L->ci->pc = NULL; /* function is not active */
L->ci->top = L->ci->base; /* `top' may not have a valid value yet */
}
luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
L->ci->top = L->top + LUA_MINSTACK;
setallowhook(L, 0); /* cannot call hooks inside a hook */
lua_unlock(L);
(*hook)(L, &ar);
lua_lock(L);
lua_assert(!allowhook(L));
setallowhook(L, 1);
L->ci->top = restorestack(L, ci_top);
L->top = restorestack(L, top);
1997-09-17 03:25:59 +08:00
}
}
2002-01-26 06:14:54 +08:00
static void adjust_varargs (lua_State *L, int nfixargs) {
2002-01-10 06:02:47 +08:00
int i;
Table *htab;
2002-05-28 04:35:40 +08:00
TObject nname;
2002-01-26 06:14:54 +08:00
int actual = L->top - L->ci->base; /* actual number of arguments */
if (actual < nfixargs) {
luaD_checkstack(L, nfixargs - actual);
for (; actual < nfixargs; ++actual)
2002-01-10 06:02:47 +08:00
setnilvalue(L->top++);
}
2002-01-26 06:14:54 +08:00
actual -= nfixargs; /* number of extra arguments */
htab = luaH_new(L, 0, 0); /* create `arg' table */
for (i=0; i<actual; i++) /* put extra arguments into `arg' table */
2002-05-28 04:35:40 +08:00
setobj(luaH_setnum(L, htab, i+1), L->top - actual + i);
2002-01-10 06:02:47 +08:00
/* store counter in field `n' */
setsvalue(&nname, luaS_newliteral(L, "n"));
2002-05-28 04:35:40 +08:00
setnvalue(luaH_set(L, htab, &nname), actual);
2002-01-26 06:14:54 +08:00
L->top -= actual; /* remove extra elements from the stack */
2002-01-10 06:02:47 +08:00
sethvalue(L->top, htab);
2002-01-26 06:14:54 +08:00
incr_top(L);
2002-01-10 06:02:47 +08:00
}
static StkId tryfuncTM (lua_State *L, StkId func) {
const TObject *tm = luaT_gettmbyobj(L, func, TM_CALL);
2002-07-10 02:19:19 +08:00
StkId p;
if (ttype(tm) != LUA_TFUNCTION) {
L->ci--; /* undo increment (no function here) */
luaG_typeerror(L, func, "call");
}
2002-07-10 02:19:19 +08:00
/* Open a hole inside the stack at `func' */
for (p = L->top; p > func; p--) setobj(p, p-1);
incr_top(L);
func = L->ci->base - 1; /* previous call may change stack */
setobj(func, tm); /* tag method is the new function to be called */
return func;
}
2001-12-20 23:13:38 +08:00
StkId luaD_precall (lua_State *L, StkId func) {
CallInfo *ci;
2002-01-10 06:02:47 +08:00
LClosure *cl;
2002-03-27 04:46:10 +08:00
if (++L->ci == L->end_ci) luaD_growCI(L);
2002-01-26 06:14:54 +08:00
ci = L->ci;
2002-04-11 02:05:08 +08:00
ci->base = func+1;
2002-01-26 06:14:54 +08:00
ci->pc = NULL;
if (ttype(func) != LUA_TFUNCTION) /* `func' is not a function? */
func = tryfuncTM(L, func); /* check the `function' tag method */
2002-01-10 06:02:47 +08:00
cl = &clvalue(func)->l;
2002-07-09 02:21:33 +08:00
if (L->hookmask & LUA_MASKCALL) {
luaD_callhook(L, LUA_HOOKCALL, -1);
2002-01-26 06:14:54 +08:00
ci = L->ci; /* previous call may realocate `ci' */
}
2002-01-10 06:02:47 +08:00
if (!cl->isC) { /* Lua function? prepare its call */
Proto *p = cl->p;
ci->savedpc = p->code; /* starting point */
if (p->is_vararg) /* varargs? */
2002-01-26 06:14:54 +08:00
adjust_varargs(L, p->numparams);
2002-03-20 20:52:32 +08:00
luaD_checkstack(L, p->maxstacksize);
2002-01-26 06:14:54 +08:00
ci->top = ci->base + p->maxstacksize;
2002-01-10 06:02:47 +08:00
while (L->top < ci->top)
setnilvalue(L->top++);
L->top = ci->top;
return NULL;
}
else { /* if is a C function, call it */
int n;
luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
ci->top = L->top + LUA_MINSTACK;
2002-01-10 06:02:47 +08:00
lua_unlock(L);
#if LUA_COMPATUPVALUES
2002-01-10 06:02:47 +08:00
lua_pushupvalues(L);
#endif
2002-01-26 06:14:54 +08:00
n = (*clvalue(ci->base-1)->c.f)(L); /* do the actual call */
2002-01-10 06:02:47 +08:00
lua_lock(L);
return L->top - n;
}
2001-12-20 23:13:38 +08:00
}
void luaD_poscall (lua_State *L, int wanted, StkId firstResult) {
StkId res;
2002-07-09 02:21:33 +08:00
if (L->hookmask & LUA_MASKRET) {
2002-03-20 20:52:32 +08:00
ptrdiff_t fr = savestack(L, firstResult); /* next call may change stack */
2002-07-09 02:21:33 +08:00
luaD_callhook(L, LUA_HOOKRET, -1);
2002-03-20 20:52:32 +08:00
firstResult = restorestack(L, fr);
2002-01-26 06:14:54 +08:00
}
res = L->ci->base - 1; /* res == final position of 1st result */
2001-12-20 23:13:38 +08:00
L->ci--;
/* move results to correct place */
while (wanted != 0 && firstResult < L->top) {
setobj(res++, firstResult++);
wanted--;
}
while (wanted-- > 0)
setnilvalue(res++);
L->top = res;
luaC_checkGC(L);
1997-09-17 03:25:59 +08:00
}
/*
** Call a function (C or Lua). The function to be called is at *func.
** The arguments are on the stack, right after the function.
** When returns, all the results are on the stack, starting at the original
** function position.
*/
2001-12-20 23:13:38 +08:00
void luaD_call (lua_State *L, StkId func, int nResults) {
StkId firstResult = luaD_precall(L, func);
2002-01-10 06:02:47 +08:00
if (firstResult == NULL) { /* is a Lua function? */
firstResult = luaV_execute(L); /* call it */
if (firstResult == NULL) {
luaD_poscall(L, 0, L->top);
2002-06-27 00:37:23 +08:00
luaG_runerror(L, "attempt to yield across tag-method/C-call boundary");
2002-01-10 06:02:47 +08:00
}
}
2001-12-20 23:13:38 +08:00
luaD_poscall(L, nResults, firstResult);
1998-07-13 00:14:34 +08:00
}
1997-09-17 03:25:59 +08:00
2002-01-10 06:02:47 +08:00
LUA_API void lua_cobegin (lua_State *L, int nargs) {
lua_lock(L);
luaD_precall(L, L->top - (nargs+1));
2002-01-10 06:02:47 +08:00
lua_unlock(L);
}
2002-02-06 06:38:37 +08:00
static void move_results (lua_State *L, TObject *from, TObject *to) {
while (from < to) {
setobj(L->top, from);
from++;
2002-01-26 06:14:54 +08:00
incr_top(L);
}
}
2002-05-02 04:48:12 +08:00
struct ResS {
2002-06-18 23:19:27 +08:00
TObject err[2];
2002-05-02 04:48:12 +08:00
int numres;
};
static void resume (lua_State *L, void *ud) {
2002-01-10 06:02:47 +08:00
StkId firstResult;
2002-02-06 06:38:37 +08:00
CallInfo *ci = L->ci;
if (ci->savedpc != ci_func(ci)->l.p->code) { /* not first time? */
/* finish interupted execution of `OP_CALL' */
int nresults;
lua_assert(GET_OPCODE(*((ci-1)->savedpc - 1)) == OP_CALL);
nresults = GETARG_C(*((ci-1)->savedpc - 1)) - 1;
luaD_poscall(L, nresults, L->top); /* complete it */
if (nresults >= 0) L->top = L->ci->top;
}
2002-02-06 06:38:37 +08:00
firstResult = luaV_execute(L);
if (firstResult == NULL) /* yield? */
2002-05-02 04:48:12 +08:00
cast(struct ResS *, ud)->numres = L->ci->yield_results;
2002-02-06 06:38:37 +08:00
else { /* return */
2002-05-02 04:48:12 +08:00
cast(struct ResS *, ud)->numres = L->top - firstResult;
2002-02-06 06:38:37 +08:00
luaD_poscall(L, LUA_MULTRET, firstResult); /* finalize this coroutine */
2002-01-10 06:02:47 +08:00
}
2002-02-06 06:38:37 +08:00
}
LUA_API int lua_resume (lua_State *L, lua_State *co) {
CallInfo *ci;
2002-05-02 04:48:12 +08:00
struct ResS ud;
2002-02-06 06:38:37 +08:00
int status;
lua_lock(L);
ci = co->ci;
if (ci == co->base_ci) /* no activation record? ?? */
2002-07-04 20:29:32 +08:00
luaG_runerror(L, "cannot resume dead thread");
2002-02-06 06:38:37 +08:00
if (co->errorJmp != NULL) /* ?? */
2002-07-04 20:29:32 +08:00
luaG_runerror(L, "cannot resume active thread");
2002-06-18 23:19:27 +08:00
status = luaD_runprotected(co, resume, ud.err);
2002-02-06 06:38:37 +08:00
if (status == 0)
2002-05-02 04:48:12 +08:00
move_results(L, co->top - ud.numres, co->top);
2002-06-18 23:19:27 +08:00
else
pusherrormsg(L, status, ud.err);
2002-01-10 06:02:47 +08:00
lua_unlock(L);
2002-02-06 06:38:37 +08:00
return status;
2002-01-10 06:02:47 +08:00
}
LUA_API int lua_yield (lua_State *L, int nresults) {
CallInfo *ci;
lua_lock(L);
ci = L->ci;
if (ci_func(ci-1)->c.isC)
2002-06-27 00:37:23 +08:00
luaG_runerror(L, "cannot yield a C function");
2002-01-26 06:14:54 +08:00
ci->yield_results = nresults;
2002-01-10 06:02:47 +08:00
lua_unlock(L);
2002-01-26 06:14:54 +08:00
return -1;
2002-01-10 06:02:47 +08:00
}
1997-09-17 03:25:59 +08:00
/*
2000-09-26 00:22:42 +08:00
** Execute a protected call.
1997-09-17 03:25:59 +08:00
*/
2000-09-26 00:22:42 +08:00
struct CallS { /* data to `f_call' */
2002-06-18 23:19:27 +08:00
TObject err[2];
2000-09-26 00:22:42 +08:00
StkId func;
int nresults;
};
2000-09-26 00:22:42 +08:00
static void f_call (lua_State *L, void *ud) {
2001-09-01 03:46:07 +08:00
struct CallS *c = cast(struct CallS *, ud);
2001-12-20 23:13:38 +08:00
luaD_call(L, c->func, c->nresults);
}
2002-06-18 23:19:27 +08:00
int luaD_pcall (lua_State *L, int nargs, int nresults) {
2000-09-26 00:22:42 +08:00
struct CallS c;
int status;
2002-01-26 06:14:54 +08:00
c.func = L->top - (nargs+1); /* function to be called */
c.nresults = nresults;
2002-06-18 23:19:27 +08:00
status = luaD_runprotected(L, &f_call, c.err);
if (status != 0) { /* an error occurred? */
2002-01-26 06:14:54 +08:00
L->top -= nargs+1; /* remove parameters and func from the stack */
luaF_close(L, L->top); /* close eventual pending closures */
2002-06-18 23:19:27 +08:00
pusherrormsg(L, status, c.err);
}
2000-09-26 00:22:42 +08:00
return status;
1997-09-17 03:25:59 +08:00
}
/*
2000-09-26 00:22:42 +08:00
** Execute a protected parser.
1997-09-17 03:25:59 +08:00
*/
struct SParser { /* data to `f_parser' */
2002-06-18 23:19:27 +08:00
TObject err[2];
2000-09-26 00:22:42 +08:00
ZIO *z;
int bin;
};
static void f_parser (lua_State *L, void *ud) {
2001-09-01 03:46:07 +08:00
struct SParser *p = cast(struct SParser *, ud);
2000-09-26 00:22:42 +08:00
Proto *tf = p->bin ? luaU_undump(L, p->z) : luaY_parser(L, p->z);
Closure *cl = luaF_newLclosure(L, 0, gt(L));
2001-10-03 00:45:03 +08:00
cl->l.p = tf;
setclvalue(L->top, cl);
2002-01-26 06:14:54 +08:00
incr_top(L);
1997-09-17 03:25:59 +08:00
}
int luaD_protectedparser (lua_State *L, ZIO *z, int bin) {
struct SParser p;
2001-02-21 02:15:33 +08:00
lu_mem old_blocks;
2000-09-26 00:22:42 +08:00
int status;
p.z = z; p.bin = bin;
/* before parsing, give a (good) chance to GC */
if (G(L)->nblocks + G(L)->nblocks/4 >= G(L)->GCthreshold)
luaC_collectgarbage(L);
old_blocks = G(L)->nblocks;
2002-06-18 23:19:27 +08:00
status = luaD_runprotected(L, f_parser, p.err);
if (status == 0) {
2000-10-05 20:14:08 +08:00
/* add new memory to threshold (as it probably will stay) */
lua_assert(G(L)->nblocks >= old_blocks);
G(L)->GCthreshold += (G(L)->nblocks - old_blocks);
}
2002-06-04 04:12:50 +08:00
else
2002-06-18 23:19:27 +08:00
pusherrormsg(L, status, p.err);
2000-09-26 00:22:42 +08:00
return status;
1997-09-17 03:25:59 +08:00
}
2000-09-26 00:22:42 +08:00
/*
** {======================================================
2001-11-28 04:56:47 +08:00
** Error-recovery functions (based on long jumps)
2000-09-26 00:22:42 +08:00
** =======================================================
*/
2002-06-18 23:19:27 +08:00
static void seterrorobj (lua_State *L, int errcode, TObject *m) {
switch (errcode) {
case LUA_ERRMEM: {
if (G(L) != NULL && G(L)->GCthreshold > 0) /* state is OK? */
setsvalue(&m[0], luaS_new(L, MEMERRMSG));
break;
}
case LUA_ERRERR: {
setsvalue(&m[0], luaS_new(L, "error in error handling"));
break;
}
case LUA_ERRSYNTAX: { /* message is on stack top */
setobj(&m[0], L->top - 1);
break;
}
case LUA_ERRRUN: { /* traceback is on stack top */
setobj(&m[0], L->top - 2);
setobj(&m[1], L->top - 1);
break;
}
2000-09-26 00:22:42 +08:00
}
}
2002-06-18 23:19:27 +08:00
void luaD_throw (lua_State *L, int errcode) {
2000-09-26 00:22:42 +08:00
if (L->errorJmp) {
seterrorobj(L, errcode, L->errorJmp->err);
2000-09-26 00:22:42 +08:00
L->errorJmp->status = errcode;
longjmp(L->errorJmp->b, 1);
}
else {
2002-04-17 01:08:28 +08:00
G(L)->panic(L);
2000-09-26 00:22:42 +08:00
exit(EXIT_FAILURE);
}
}
2002-05-02 04:48:12 +08:00
int luaD_runprotected (lua_State *L, Pfunc f, TObject *ud) {
2000-09-26 00:22:42 +08:00
struct lua_longjmp lj;
2002-01-26 06:14:54 +08:00
lj.ci = L->ci;
lj.top = L->top;
2002-07-09 02:21:33 +08:00
lj.allowhooks = allowhook(L);
2000-09-26 00:22:42 +08:00
lj.status = 0;
2002-05-02 04:48:12 +08:00
lj.err = ud;
2000-09-26 00:22:42 +08:00
lj.previous = L->errorJmp; /* chain new error handler */
L->errorJmp = &lj;
if (setjmp(lj.b) == 0)
(*f)(L, ud);
2002-05-02 04:48:12 +08:00
else { /* an error occurred */
L->ci = lj.ci; /* restore the state */
L->top = lj.top;
2002-07-09 02:21:33 +08:00
setallowhook(L, lj.allowhooks);
2000-09-26 00:22:42 +08:00
restore_stack_limit(L);
}
L->errorJmp = lj.previous; /* restore old error handler */
return lj.status;
}
int luaD_isprotected (lua_State *L, CallInfo *ci) {
struct lua_longjmp *l;
for (l = L->errorJmp; l; l = l->previous)
if (l->ci+1 == ci) return 1;
return 0;
}
2000-09-26 00:22:42 +08:00
/* }====================================================== */