00001
00002
00003
00004
00005
00006
00007
00008 #include <stdarg.h>
00009 #include <stddef.h>
00010 #include <string.h>
00011
00012
00013 #define ldebug_c
00014 #define LUA_CORE
00015
00016 #include "lua.h"
00017
00018 #include "lapi.h"
00019 #include "lcode.h"
00020 #include "ldebug.h"
00021 #include "ldo.h"
00022 #include "lfunc.h"
00023 #include "lobject.h"
00024 #include "lopcodes.h"
00025 #include "lstate.h"
00026 #include "lstring.h"
00027 #include "ltable.h"
00028 #include "ltm.h"
00029 #include "lvm.h"
00030
00031
00032
00033 static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name);
00034
00035
00036 static int currentpc (lua_State *L, CallInfo *ci) {
00037 if (!isLua(ci)) return -1;
00038 if (ci == L->ci)
00039 ci->savedpc = L->savedpc;
00040 return pcRel(ci->savedpc, ci_func(ci)->l.p);
00041 }
00042
00043
00044 static int currentline (lua_State *L, CallInfo *ci) {
00045 int pc = currentpc(L, ci);
00046 if (pc < 0)
00047 return -1;
00048 else
00049 return getline(ci_func(ci)->l.p, pc);
00050 }
00051
00052
00053
00054
00055
00056 LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count) {
00057 if (func == NULL || mask == 0) {
00058 mask = 0;
00059 func = NULL;
00060 }
00061 L->hook = func;
00062 L->basehookcount = count;
00063 resethookcount(L);
00064 L->hookmask = cast_byte(mask);
00065 return 1;
00066 }
00067
00068
00069 LUA_API lua_Hook lua_gethook (lua_State *L) {
00070 return L->hook;
00071 }
00072
00073
00074 LUA_API int lua_gethookmask (lua_State *L) {
00075 return L->hookmask;
00076 }
00077
00078
00079 LUA_API int lua_gethookcount (lua_State *L) {
00080 return L->basehookcount;
00081 }
00082
00083
00084 LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
00085 int status;
00086 CallInfo *ci;
00087 lua_lock(L);
00088 for (ci = L->ci; level > 0 && ci > L->base_ci; ci--) {
00089 level--;
00090 if (f_isLua(ci))
00091 level -= ci->tailcalls;
00092 }
00093 if (level == 0 && ci > L->base_ci) {
00094 status = 1;
00095 ar->i_ci = cast_int(ci - L->base_ci);
00096 }
00097 else if (level < 0) {
00098 status = 1;
00099 ar->i_ci = 0;
00100 }
00101 else status = 0;
00102 lua_unlock(L);
00103 return status;
00104 }
00105
00106
00107 static Proto *getluaproto (CallInfo *ci) {
00108 return (isLua(ci) ? ci_func(ci)->l.p : NULL);
00109 }
00110
00111
00112 static const char *findlocal (lua_State *L, CallInfo *ci, int n) {
00113 const char *name;
00114 Proto *fp = getluaproto(ci);
00115 if (fp && (name = luaF_getlocalname(fp, n, currentpc(L, ci))) != NULL)
00116 return name;
00117 else {
00118 StkId limit = (ci == L->ci) ? L->top : (ci+1)->func;
00119 if (limit - ci->base >= n && n > 0)
00120 return "(*temporary)";
00121 else
00122 return NULL;
00123 }
00124 }
00125
00126
00127 LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
00128 CallInfo *ci = L->base_ci + ar->i_ci;
00129 const char *name = findlocal(L, ci, n);
00130 lua_lock(L);
00131 if (name)
00132 luaA_pushobject(L, ci->base + (n - 1));
00133 lua_unlock(L);
00134 return name;
00135 }
00136
00137
00138 LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
00139 CallInfo *ci = L->base_ci + ar->i_ci;
00140 const char *name = findlocal(L, ci, n);
00141 lua_lock(L);
00142 if (name)
00143 setobjs2s(L, ci->base + (n - 1), L->top - 1);
00144 L->top--;
00145 lua_unlock(L);
00146 return name;
00147 }
00148
00149
00150 static void funcinfo (lua_Debug *ar, Closure *cl) {
00151 if (cl->c.isC) {
00152 ar->source = "=[C]";
00153 ar->linedefined = -1;
00154 ar->lastlinedefined = -1;
00155 ar->what = "C";
00156 }
00157 else {
00158 ar->source = getstr(cl->l.p->source);
00159 ar->linedefined = cl->l.p->linedefined;
00160 ar->lastlinedefined = cl->l.p->lastlinedefined;
00161 ar->what = (ar->linedefined == 0) ? "main" : "Lua";
00162 }
00163 luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
00164 }
00165
00166
00167 static void info_tailcall (lua_Debug *ar) {
00168 ar->name = ar->namewhat = "";
00169 ar->what = "tail";
00170 ar->lastlinedefined = ar->linedefined = ar->currentline = -1;
00171 ar->source = "=(tail call)";
00172 luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
00173 ar->nups = 0;
00174 }
00175
00176
00177 static void collectvalidlines (lua_State *L, Closure *f) {
00178 if (f == NULL || f->c.isC) {
00179 setnilvalue(L->top);
00180 }
00181 else {
00182 Table *t = luaH_new(L, 0, 0);
00183 int *lineinfo = f->l.p->lineinfo;
00184 int i;
00185 for (i=0; i<f->l.p->sizelineinfo; i++)
00186 setbvalue(luaH_setnum(L, t, lineinfo[i]), 1);
00187 sethvalue(L, L->top, t);
00188 }
00189 incr_top(L);
00190 }
00191
00192
00193 static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
00194 Closure *f, CallInfo *ci) {
00195 int status = 1;
00196 if (f == NULL) {
00197 info_tailcall(ar);
00198 return status;
00199 }
00200 for (; *what; what++) {
00201 switch (*what) {
00202 case 'S': {
00203 funcinfo(ar, f);
00204 break;
00205 }
00206 case 'l': {
00207 ar->currentline = (ci) ? currentline(L, ci) : -1;
00208 break;
00209 }
00210 case 'u': {
00211 ar->nups = f->c.nupvalues;
00212 break;
00213 }
00214 case 'n': {
00215 ar->namewhat = (ci) ? getfuncname(L, ci, &ar->name) : NULL;
00216 if (ar->namewhat == NULL) {
00217 ar->namewhat = "";
00218 ar->name = NULL;
00219 }
00220 break;
00221 }
00222 case 'L':
00223 case 'f':
00224 break;
00225 default: status = 0;
00226 }
00227 }
00228 return status;
00229 }
00230
00231
00232 LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
00233 int status;
00234 Closure *f = NULL;
00235 CallInfo *ci = NULL;
00236 lua_lock(L);
00237 if (*what == '>') {
00238 StkId func = L->top - 1;
00239 luai_apicheck(L, ttisfunction(func));
00240 what++;
00241 f = clvalue(func);
00242 L->top--;
00243 }
00244 else if (ar->i_ci != 0) {
00245 ci = L->base_ci + ar->i_ci;
00246 lua_assert(ttisfunction(ci->func));
00247 f = clvalue(ci->func);
00248 }
00249 status = auxgetinfo(L, what, ar, f, ci);
00250 if (strchr(what, 'f')) {
00251 if (f == NULL) setnilvalue(L->top);
00252 else setclvalue(L, L->top, f);
00253 incr_top(L);
00254 }
00255 if (strchr(what, 'L'))
00256 collectvalidlines(L, f);
00257 lua_unlock(L);
00258 return status;
00259 }
00260
00261
00262
00263
00264
00265
00266
00267
00268 #define check(x) if (!(x)) return 0;
00269
00270 #define checkjump(pt,pc) check(0 <= pc && pc < pt->sizecode)
00271
00272 #define checkreg(pt,reg) check((reg) < (pt)->maxstacksize)
00273
00274
00275
00276 static int precheck (const Proto *pt) {
00277 check(pt->maxstacksize <= MAXSTACK);
00278 check(pt->numparams+(pt->is_vararg & VARARG_HASARG) <= pt->maxstacksize);
00279 check(!(pt->is_vararg & VARARG_NEEDSARG) ||
00280 (pt->is_vararg & VARARG_HASARG));
00281 check(pt->sizeupvalues <= pt->nups);
00282 check(pt->sizelineinfo == pt->sizecode || pt->sizelineinfo == 0);
00283 check(pt->sizecode > 0 && GET_OPCODE(pt->code[pt->sizecode-1]) == OP_RETURN);
00284 return 1;
00285 }
00286
00287
00288 #define checkopenop(pt,pc) luaG_checkopenop((pt)->code[(pc)+1])
00289
00290 int luaG_checkopenop (Instruction i) {
00291 switch (GET_OPCODE(i)) {
00292 case OP_CALL:
00293 case OP_TAILCALL:
00294 case OP_RETURN:
00295 case OP_SETLIST: {
00296 check(GETARG_B(i) == 0);
00297 return 1;
00298 }
00299 default: return 0;
00300 }
00301 }
00302
00303
00304 static int checkArgMode (const Proto *pt, int r, enum OpArgMask mode) {
00305 switch (mode) {
00306 case OpArgN: check(r == 0); break;
00307 case OpArgU: break;
00308 case OpArgR: checkreg(pt, r); break;
00309 case OpArgK:
00310 check(ISK(r) ? INDEXK(r) < pt->sizek : r < pt->maxstacksize);
00311 break;
00312 }
00313 return 1;
00314 }
00315
00316
00317 static Instruction symbexec (const Proto *pt, int lastpc, int reg) {
00318 int pc;
00319 int last;
00320 last = pt->sizecode-1;
00321 check(precheck(pt));
00322 for (pc = 0; pc < lastpc; pc++) {
00323 Instruction i = pt->code[pc];
00324 OpCode op = GET_OPCODE(i);
00325 int a = GETARG_A(i);
00326 int b = 0;
00327 int c = 0;
00328 check(op < NUM_OPCODES);
00329 checkreg(pt, a);
00330 switch (getOpMode(op)) {
00331 case iABC: {
00332 b = GETARG_B(i);
00333 c = GETARG_C(i);
00334 check(checkArgMode(pt, b, getBMode(op)));
00335 check(checkArgMode(pt, c, getCMode(op)));
00336 break;
00337 }
00338 case iABx: {
00339 b = GETARG_Bx(i);
00340 if (getBMode(op) == OpArgK) check(b < pt->sizek);
00341 break;
00342 }
00343 case iAsBx: {
00344 b = GETARG_sBx(i);
00345 if (getBMode(op) == OpArgR) {
00346 int dest = pc+1+b;
00347 check(0 <= dest && dest < pt->sizecode);
00348 if (dest > 0) {
00349 int j;
00350
00351
00352
00353
00354 for (j = 0; j < dest; j++) {
00355 Instruction d = pt->code[dest-1-j];
00356 if (!(GET_OPCODE(d) == OP_SETLIST && GETARG_C(d) == 0)) break;
00357 }
00358
00359
00360 check((j&1) == 0);
00361 }
00362 }
00363 break;
00364 }
00365 }
00366 if (testAMode(op)) {
00367 if (a == reg) last = pc;
00368 }
00369 if (testTMode(op)) {
00370 check(pc+2 < pt->sizecode);
00371 check(GET_OPCODE(pt->code[pc+1]) == OP_JMP);
00372 }
00373 switch (op) {
00374 case OP_LOADBOOL: {
00375 if (c == 1) {
00376 check(pc+2 < pt->sizecode);
00377 check(GET_OPCODE(pt->code[pc+1]) != OP_SETLIST ||
00378 GETARG_C(pt->code[pc+1]) != 0);
00379 }
00380 break;
00381 }
00382 case OP_LOADNIL: {
00383 if (a <= reg && reg <= b)
00384 last = pc;
00385 break;
00386 }
00387 case OP_GETUPVAL:
00388 case OP_SETUPVAL: {
00389 check(b < pt->nups);
00390 break;
00391 }
00392 case OP_GETGLOBAL:
00393 case OP_SETGLOBAL: {
00394 check(ttisstring(&pt->k[b]));
00395 break;
00396 }
00397 case OP_SELF: {
00398 checkreg(pt, a+1);
00399 if (reg == a+1) last = pc;
00400 break;
00401 }
00402 case OP_CONCAT: {
00403 check(b < c);
00404 break;
00405 }
00406 case OP_TFORLOOP: {
00407 check(c >= 1);
00408 checkreg(pt, a+2+c);
00409 if (reg >= a+2) last = pc;
00410 break;
00411 }
00412 case OP_FORLOOP:
00413 case OP_FORPREP:
00414 checkreg(pt, a+3);
00415
00416 case OP_JMP: {
00417 int dest = pc+1+b;
00418
00419 if (reg != NO_REG && pc < dest && dest <= lastpc)
00420 pc += b;
00421 break;
00422 }
00423 case OP_CALL:
00424 case OP_TAILCALL: {
00425 if (b != 0) {
00426 checkreg(pt, a+b-1);
00427 }
00428 c--;
00429 if (c == LUA_MULTRET) {
00430 check(checkopenop(pt, pc));
00431 }
00432 else if (c != 0)
00433 checkreg(pt, a+c-1);
00434 if (reg >= a) last = pc;
00435 break;
00436 }
00437 case OP_RETURN: {
00438 b--;
00439 if (b > 0) checkreg(pt, a+b-1);
00440 break;
00441 }
00442 case OP_SETLIST: {
00443 if (b > 0) checkreg(pt, a + b);
00444 if (c == 0) {
00445 pc++;
00446 check(pc < pt->sizecode - 1);
00447 }
00448 break;
00449 }
00450 case OP_CLOSURE: {
00451 int nup, j;
00452 check(b < pt->sizep);
00453 nup = pt->p[b]->nups;
00454 check(pc + nup < pt->sizecode);
00455 for (j = 1; j <= nup; j++) {
00456 OpCode op1 = GET_OPCODE(pt->code[pc + j]);
00457 check(op1 == OP_GETUPVAL || op1 == OP_MOVE);
00458 }
00459 if (reg != NO_REG)
00460 pc += nup;
00461 break;
00462 }
00463 case OP_VARARG: {
00464 check((pt->is_vararg & VARARG_ISVARARG) &&
00465 !(pt->is_vararg & VARARG_NEEDSARG));
00466 b--;
00467 if (b == LUA_MULTRET) check(checkopenop(pt, pc));
00468 checkreg(pt, a+b-1);
00469 break;
00470 }
00471 default: break;
00472 }
00473 }
00474 return pt->code[last];
00475 }
00476
00477 #undef check
00478 #undef checkjump
00479 #undef checkreg
00480
00481
00482
00483
00484 int luaG_checkcode (const Proto *pt) {
00485 return (symbexec(pt, pt->sizecode, NO_REG) != 0);
00486 }
00487
00488
00489 static const char *kname (Proto *p, int c) {
00490 if (ISK(c) && ttisstring(&p->k[INDEXK(c)]))
00491 return svalue(&p->k[INDEXK(c)]);
00492 else
00493 return "?";
00494 }
00495
00496
00497 static const char *getobjname (lua_State *L, CallInfo *ci, int stackpos,
00498 const char **name) {
00499 if (isLua(ci)) {
00500 Proto *p = ci_func(ci)->l.p;
00501 int pc = currentpc(L, ci);
00502 Instruction i;
00503 *name = luaF_getlocalname(p, stackpos+1, pc);
00504 if (*name)
00505 return "local";
00506 i = symbexec(p, pc, stackpos);
00507 lua_assert(pc != -1);
00508 switch (GET_OPCODE(i)) {
00509 case OP_GETGLOBAL: {
00510 int g = GETARG_Bx(i);
00511 lua_assert(ttisstring(&p->k[g]));
00512 *name = svalue(&p->k[g]);
00513 return "global";
00514 }
00515 case OP_MOVE: {
00516 int a = GETARG_A(i);
00517 int b = GETARG_B(i);
00518 if (b < a)
00519 return getobjname(L, ci, b, name);
00520 break;
00521 }
00522 case OP_GETTABLE: {
00523 int k = GETARG_C(i);
00524 *name = kname(p, k);
00525 return "field";
00526 }
00527 case OP_GETUPVAL: {
00528 int u = GETARG_B(i);
00529 *name = p->upvalues ? getstr(p->upvalues[u]) : "?";
00530 return "upvalue";
00531 }
00532 case OP_SELF: {
00533 int k = GETARG_C(i);
00534 *name = kname(p, k);
00535 return "method";
00536 }
00537 default: break;
00538 }
00539 }
00540 return NULL;
00541 }
00542
00543
00544 static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
00545 Instruction i;
00546 if ((isLua(ci) && ci->tailcalls > 0) || !isLua(ci - 1))
00547 return NULL;
00548 ci--;
00549 i = ci_func(ci)->l.p->code[currentpc(L, ci)];
00550 if (GET_OPCODE(i) == OP_CALL || GET_OPCODE(i) == OP_TAILCALL ||
00551 GET_OPCODE(i) == OP_TFORLOOP)
00552 return getobjname(L, ci, GETARG_A(i), name);
00553 else
00554 return NULL;
00555 }
00556
00557
00558
00559 static int isinstack (CallInfo *ci, const TValue *o) {
00560 StkId p;
00561 for (p = ci->base; p < ci->top; p++)
00562 if (o == p) return 1;
00563 return 0;
00564 }
00565
00566
00567 void luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
00568 const char *name = NULL;
00569 const char *t = luaT_typenames[ttype(o)];
00570 const char *kind = (isinstack(L->ci, o)) ?
00571 getobjname(L, L->ci, cast_int(o - L->base), &name) :
00572 NULL;
00573 if (kind)
00574 luaG_runerror(L, "attempt to %s %s " LUA_QS " (a %s value)",
00575 op, kind, name, t);
00576 else
00577 luaG_runerror(L, "attempt to %s a %s value", op, t);
00578 }
00579
00580
00581 void luaG_concaterror (lua_State *L, StkId p1, StkId p2) {
00582 if (ttisstring(p1) || ttisnumber(p1)) p1 = p2;
00583 lua_assert(!ttisstring(p1) && !ttisnumber(p1));
00584 luaG_typeerror(L, p1, "concatenate");
00585 }
00586
00587
00588 void luaG_aritherror (lua_State *L, const TValue *p1, const TValue *p2) {
00589 TValue temp;
00590 if (luaV_tonumber(p1, &temp) == NULL)
00591 p2 = p1;
00592 luaG_typeerror(L, p2, "perform arithmetic on");
00593 }
00594
00595
00596 int luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
00597 const char *t1 = luaT_typenames[ttype(p1)];
00598 const char *t2 = luaT_typenames[ttype(p2)];
00599 if (t1[2] == t2[2])
00600 luaG_runerror(L, "attempt to compare two %s values", t1);
00601 else
00602 luaG_runerror(L, "attempt to compare %s with %s", t1, t2);
00603 return 0;
00604 }
00605
00606
00607 static void addinfo (lua_State *L, const char *msg) {
00608 CallInfo *ci = L->ci;
00609 if (isLua(ci)) {
00610 char buff[LUA_IDSIZE];
00611 int line = currentline(L, ci);
00612 luaO_chunkid(buff, getstr(getluaproto(ci)->source), LUA_IDSIZE);
00613 luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);
00614 }
00615 }
00616
00617
00618 void luaG_errormsg (lua_State *L) {
00619 if (L->errfunc != 0) {
00620 StkId errfunc = restorestack(L, L->errfunc);
00621 if (!ttisfunction(errfunc)) luaD_throw(L, LUA_ERRERR);
00622 setobjs2s(L, L->top, L->top - 1);
00623 setobjs2s(L, L->top - 1, errfunc);
00624 incr_top(L);
00625 luaD_call(L, L->top - 2, 1);
00626 }
00627 luaD_throw(L, LUA_ERRRUN);
00628 }
00629
00630
00631 void luaG_runerror (lua_State *L, const char *fmt, ...) {
00632 va_list argp;
00633 va_start(argp, fmt);
00634 addinfo(L, luaO_pushvfstring(L, fmt, argp));
00635 va_end(argp);
00636 luaG_errormsg(L);
00637 }