1999-12-15 02:31:20 +08:00
|
|
|
/*
|
2001-03-07 21:22:55 +08:00
|
|
|
** $Id: ldebug.c,v 1.72 2001/03/06 14:46:54 roberto Exp roberto $
|
1999-12-15 02:31:20 +08:00
|
|
|
** Debug Interface
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2000-02-18 02:30:36 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2000-06-12 21:52:05 +08:00
|
|
|
#include "lua.h"
|
|
|
|
|
1999-12-15 02:31:20 +08:00
|
|
|
#include "lapi.h"
|
2000-06-29 04:21:06 +08:00
|
|
|
#include "lcode.h"
|
1999-12-30 00:31:15 +08:00
|
|
|
#include "ldebug.h"
|
2000-01-19 20:00:45 +08:00
|
|
|
#include "ldo.h"
|
1999-12-15 02:31:20 +08:00
|
|
|
#include "lfunc.h"
|
|
|
|
#include "lobject.h"
|
2000-06-29 04:21:06 +08:00
|
|
|
#include "lopcodes.h"
|
1999-12-15 02:31:20 +08:00
|
|
|
#include "lstate.h"
|
2000-09-12 04:29:27 +08:00
|
|
|
#include "lstring.h"
|
1999-12-15 02:31:20 +08:00
|
|
|
#include "ltable.h"
|
|
|
|
#include "ltm.h"
|
|
|
|
#include "luadebug.h"
|
|
|
|
|
|
|
|
|
2000-10-05 20:14:08 +08:00
|
|
|
|
2001-02-24 01:17:25 +08:00
|
|
|
static const l_char *getfuncname (lua_State *L, StkId f, const l_char **name);
|
2000-08-12 00:17:28 +08:00
|
|
|
|
2000-01-19 20:00:45 +08:00
|
|
|
|
|
|
|
static void setnormalized (TObject *d, const TObject *s) {
|
2000-10-05 20:14:08 +08:00
|
|
|
if (ttype(s) == LUA_TMARK) {
|
2001-01-18 23:59:09 +08:00
|
|
|
setclvalue(d, infovalue(s)->func);
|
2000-06-27 03:28:31 +08:00
|
|
|
}
|
2001-01-18 23:59:09 +08:00
|
|
|
else setobj(d, s);
|
2000-10-05 20:14:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int isLmark (StkId o) {
|
|
|
|
return (o && ttype(o) == LUA_TMARK && !infovalue(o)->func->isC);
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API lua_Hook lua_setcallhook (lua_State *L, lua_Hook func) {
|
2001-01-24 23:45:33 +08:00
|
|
|
lua_Hook oldhook;
|
2001-03-03 01:27:50 +08:00
|
|
|
lua_lock(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
oldhook = L->callhook;
|
1999-12-15 02:31:20 +08:00
|
|
|
L->callhook = func;
|
2001-03-03 01:27:50 +08:00
|
|
|
lua_unlock(L);
|
2000-01-19 20:00:45 +08:00
|
|
|
return oldhook;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API lua_Hook lua_setlinehook (lua_State *L, lua_Hook func) {
|
2001-01-24 23:45:33 +08:00
|
|
|
lua_Hook oldhook;
|
2001-03-03 01:27:50 +08:00
|
|
|
lua_lock(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
oldhook = L->linehook;
|
2000-01-19 20:00:45 +08:00
|
|
|
L->linehook = func;
|
2001-03-03 01:27:50 +08:00
|
|
|
lua_unlock(L);
|
2000-01-19 20:00:45 +08:00
|
|
|
return oldhook;
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-01-19 20:00:45 +08:00
|
|
|
static StkId aux_stackedfunction (lua_State *L, int level, StkId top) {
|
1999-12-15 02:31:20 +08:00
|
|
|
int i;
|
2000-08-11 03:50:47 +08:00
|
|
|
for (i = (top-1) - L->stack; i>=0; i--) {
|
2001-01-18 23:59:09 +08:00
|
|
|
if (is_T_MARK(&L->stack[i])) {
|
1999-12-24 02:19:57 +08:00
|
|
|
if (level == 0)
|
1999-12-15 02:31:20 +08:00
|
|
|
return L->stack+i;
|
1999-12-24 02:19:57 +08:00
|
|
|
level--;
|
|
|
|
}
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
2000-01-19 20:00:45 +08:00
|
|
|
return NULL;
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
|
2001-01-24 23:45:33 +08:00
|
|
|
StkId f;
|
|
|
|
int status;
|
2001-03-03 01:27:50 +08:00
|
|
|
lua_lock(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
f = aux_stackedfunction(L, level, L->top);
|
|
|
|
if (f == NULL) status = 0; /* there is no such level */
|
2000-01-19 20:00:45 +08:00
|
|
|
else {
|
|
|
|
ar->_func = f;
|
2001-01-24 23:45:33 +08:00
|
|
|
status = 1;
|
2000-01-19 20:00:45 +08:00
|
|
|
}
|
2001-03-03 01:27:50 +08:00
|
|
|
lua_unlock(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
return status;
|
1999-12-31 02:28:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
static int nups (StkId f) {
|
2000-01-19 20:00:45 +08:00
|
|
|
switch (ttype(f)) {
|
2000-10-05 20:14:08 +08:00
|
|
|
case LUA_TFUNCTION:
|
2000-06-09 02:27:13 +08:00
|
|
|
return clvalue(f)->nupvalues;
|
2000-10-05 20:14:08 +08:00
|
|
|
case LUA_TMARK:
|
2000-06-27 03:28:31 +08:00
|
|
|
return infovalue(f)->func->nupvalues;
|
1999-12-24 02:19:57 +08:00
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-09 02:26:05 +08:00
|
|
|
int luaG_getline (int *lineinfo, int pc, int refline, int *prefi) {
|
2000-08-31 21:29:12 +08:00
|
|
|
int refi;
|
2000-10-06 20:45:25 +08:00
|
|
|
if (lineinfo == NULL || pc == -1)
|
|
|
|
return -1; /* no line info or function is not active */
|
2000-08-31 21:29:12 +08:00
|
|
|
refi = prefi ? *prefi : 0;
|
2000-08-09 02:26:05 +08:00
|
|
|
if (lineinfo[refi] < 0)
|
2001-02-10 02:37:33 +08:00
|
|
|
refline += -lineinfo[refi++];
|
2001-01-19 21:20:30 +08:00
|
|
|
lua_assert(lineinfo[refi] >= 0);
|
2000-08-09 02:26:05 +08:00
|
|
|
while (lineinfo[refi] > pc) {
|
|
|
|
refline--;
|
|
|
|
refi--;
|
|
|
|
if (lineinfo[refi] < 0)
|
2001-02-10 02:37:33 +08:00
|
|
|
refline -= -lineinfo[refi--];
|
2001-01-19 21:20:30 +08:00
|
|
|
lua_assert(lineinfo[refi] >= 0);
|
2000-08-09 02:26:05 +08:00
|
|
|
}
|
|
|
|
for (;;) {
|
|
|
|
int nextline = refline + 1;
|
|
|
|
int nextref = refi + 1;
|
|
|
|
if (lineinfo[nextref] < 0)
|
2001-02-10 02:37:33 +08:00
|
|
|
nextline += -lineinfo[nextref++];
|
2001-01-19 21:20:30 +08:00
|
|
|
lua_assert(lineinfo[nextref] >= 0);
|
2000-08-09 02:26:05 +08:00
|
|
|
if (lineinfo[nextref] > pc)
|
|
|
|
break;
|
|
|
|
refline = nextline;
|
|
|
|
refi = nextref;
|
|
|
|
}
|
|
|
|
if (prefi) *prefi = refi;
|
|
|
|
return refline;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
static int currentpc (StkId f) {
|
2000-06-29 04:21:06 +08:00
|
|
|
CallInfo *ci = infovalue(f);
|
2001-01-19 21:20:30 +08:00
|
|
|
lua_assert(isLmark(f));
|
2000-10-06 20:45:25 +08:00
|
|
|
if (ci->pc)
|
|
|
|
return (*ci->pc - ci->func->f.l->code) - 1;
|
|
|
|
else
|
|
|
|
return -1; /* function is not active */
|
2000-06-29 04:21:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
static int currentline (StkId f) {
|
2000-10-05 20:14:08 +08:00
|
|
|
if (!isLmark(f))
|
2000-06-27 03:28:31 +08:00
|
|
|
return -1; /* only active lua functions have current-line information */
|
|
|
|
else {
|
|
|
|
CallInfo *ci = infovalue(f);
|
2000-08-09 02:26:05 +08:00
|
|
|
int *lineinfo = ci->func->f.l->lineinfo;
|
2000-10-21 00:39:03 +08:00
|
|
|
return luaG_getline(lineinfo, currentpc(f), 1, NULL);
|
2000-06-27 03:28:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-12-15 02:31:20 +08:00
|
|
|
|
2000-03-11 02:37:44 +08:00
|
|
|
static Proto *getluaproto (StkId f) {
|
2000-10-05 20:14:08 +08:00
|
|
|
return (isLmark(f) ? infovalue(f)->func->f.l : NULL);
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-24 01:17:25 +08:00
|
|
|
LUA_API const l_char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
|
|
|
|
const l_char *name;
|
2001-01-24 23:45:33 +08:00
|
|
|
StkId f;
|
|
|
|
Proto *fp;
|
2001-03-03 01:27:50 +08:00
|
|
|
lua_lock(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
name = NULL;
|
|
|
|
f = ar->_func;
|
|
|
|
fp = getluaproto(f);
|
|
|
|
if (fp) { /* `f' is a Lua function? */
|
|
|
|
name = luaF_getlocalname(fp, n, currentpc(f));
|
|
|
|
if (name)
|
|
|
|
luaA_pushobject(L, (f+1)+(n-1)); /* push value */
|
|
|
|
}
|
2001-03-03 01:27:50 +08:00
|
|
|
lua_unlock(L);
|
2000-08-29 01:57:04 +08:00
|
|
|
return name;
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-24 01:17:25 +08:00
|
|
|
LUA_API const l_char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
|
|
|
|
const l_char *name;
|
2001-01-24 23:45:33 +08:00
|
|
|
StkId f;
|
|
|
|
Proto *fp;
|
2001-03-03 01:27:50 +08:00
|
|
|
lua_lock(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
name = NULL;
|
|
|
|
f = ar->_func;
|
|
|
|
fp = getluaproto(f);
|
2000-08-31 21:29:12 +08:00
|
|
|
L->top--; /* pop new value */
|
2001-01-24 23:45:33 +08:00
|
|
|
if (fp) { /* `f' is a Lua function? */
|
|
|
|
name = luaF_getlocalname(fp, n, currentpc(f));
|
2001-02-24 01:17:25 +08:00
|
|
|
if (!name || name[0] == l_c('(')) /* `(' starts private locals */
|
2001-01-24 23:45:33 +08:00
|
|
|
name = NULL;
|
|
|
|
else
|
|
|
|
setobj((f+1)+(n-1), L->top);
|
|
|
|
}
|
2001-03-03 01:27:50 +08:00
|
|
|
lua_unlock(L);
|
2000-08-29 01:57:04 +08:00
|
|
|
return name;
|
2000-01-19 20:00:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-09-12 04:29:27 +08:00
|
|
|
static void infoLproto (lua_Debug *ar, Proto *f) {
|
2001-02-10 04:22:29 +08:00
|
|
|
ar->source = getstr(f->source);
|
2000-09-12 04:29:27 +08:00
|
|
|
ar->linedefined = f->lineDefined;
|
2001-02-24 01:17:25 +08:00
|
|
|
ar->what = l_s("Lua");
|
2000-09-12 04:29:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
static void funcinfo (lua_State *L, lua_Debug *ar, StkId func) {
|
2000-10-05 20:14:08 +08:00
|
|
|
Closure *cl = NULL;
|
2000-01-19 20:00:45 +08:00
|
|
|
switch (ttype(func)) {
|
2000-10-05 20:14:08 +08:00
|
|
|
case LUA_TFUNCTION:
|
|
|
|
cl = clvalue(func);
|
2000-06-27 03:28:31 +08:00
|
|
|
break;
|
2000-10-05 20:14:08 +08:00
|
|
|
case LUA_TMARK:
|
|
|
|
cl = infovalue(func)->func;
|
2000-03-30 04:19:20 +08:00
|
|
|
break;
|
|
|
|
default:
|
2001-02-24 01:17:25 +08:00
|
|
|
luaD_error(L, l_s("value for `lua_getinfo' is not a function"));
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
2000-10-05 20:14:08 +08:00
|
|
|
if (cl->isC) {
|
2001-02-24 01:17:25 +08:00
|
|
|
ar->source = l_s("=C");
|
2000-10-05 20:14:08 +08:00
|
|
|
ar->linedefined = -1;
|
2001-02-24 01:17:25 +08:00
|
|
|
ar->what = l_s("C");
|
2000-10-05 20:14:08 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
infoLproto(ar, cl->f.l);
|
2001-03-06 22:46:54 +08:00
|
|
|
luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
|
2000-01-19 20:00:45 +08:00
|
|
|
if (ar->linedefined == 0)
|
2001-02-24 01:17:25 +08:00
|
|
|
ar->what = l_s("main");
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-24 01:17:25 +08:00
|
|
|
static const l_char *travtagmethods (global_State *G, const TObject *o) {
|
2000-10-05 21:00:17 +08:00
|
|
|
if (ttype(o) == LUA_TFUNCTION) {
|
|
|
|
int e;
|
|
|
|
for (e=0; e<TM_N; e++) {
|
|
|
|
int t;
|
2001-01-19 21:20:30 +08:00
|
|
|
for (t=0; t<G->ntag; t++)
|
|
|
|
if (clvalue(o) == luaT_gettm(G, t, e))
|
2000-10-05 21:00:17 +08:00
|
|
|
return luaT_eventname[e];
|
|
|
|
}
|
2000-08-08 04:21:34 +08:00
|
|
|
}
|
|
|
|
return NULL;
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-24 01:17:25 +08:00
|
|
|
static const l_char *travglobals (lua_State *L, const TObject *o) {
|
2000-05-09 03:32:53 +08:00
|
|
|
Hash *g = L->gt;
|
|
|
|
int i;
|
2000-09-19 03:39:49 +08:00
|
|
|
for (i=0; i<g->size; i++) {
|
2000-08-08 04:21:34 +08:00
|
|
|
if (luaO_equalObj(o, val(node(g, i))) &&
|
2001-02-10 02:37:33 +08:00
|
|
|
ttype_key(node(g, i)) == LUA_TSTRING)
|
2001-02-10 04:22:29 +08:00
|
|
|
return getstr(tsvalue_key(node(g, i)));
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
2000-08-08 04:21:34 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
static void getname (lua_State *L, StkId f, lua_Debug *ar) {
|
2000-08-08 04:21:34 +08:00
|
|
|
TObject o;
|
|
|
|
setnormalized(&o, f);
|
|
|
|
/* try to find a name for given function */
|
|
|
|
if ((ar->name = travglobals(L, &o)) != NULL)
|
2001-02-24 01:17:25 +08:00
|
|
|
ar->namewhat = l_s("global");
|
1999-12-15 02:31:20 +08:00
|
|
|
/* not found: try tag methods */
|
2001-01-19 21:20:30 +08:00
|
|
|
else if ((ar->name = travtagmethods(G(L), &o)) != NULL)
|
2001-02-24 01:17:25 +08:00
|
|
|
ar->namewhat = l_s("tag-method");
|
|
|
|
else ar->namewhat = l_s(""); /* not found at all */
|
2000-01-19 20:00:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-24 01:17:25 +08:00
|
|
|
LUA_API int lua_getinfo (lua_State *L, const l_char *what, lua_Debug *ar) {
|
2000-05-13 03:49:18 +08:00
|
|
|
StkId func;
|
2001-01-24 23:45:33 +08:00
|
|
|
int isactive;
|
|
|
|
int status = 1;
|
2001-03-03 01:27:50 +08:00
|
|
|
lua_lock(L);
|
2001-02-24 01:17:25 +08:00
|
|
|
isactive = (*what != l_c('>'));
|
2000-08-12 00:17:28 +08:00
|
|
|
if (isactive)
|
2000-05-13 03:49:18 +08:00
|
|
|
func = ar->_func;
|
|
|
|
else {
|
2001-02-23 02:59:59 +08:00
|
|
|
what++; /* skip the `>' */
|
2000-08-29 01:57:04 +08:00
|
|
|
func = L->top - 1;
|
2000-05-13 03:49:18 +08:00
|
|
|
}
|
2000-03-03 22:58:26 +08:00
|
|
|
for (; *what; what++) {
|
2000-01-19 20:00:45 +08:00
|
|
|
switch (*what) {
|
2001-02-24 01:17:25 +08:00
|
|
|
case l_c('S'): {
|
2000-10-21 00:39:03 +08:00
|
|
|
funcinfo(L, ar, func);
|
2000-01-19 20:00:45 +08:00
|
|
|
break;
|
2000-08-12 00:17:28 +08:00
|
|
|
}
|
2001-02-24 01:17:25 +08:00
|
|
|
case l_c('l'): {
|
2000-10-21 00:39:03 +08:00
|
|
|
ar->currentline = currentline(func);
|
2000-01-19 20:00:45 +08:00
|
|
|
break;
|
2000-08-12 00:17:28 +08:00
|
|
|
}
|
2001-02-24 01:17:25 +08:00
|
|
|
case l_c('u'): {
|
2000-10-21 00:39:03 +08:00
|
|
|
ar->nups = nups(func);
|
2000-01-19 20:00:45 +08:00
|
|
|
break;
|
2000-08-12 00:17:28 +08:00
|
|
|
}
|
2001-02-24 01:17:25 +08:00
|
|
|
case l_c('n'): {
|
2000-08-15 01:59:20 +08:00
|
|
|
ar->namewhat = (isactive) ? getfuncname(L, func, &ar->name) : NULL;
|
2000-08-12 00:17:28 +08:00
|
|
|
if (ar->namewhat == NULL)
|
2000-10-21 00:39:03 +08:00
|
|
|
getname(L, func, ar);
|
2000-01-19 20:00:45 +08:00
|
|
|
break;
|
2000-08-12 00:17:28 +08:00
|
|
|
}
|
2001-02-24 01:17:25 +08:00
|
|
|
case l_c('f'): {
|
2000-01-19 20:00:45 +08:00
|
|
|
setnormalized(L->top, func);
|
2000-08-29 01:57:04 +08:00
|
|
|
incr_top; /* push function */
|
2000-01-19 20:00:45 +08:00
|
|
|
break;
|
2000-08-12 00:17:28 +08:00
|
|
|
}
|
2001-01-24 23:45:33 +08:00
|
|
|
default: status = 0; /* invalid option */
|
2000-01-19 20:00:45 +08:00
|
|
|
}
|
|
|
|
}
|
2000-08-29 01:57:04 +08:00
|
|
|
if (!isactive) L->top--; /* pop function */
|
2001-03-03 01:27:50 +08:00
|
|
|
lua_unlock(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
return status;
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
|
|
|
|
2000-01-19 20:00:45 +08:00
|
|
|
|
2000-06-29 04:21:06 +08:00
|
|
|
/*
|
|
|
|
** {======================================================
|
2001-02-13 03:54:28 +08:00
|
|
|
** Symbolic Execution and code checker
|
2000-06-29 04:21:06 +08:00
|
|
|
** =======================================================
|
|
|
|
*/
|
|
|
|
|
2001-02-17 01:58:27 +08:00
|
|
|
#define check(x) if (!(x)) return 0;
|
2000-08-11 03:50:47 +08:00
|
|
|
|
2001-02-13 03:54:28 +08:00
|
|
|
|
|
|
|
static int checklineinfo (const Proto *pt) {
|
|
|
|
int *lineinfo = pt->lineinfo;
|
|
|
|
if (lineinfo == NULL) return 1;
|
|
|
|
check(pt->sizelineinfo >= 2 && lineinfo[pt->sizelineinfo-1] == MAX_INT);
|
|
|
|
if (*lineinfo < 0) lineinfo++;
|
|
|
|
check(*lineinfo == 0);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int precheck (const Proto *pt) {
|
|
|
|
check(checklineinfo(pt));
|
|
|
|
check(pt->maxstacksize <= MAXSTACK);
|
|
|
|
check(pt->numparams+pt->is_vararg <= pt->maxstacksize);
|
|
|
|
check(GET_OPCODE(pt->code[pt->sizecode-1]) == OP_RETURN);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* value for non-initialized entries in array stacklevel */
|
|
|
|
#define SL_EMPTY 255
|
|
|
|
|
2001-02-17 01:58:27 +08:00
|
|
|
#define checkjump(pt,sl,top,pc) if (!checkjump_aux(pt,sl,top,pc)) return 0;
|
2001-02-13 03:54:28 +08:00
|
|
|
|
2001-02-21 02:15:33 +08:00
|
|
|
static int checkjump_aux (const Proto *pt, lu_byte *sl, int top, int pc) {
|
2001-02-17 01:58:27 +08:00
|
|
|
check(0 <= pc && pc < pt->sizecode);
|
|
|
|
if (sl == NULL) return 1; /* not full checking */
|
|
|
|
if (sl[pc] == SL_EMPTY)
|
2001-02-22 00:52:09 +08:00
|
|
|
sl[pc] = (lu_byte)top;
|
2001-02-17 01:58:27 +08:00
|
|
|
else
|
|
|
|
check(sl[pc] == top);
|
|
|
|
return 1;
|
|
|
|
}
|
2001-02-13 03:54:28 +08:00
|
|
|
|
2001-02-17 01:58:27 +08:00
|
|
|
|
|
|
|
static Instruction luaG_symbexec (lua_State *L, const Proto *pt,
|
|
|
|
int lastpc, int stackpos) {
|
2000-08-11 03:50:47 +08:00
|
|
|
int stack[MAXSTACK]; /* stores last instruction that changed a stack entry */
|
2001-02-21 02:15:33 +08:00
|
|
|
lu_byte *sl = NULL;
|
2001-02-17 01:58:27 +08:00
|
|
|
int top;
|
|
|
|
int pc;
|
|
|
|
if (stackpos < 0) { /* full check? */
|
|
|
|
int i;
|
2001-02-24 04:32:16 +08:00
|
|
|
sl = luaO_openspace(L, pt->sizecode, lu_byte);
|
2001-02-17 01:58:27 +08:00
|
|
|
for (i=0; i<pt->sizecode; i++) /* initialize stack-level array */
|
|
|
|
sl[i] = SL_EMPTY;
|
|
|
|
check(precheck(pt));
|
|
|
|
}
|
|
|
|
top = pt->numparams;
|
|
|
|
pc = 0;
|
2000-06-29 04:21:06 +08:00
|
|
|
if (pt->is_vararg) /* varargs? */
|
|
|
|
top++; /* `arg' */
|
2001-02-22 00:52:09 +08:00
|
|
|
if (sl) sl[0] = (lu_byte)top;
|
2000-06-29 04:21:06 +08:00
|
|
|
while (pc < lastpc) {
|
2001-02-17 01:58:27 +08:00
|
|
|
const Instruction i = pt->code[pc++];
|
2001-02-10 02:37:33 +08:00
|
|
|
OpCode op = GET_OPCODE(i);
|
|
|
|
int arg1 = 0;
|
|
|
|
int arg2 = 0;
|
2001-02-13 03:54:28 +08:00
|
|
|
int push, pop;
|
|
|
|
check(op < NUM_OPCODES);
|
|
|
|
push = (int)luaK_opproperties[op].push;
|
|
|
|
pop = (int)luaK_opproperties[op].pop;
|
2001-02-10 02:37:33 +08:00
|
|
|
switch ((enum Mode)luaK_opproperties[op].mode) {
|
|
|
|
case iO: break;
|
2001-02-13 03:54:28 +08:00
|
|
|
case iU: arg1 = GETARG_U(i); check(arg1 >= 0); break;
|
2001-02-10 02:37:33 +08:00
|
|
|
case iS: arg1 = GETARG_S(i); break;
|
2001-02-13 03:54:28 +08:00
|
|
|
case iAB:
|
|
|
|
arg1 = GETARG_A(i); arg2 = GETARG_B(i); check(arg1 >= 0); break;
|
2001-02-10 02:37:33 +08:00
|
|
|
}
|
|
|
|
switch (op) {
|
2000-08-15 01:46:27 +08:00
|
|
|
case OP_RETURN: {
|
2001-02-13 03:54:28 +08:00
|
|
|
check(arg1 <= top);
|
2001-02-10 02:37:33 +08:00
|
|
|
pop = top-arg1;
|
2000-08-15 01:46:27 +08:00
|
|
|
break;
|
|
|
|
}
|
2001-02-10 02:37:33 +08:00
|
|
|
case OP_CALL: {
|
|
|
|
if (arg2 == MULT_RET) arg2 = 1;
|
2001-02-13 03:54:28 +08:00
|
|
|
check(arg1 < top);
|
2001-02-10 02:37:33 +08:00
|
|
|
pop = top-arg1;
|
|
|
|
push = arg2;
|
2000-08-15 01:46:27 +08:00
|
|
|
break;
|
2000-06-30 22:29:35 +08:00
|
|
|
}
|
2000-06-29 04:21:06 +08:00
|
|
|
case OP_PUSHNIL: {
|
2001-02-10 02:37:33 +08:00
|
|
|
check(arg1 > 0);
|
|
|
|
push = arg1;
|
2000-06-29 04:21:06 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OP_POP: {
|
2001-02-10 02:37:33 +08:00
|
|
|
pop = arg1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OP_PUSHSTRING:
|
|
|
|
case OP_GETGLOBAL:
|
|
|
|
case OP_GETDOTTED:
|
|
|
|
case OP_PUSHSELF:
|
|
|
|
case OP_SETGLOBAL: {
|
|
|
|
check(arg1 < pt->sizekstr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OP_PUSHNUM:
|
|
|
|
case OP_PUSHNEGNUM: {
|
|
|
|
check(arg1 < pt->sizeknum);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OP_PUSHUPVALUE: {
|
2001-02-21 02:28:11 +08:00
|
|
|
check(arg1 < pt->nupvalues);
|
2001-02-10 02:37:33 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OP_GETLOCAL:
|
|
|
|
case OP_GETINDEXED:
|
|
|
|
case OP_SETLOCAL: {
|
|
|
|
check(arg1 < top);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OP_SETTABLE: {
|
2001-02-13 03:54:28 +08:00
|
|
|
check(3 <= arg1 && arg1 <= top);
|
2001-02-10 02:37:33 +08:00
|
|
|
pop = arg2;
|
2000-06-29 04:21:06 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OP_SETLIST: {
|
2001-02-10 02:37:33 +08:00
|
|
|
pop = arg2;
|
|
|
|
check(top-pop >= 1); /* there must be a table below the list */
|
2000-06-29 04:21:06 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OP_SETMAP: {
|
2001-02-10 02:37:33 +08:00
|
|
|
pop = 2*arg1;
|
|
|
|
check(top-pop >= 1);
|
2000-06-29 04:21:06 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OP_CONCAT: {
|
2001-02-10 02:37:33 +08:00
|
|
|
pop = arg1;
|
2000-06-29 04:21:06 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OP_CLOSURE: {
|
2001-02-21 02:28:11 +08:00
|
|
|
check(arg1 < pt->sizekproto);
|
|
|
|
check(arg2 == pt->kproto[arg1]->nupvalues);
|
2001-02-10 02:37:33 +08:00
|
|
|
pop = arg2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OP_JMPNE:
|
|
|
|
case OP_JMPEQ:
|
|
|
|
case OP_JMPLT:
|
|
|
|
case OP_JMPLE:
|
|
|
|
case OP_JMPGT:
|
|
|
|
case OP_JMPGE:
|
|
|
|
case OP_JMPT:
|
2001-02-17 01:58:27 +08:00
|
|
|
case OP_JMPF:
|
|
|
|
case OP_JMP: {
|
|
|
|
checkjump(pt, sl, top-pop, pc+arg1);
|
2001-02-13 03:54:28 +08:00
|
|
|
break;
|
|
|
|
}
|
2001-02-10 02:37:33 +08:00
|
|
|
case OP_FORLOOP:
|
2001-02-17 01:58:27 +08:00
|
|
|
case OP_LFORLOOP:
|
2000-08-15 01:46:27 +08:00
|
|
|
case OP_JMPONT:
|
|
|
|
case OP_JMPONF: {
|
2001-02-13 03:54:28 +08:00
|
|
|
int newpc = pc+arg1;
|
2001-02-17 01:58:27 +08:00
|
|
|
checkjump(pt, sl, top, newpc);
|
2001-02-10 02:37:33 +08:00
|
|
|
/* jump is forward and do not skip `lastpc' and not full check? */
|
|
|
|
if (pc < newpc && newpc <= lastpc && stackpos >= 0) {
|
2000-08-15 01:46:27 +08:00
|
|
|
stack[top-1] = pc-1; /* value comes from `and'/`or' */
|
|
|
|
pc = newpc; /* do the jump */
|
2001-02-10 02:37:33 +08:00
|
|
|
pop = 0; /* do not pop */
|
2000-08-15 01:46:27 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2001-02-13 03:54:28 +08:00
|
|
|
case OP_PUSHNILJMP: {
|
2001-02-17 01:58:27 +08:00
|
|
|
check(GET_OPCODE(pt->code[pc]) == OP_PUSHINT); /* only valid sequence */
|
2001-02-13 03:54:28 +08:00
|
|
|
break;
|
|
|
|
}
|
2001-02-10 02:37:33 +08:00
|
|
|
case OP_FORPREP: {
|
2001-02-17 01:58:27 +08:00
|
|
|
int endfor = pc-arg1-1; /* jump is `negative' here */
|
2001-02-10 02:37:33 +08:00
|
|
|
check(top >= 3);
|
2001-02-17 01:58:27 +08:00
|
|
|
checkjump(pt, sl, top+push, endfor);
|
|
|
|
check(GET_OPCODE(pt->code[endfor]) == OP_FORLOOP);
|
|
|
|
check(GETARG_S(pt->code[endfor]) == arg1);
|
2001-02-10 02:37:33 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OP_LFORPREP: {
|
2001-02-17 01:58:27 +08:00
|
|
|
int endfor = pc-arg1-1; /* jump is `negative' here */
|
2001-02-10 02:37:33 +08:00
|
|
|
check(top >= 1);
|
2001-02-17 01:58:27 +08:00
|
|
|
checkjump(pt, sl, top+push, endfor);
|
|
|
|
check(GET_OPCODE(pt->code[endfor]) == OP_LFORLOOP);
|
|
|
|
check(GETARG_S(pt->code[endfor]) == arg1);
|
2001-02-10 02:37:33 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OP_PUSHINT:
|
|
|
|
case OP_GETTABLE:
|
|
|
|
case OP_CREATETABLE:
|
|
|
|
case OP_ADD:
|
|
|
|
case OP_ADDI:
|
|
|
|
case OP_SUB:
|
|
|
|
case OP_MULT:
|
|
|
|
case OP_DIV:
|
|
|
|
case OP_POW:
|
|
|
|
case OP_MINUS:
|
|
|
|
case OP_NOT: {
|
|
|
|
break;
|
2000-06-29 04:21:06 +08:00
|
|
|
}
|
|
|
|
}
|
2001-02-10 02:37:33 +08:00
|
|
|
top -= pop;
|
2001-02-13 03:54:28 +08:00
|
|
|
check(0 <= top && top+push <= pt->maxstacksize);
|
2001-02-10 02:37:33 +08:00
|
|
|
while (push--) stack[top++] = pc-1;
|
2001-02-17 01:58:27 +08:00
|
|
|
checkjump(pt, sl, top, pc);
|
2000-06-29 04:21:06 +08:00
|
|
|
}
|
2001-02-17 01:58:27 +08:00
|
|
|
return (stackpos >= 0) ? pt->code[stack[stackpos]] : 1;
|
2001-02-10 02:37:33 +08:00
|
|
|
}
|
|
|
|
|
2001-02-17 01:58:27 +08:00
|
|
|
/* }====================================================== */
|
|
|
|
|
2001-02-10 02:37:33 +08:00
|
|
|
|
2001-02-13 03:54:28 +08:00
|
|
|
int luaG_checkcode (lua_State *L, const Proto *pt) {
|
2001-02-17 01:58:27 +08:00
|
|
|
return luaG_symbexec(L, pt, pt->sizecode-1, -1);
|
2000-06-29 04:21:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-24 01:17:25 +08:00
|
|
|
static const l_char *getobjname (lua_State *L, StkId obj, const l_char **name) {
|
2000-06-29 04:21:06 +08:00
|
|
|
StkId func = aux_stackedfunction(L, 0, obj);
|
2000-10-05 20:14:08 +08:00
|
|
|
if (!isLmark(func))
|
|
|
|
return NULL; /* not an active Lua function */
|
2000-06-29 04:21:06 +08:00
|
|
|
else {
|
|
|
|
Proto *p = infovalue(func)->func->f.l;
|
2000-10-21 00:39:03 +08:00
|
|
|
int pc = currentpc(func);
|
2000-06-29 04:21:06 +08:00
|
|
|
int stackpos = obj - (func+1); /* func+1 == function base */
|
2001-02-17 01:58:27 +08:00
|
|
|
Instruction i = luaG_symbexec(L, p, pc, stackpos);
|
2001-01-19 21:20:30 +08:00
|
|
|
lua_assert(pc != -1);
|
2000-06-29 04:21:06 +08:00
|
|
|
switch (GET_OPCODE(i)) {
|
|
|
|
case OP_GETGLOBAL: {
|
2001-02-10 04:22:29 +08:00
|
|
|
*name = getstr(p->kstr[GETARG_U(i)]);
|
2001-02-24 01:17:25 +08:00
|
|
|
return l_s("global");
|
2000-06-29 04:21:06 +08:00
|
|
|
}
|
|
|
|
case OP_GETLOCAL: {
|
|
|
|
*name = luaF_getlocalname(p, GETARG_U(i)+1, pc);
|
2001-01-19 21:20:30 +08:00
|
|
|
lua_assert(*name);
|
2001-02-24 01:17:25 +08:00
|
|
|
return l_s("local");
|
2000-06-29 04:21:06 +08:00
|
|
|
}
|
|
|
|
case OP_PUSHSELF:
|
|
|
|
case OP_GETDOTTED: {
|
2001-02-10 04:22:29 +08:00
|
|
|
*name = getstr(p->kstr[GETARG_U(i)]);
|
2001-02-24 01:17:25 +08:00
|
|
|
return l_s("field");
|
2000-06-29 04:21:06 +08:00
|
|
|
}
|
|
|
|
default:
|
2000-08-16 02:28:48 +08:00
|
|
|
return NULL; /* no useful name found */
|
2000-06-29 04:21:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-24 01:17:25 +08:00
|
|
|
static const l_char *getfuncname (lua_State *L, StkId f, const l_char **name) {
|
2000-08-12 00:17:28 +08:00
|
|
|
StkId func = aux_stackedfunction(L, 0, f); /* calling function */
|
2000-10-05 20:14:08 +08:00
|
|
|
if (!isLmark(func))
|
|
|
|
return NULL; /* not an active Lua function */
|
2000-08-12 00:17:28 +08:00
|
|
|
else {
|
|
|
|
Proto *p = infovalue(func)->func->f.l;
|
2000-10-21 00:39:03 +08:00
|
|
|
int pc = currentpc(func);
|
2000-08-31 21:29:12 +08:00
|
|
|
Instruction i;
|
|
|
|
if (pc == -1) return NULL; /* function is not activated */
|
|
|
|
i = p->code[pc];
|
2001-03-07 21:22:55 +08:00
|
|
|
return (GET_OPCODE(i) == OP_CALL
|
|
|
|
? getobjname(L, (func+1)+GETARG_A(i), name)
|
|
|
|
: NULL); /* no useful name found */
|
2000-08-12 00:17:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-24 01:17:25 +08:00
|
|
|
void luaG_typeerror (lua_State *L, StkId o, const l_char *op) {
|
|
|
|
const l_char *name;
|
|
|
|
const l_char *kind = getobjname(L, o, &name);
|
|
|
|
const l_char *t = luaT_typename(G(L), o);
|
2000-06-29 04:21:06 +08:00
|
|
|
if (kind)
|
2001-02-24 01:17:25 +08:00
|
|
|
luaO_verror(L, l_s("attempt to %.30s %.20s `%.40s' (a %.10s value)"),
|
2000-08-11 03:50:47 +08:00
|
|
|
op, kind, name, t);
|
2000-06-29 04:21:06 +08:00
|
|
|
else
|
2001-02-24 01:17:25 +08:00
|
|
|
luaO_verror(L, l_s("attempt to %.30s a %.10s value"), op, t);
|
1999-12-30 00:31:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-24 01:17:25 +08:00
|
|
|
void luaG_binerror (lua_State *L, StkId p1, int t, const l_char *op) {
|
2000-08-11 03:50:47 +08:00
|
|
|
if (ttype(p1) == t) p1++;
|
2001-01-19 21:20:30 +08:00
|
|
|
lua_assert(ttype(p1) != t);
|
2000-08-11 03:50:47 +08:00
|
|
|
luaG_typeerror(L, p1, op);
|
1999-12-30 00:31:15 +08:00
|
|
|
}
|
2000-08-09 04:42:07 +08:00
|
|
|
|
2000-08-12 00:17:28 +08:00
|
|
|
|
2001-02-08 02:13:49 +08:00
|
|
|
void luaG_ordererror (lua_State *L, const TObject *p1, const TObject *p2) {
|
2001-02-24 01:17:25 +08:00
|
|
|
const l_char *t1 = luaT_typename(G(L), p1);
|
|
|
|
const l_char *t2 = luaT_typename(G(L), p2);
|
2000-08-12 00:17:28 +08:00
|
|
|
if (t1[2] == t2[2])
|
2001-02-24 01:17:25 +08:00
|
|
|
luaO_verror(L, l_s("attempt to compare two %.10s values"), t1);
|
2000-08-12 00:17:28 +08:00
|
|
|
else
|
2001-02-24 01:17:25 +08:00
|
|
|
luaO_verror(L, l_s("attempt to compare %.10s with %.10s"), t1, t2);
|
2000-08-12 00:17:28 +08:00
|
|
|
}
|
|
|
|
|