annotate src/lua/ldebug.c @ 550:23572082c4a5

going to explore a cool new idea for the final video.
author Robert McIntyre <rlm@mit.edu>
date Thu, 30 Aug 2012 09:32:57 -0500
parents 27763b933818
children
rev   line source
rlm@1 1 /*
rlm@1 2 ** $Id: ldebug.c,v 2.29.1.6 2008/05/08 16:56:26 roberto Exp $
rlm@1 3 ** Debug Interface
rlm@1 4 ** See Copyright Notice in lua.h
rlm@1 5 */
rlm@1 6
rlm@1 7
rlm@1 8 #include <stdarg.h>
rlm@1 9 #include <stddef.h>
rlm@1 10 #include <string.h>
rlm@1 11
rlm@1 12
rlm@1 13 #define ldebug_c
rlm@1 14 #define LUA_CORE
rlm@1 15
rlm@1 16 #include "lua.h"
rlm@1 17
rlm@1 18 #include "lapi.h"
rlm@1 19 #include "lcode.h"
rlm@1 20 #include "ldebug.h"
rlm@1 21 #include "ldo.h"
rlm@1 22 #include "lfunc.h"
rlm@1 23 #include "lobject.h"
rlm@1 24 #include "lopcodes.h"
rlm@1 25 #include "lstate.h"
rlm@1 26 #include "lstring.h"
rlm@1 27 #include "ltable.h"
rlm@1 28 #include "ltm.h"
rlm@1 29 #include "lvm.h"
rlm@1 30
rlm@1 31
rlm@1 32
rlm@1 33 static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name);
rlm@1 34
rlm@1 35
rlm@1 36 static int currentpc (lua_State *L, CallInfo *ci) {
rlm@1 37 if (!isLua(ci)) return -1; /* function is not a Lua function? */
rlm@1 38 if (ci == L->ci)
rlm@1 39 ci->savedpc = L->savedpc;
rlm@1 40 return pcRel(ci->savedpc, ci_func(ci)->l.p);
rlm@1 41 }
rlm@1 42
rlm@1 43
rlm@1 44 static int currentline (lua_State *L, CallInfo *ci) {
rlm@1 45 int pc = currentpc(L, ci);
rlm@1 46 if (pc < 0)
rlm@1 47 return -1; /* only active lua functions have current-line information */
rlm@1 48 else
rlm@1 49 return getline(ci_func(ci)->l.p, pc);
rlm@1 50 }
rlm@1 51
rlm@1 52
rlm@1 53 /*
rlm@1 54 ** this function can be called asynchronous (e.g. during a signal)
rlm@1 55 */
rlm@1 56 LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count) {
rlm@1 57 if (func == NULL || mask == 0) { /* turn off hooks? */
rlm@1 58 mask = 0;
rlm@1 59 func = NULL;
rlm@1 60 }
rlm@1 61 L->hook = func;
rlm@1 62 L->basehookcount = count;
rlm@1 63 resethookcount(L);
rlm@1 64 L->hookmask = cast_byte(mask);
rlm@1 65 return 1;
rlm@1 66 }
rlm@1 67
rlm@1 68
rlm@1 69 LUA_API lua_Hook lua_gethook (lua_State *L) {
rlm@1 70 return L->hook;
rlm@1 71 }
rlm@1 72
rlm@1 73
rlm@1 74 LUA_API int lua_gethookmask (lua_State *L) {
rlm@1 75 return L->hookmask;
rlm@1 76 }
rlm@1 77
rlm@1 78
rlm@1 79 LUA_API int lua_gethookcount (lua_State *L) {
rlm@1 80 return L->basehookcount;
rlm@1 81 }
rlm@1 82
rlm@1 83
rlm@1 84 LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
rlm@1 85 int status;
rlm@1 86 CallInfo *ci;
rlm@1 87 lua_lock(L);
rlm@1 88 for (ci = L->ci; level > 0 && ci > L->base_ci; ci--) {
rlm@1 89 level--;
rlm@1 90 if (f_isLua(ci)) /* Lua function? */
rlm@1 91 level -= ci->tailcalls; /* skip lost tail calls */
rlm@1 92 }
rlm@1 93 if (level == 0 && ci > L->base_ci) { /* level found? */
rlm@1 94 status = 1;
rlm@1 95 ar->i_ci = cast_int(ci - L->base_ci);
rlm@1 96 }
rlm@1 97 else if (level < 0) { /* level is of a lost tail call? */
rlm@1 98 status = 1;
rlm@1 99 ar->i_ci = 0;
rlm@1 100 }
rlm@1 101 else status = 0; /* no such level */
rlm@1 102 lua_unlock(L);
rlm@1 103 return status;
rlm@1 104 }
rlm@1 105
rlm@1 106
rlm@1 107 static Proto *getluaproto (CallInfo *ci) {
rlm@1 108 return (isLua(ci) ? ci_func(ci)->l.p : NULL);
rlm@1 109 }
rlm@1 110
rlm@1 111
rlm@1 112 static const char *findlocal (lua_State *L, CallInfo *ci, int n) {
rlm@1 113 const char *name;
rlm@1 114 Proto *fp = getluaproto(ci);
rlm@1 115 if (fp && (name = luaF_getlocalname(fp, n, currentpc(L, ci))) != NULL)
rlm@1 116 return name; /* is a local variable in a Lua function */
rlm@1 117 else {
rlm@1 118 StkId limit = (ci == L->ci) ? L->top : (ci+1)->func;
rlm@1 119 if (limit - ci->base >= n && n > 0) /* is 'n' inside 'ci' stack? */
rlm@1 120 return "(*temporary)";
rlm@1 121 else
rlm@1 122 return NULL;
rlm@1 123 }
rlm@1 124 }
rlm@1 125
rlm@1 126
rlm@1 127 LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
rlm@1 128 CallInfo *ci = L->base_ci + ar->i_ci;
rlm@1 129 const char *name = findlocal(L, ci, n);
rlm@1 130 lua_lock(L);
rlm@1 131 if (name)
rlm@1 132 luaA_pushobject(L, ci->base + (n - 1));
rlm@1 133 lua_unlock(L);
rlm@1 134 return name;
rlm@1 135 }
rlm@1 136
rlm@1 137
rlm@1 138 LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
rlm@1 139 CallInfo *ci = L->base_ci + ar->i_ci;
rlm@1 140 const char *name = findlocal(L, ci, n);
rlm@1 141 lua_lock(L);
rlm@1 142 if (name)
rlm@1 143 setobjs2s(L, ci->base + (n - 1), L->top - 1);
rlm@1 144 L->top--; /* pop value */
rlm@1 145 lua_unlock(L);
rlm@1 146 return name;
rlm@1 147 }
rlm@1 148
rlm@1 149
rlm@1 150 static void funcinfo (lua_Debug *ar, Closure *cl) {
rlm@1 151 if (cl->c.isC) {
rlm@1 152 ar->source = "=[C]";
rlm@1 153 ar->linedefined = -1;
rlm@1 154 ar->lastlinedefined = -1;
rlm@1 155 ar->what = "C";
rlm@1 156 }
rlm@1 157 else {
rlm@1 158 ar->source = getstr(cl->l.p->source);
rlm@1 159 ar->linedefined = cl->l.p->linedefined;
rlm@1 160 ar->lastlinedefined = cl->l.p->lastlinedefined;
rlm@1 161 ar->what = (ar->linedefined == 0) ? "main" : "Lua";
rlm@1 162 }
rlm@1 163 luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
rlm@1 164 }
rlm@1 165
rlm@1 166
rlm@1 167 static void info_tailcall (lua_Debug *ar) {
rlm@1 168 ar->name = ar->namewhat = "";
rlm@1 169 ar->what = "tail";
rlm@1 170 ar->lastlinedefined = ar->linedefined = ar->currentline = -1;
rlm@1 171 ar->source = "=(tail call)";
rlm@1 172 luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
rlm@1 173 ar->nups = 0;
rlm@1 174 }
rlm@1 175
rlm@1 176
rlm@1 177 static void collectvalidlines (lua_State *L, Closure *f) {
rlm@1 178 if (f == NULL || f->c.isC) {
rlm@1 179 setnilvalue(L->top);
rlm@1 180 }
rlm@1 181 else {
rlm@1 182 Table *t = luaH_new(L, 0, 0);
rlm@1 183 int *lineinfo = f->l.p->lineinfo;
rlm@1 184 int i;
rlm@1 185 for (i=0; i<f->l.p->sizelineinfo; i++)
rlm@1 186 setbvalue(luaH_setnum(L, t, lineinfo[i]), 1);
rlm@1 187 sethvalue(L, L->top, t);
rlm@1 188 }
rlm@1 189 incr_top(L);
rlm@1 190 }
rlm@1 191
rlm@1 192
rlm@1 193 static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
rlm@1 194 Closure *f, CallInfo *ci) {
rlm@1 195 int status = 1;
rlm@1 196 if (f == NULL) {
rlm@1 197 info_tailcall(ar);
rlm@1 198 return status;
rlm@1 199 }
rlm@1 200 for (; *what; what++) {
rlm@1 201 switch (*what) {
rlm@1 202 case 'S': {
rlm@1 203 funcinfo(ar, f);
rlm@1 204 break;
rlm@1 205 }
rlm@1 206 case 'l': {
rlm@1 207 ar->currentline = (ci) ? currentline(L, ci) : -1;
rlm@1 208 break;
rlm@1 209 }
rlm@1 210 case 'u': {
rlm@1 211 ar->nups = f->c.nupvalues;
rlm@1 212 break;
rlm@1 213 }
rlm@1 214 case 'n': {
rlm@1 215 ar->namewhat = (ci) ? getfuncname(L, ci, &ar->name) : NULL;
rlm@1 216 if (ar->namewhat == NULL) {
rlm@1 217 ar->namewhat = ""; /* not found */
rlm@1 218 ar->name = NULL;
rlm@1 219 }
rlm@1 220 break;
rlm@1 221 }
rlm@1 222 case 'L':
rlm@1 223 case 'f': /* handled by lua_getinfo */
rlm@1 224 break;
rlm@1 225 default: status = 0; /* invalid option */
rlm@1 226 }
rlm@1 227 }
rlm@1 228 return status;
rlm@1 229 }
rlm@1 230
rlm@1 231
rlm@1 232 LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
rlm@1 233 int status;
rlm@1 234 Closure *f = NULL;
rlm@1 235 CallInfo *ci = NULL;
rlm@1 236 lua_lock(L);
rlm@1 237 if (*what == '>') {
rlm@1 238 StkId func = L->top - 1;
rlm@1 239 luai_apicheck(L, ttisfunction(func));
rlm@1 240 what++; /* skip the '>' */
rlm@1 241 f = clvalue(func);
rlm@1 242 L->top--; /* pop function */
rlm@1 243 }
rlm@1 244 else if (ar->i_ci != 0) { /* no tail call? */
rlm@1 245 ci = L->base_ci + ar->i_ci;
rlm@1 246 lua_assert(ttisfunction(ci->func));
rlm@1 247 f = clvalue(ci->func);
rlm@1 248 }
rlm@1 249 status = auxgetinfo(L, what, ar, f, ci);
rlm@1 250 if (strchr(what, 'f')) {
rlm@1 251 if (f == NULL) setnilvalue(L->top);
rlm@1 252 else setclvalue(L, L->top, f);
rlm@1 253 incr_top(L);
rlm@1 254 }
rlm@1 255 if (strchr(what, 'L'))
rlm@1 256 collectvalidlines(L, f);
rlm@1 257 lua_unlock(L);
rlm@1 258 return status;
rlm@1 259 }
rlm@1 260
rlm@1 261
rlm@1 262 /*
rlm@1 263 ** {======================================================
rlm@1 264 ** Symbolic Execution and code checker
rlm@1 265 ** =======================================================
rlm@1 266 */
rlm@1 267
rlm@1 268 #define check(x) if (!(x)) return 0;
rlm@1 269
rlm@1 270 #define checkjump(pt,pc) check(0 <= pc && pc < pt->sizecode)
rlm@1 271
rlm@1 272 #define checkreg(pt,reg) check((reg) < (pt)->maxstacksize)
rlm@1 273
rlm@1 274
rlm@1 275
rlm@1 276 static int precheck (const Proto *pt) {
rlm@1 277 check(pt->maxstacksize <= MAXSTACK);
rlm@1 278 check(pt->numparams+(pt->is_vararg & VARARG_HASARG) <= pt->maxstacksize);
rlm@1 279 check(!(pt->is_vararg & VARARG_NEEDSARG) ||
rlm@1 280 (pt->is_vararg & VARARG_HASARG));
rlm@1 281 check(pt->sizeupvalues <= pt->nups);
rlm@1 282 check(pt->sizelineinfo == pt->sizecode || pt->sizelineinfo == 0);
rlm@1 283 check(pt->sizecode > 0 && GET_OPCODE(pt->code[pt->sizecode-1]) == OP_RETURN);
rlm@1 284 return 1;
rlm@1 285 }
rlm@1 286
rlm@1 287
rlm@1 288 #define checkopenop(pt,pc) luaG_checkopenop((pt)->code[(pc)+1])
rlm@1 289
rlm@1 290 int luaG_checkopenop (Instruction i) {
rlm@1 291 switch (GET_OPCODE(i)) {
rlm@1 292 case OP_CALL:
rlm@1 293 case OP_TAILCALL:
rlm@1 294 case OP_RETURN:
rlm@1 295 case OP_SETLIST: {
rlm@1 296 check(GETARG_B(i) == 0);
rlm@1 297 return 1;
rlm@1 298 }
rlm@1 299 default: return 0; /* invalid instruction after an open call */
rlm@1 300 }
rlm@1 301 }
rlm@1 302
rlm@1 303
rlm@1 304 static int checkArgMode (const Proto *pt, int r, enum OpArgMask mode) {
rlm@1 305 switch (mode) {
rlm@1 306 case OpArgN: check(r == 0); break;
rlm@1 307 case OpArgU: break;
rlm@1 308 case OpArgR: checkreg(pt, r); break;
rlm@1 309 case OpArgK:
rlm@1 310 check(ISK(r) ? INDEXK(r) < pt->sizek : r < pt->maxstacksize);
rlm@1 311 break;
rlm@1 312 }
rlm@1 313 return 1;
rlm@1 314 }
rlm@1 315
rlm@1 316
rlm@1 317 static Instruction symbexec (const Proto *pt, int lastpc, int reg) {
rlm@1 318 int pc;
rlm@1 319 int last; /* stores position of last instruction that changed `reg' */
rlm@1 320 last = pt->sizecode-1; /* points to final return (a `neutral' instruction) */
rlm@1 321 check(precheck(pt));
rlm@1 322 for (pc = 0; pc < lastpc; pc++) {
rlm@1 323 Instruction i = pt->code[pc];
rlm@1 324 OpCode op = GET_OPCODE(i);
rlm@1 325 int a = GETARG_A(i);
rlm@1 326 int b = 0;
rlm@1 327 int c = 0;
rlm@1 328 check(op < NUM_OPCODES);
rlm@1 329 checkreg(pt, a);
rlm@1 330 switch (getOpMode(op)) {
rlm@1 331 case iABC: {
rlm@1 332 b = GETARG_B(i);
rlm@1 333 c = GETARG_C(i);
rlm@1 334 check(checkArgMode(pt, b, getBMode(op)));
rlm@1 335 check(checkArgMode(pt, c, getCMode(op)));
rlm@1 336 break;
rlm@1 337 }
rlm@1 338 case iABx: {
rlm@1 339 b = GETARG_Bx(i);
rlm@1 340 if (getBMode(op) == OpArgK) check(b < pt->sizek);
rlm@1 341 break;
rlm@1 342 }
rlm@1 343 case iAsBx: {
rlm@1 344 b = GETARG_sBx(i);
rlm@1 345 if (getBMode(op) == OpArgR) {
rlm@1 346 int dest = pc+1+b;
rlm@1 347 check(0 <= dest && dest < pt->sizecode);
rlm@1 348 if (dest > 0) {
rlm@1 349 int j;
rlm@1 350 /* check that it does not jump to a setlist count; this
rlm@1 351 is tricky, because the count from a previous setlist may
rlm@1 352 have the same value of an invalid setlist; so, we must
rlm@1 353 go all the way back to the first of them (if any) */
rlm@1 354 for (j = 0; j < dest; j++) {
rlm@1 355 Instruction d = pt->code[dest-1-j];
rlm@1 356 if (!(GET_OPCODE(d) == OP_SETLIST && GETARG_C(d) == 0)) break;
rlm@1 357 }
rlm@1 358 /* if 'j' is even, previous value is not a setlist (even if
rlm@1 359 it looks like one) */
rlm@1 360 check((j&1) == 0);
rlm@1 361 }
rlm@1 362 }
rlm@1 363 break;
rlm@1 364 }
rlm@1 365 }
rlm@1 366 if (testAMode(op)) {
rlm@1 367 if (a == reg) last = pc; /* change register `a' */
rlm@1 368 }
rlm@1 369 if (testTMode(op)) {
rlm@1 370 check(pc+2 < pt->sizecode); /* check skip */
rlm@1 371 check(GET_OPCODE(pt->code[pc+1]) == OP_JMP);
rlm@1 372 }
rlm@1 373 switch (op) {
rlm@1 374 case OP_LOADBOOL: {
rlm@1 375 if (c == 1) { /* does it jump? */
rlm@1 376 check(pc+2 < pt->sizecode); /* check its jump */
rlm@1 377 check(GET_OPCODE(pt->code[pc+1]) != OP_SETLIST ||
rlm@1 378 GETARG_C(pt->code[pc+1]) != 0);
rlm@1 379 }
rlm@1 380 break;
rlm@1 381 }
rlm@1 382 case OP_LOADNIL: {
rlm@1 383 if (a <= reg && reg <= b)
rlm@1 384 last = pc; /* set registers from `a' to `b' */
rlm@1 385 break;
rlm@1 386 }
rlm@1 387 case OP_GETUPVAL:
rlm@1 388 case OP_SETUPVAL: {
rlm@1 389 check(b < pt->nups);
rlm@1 390 break;
rlm@1 391 }
rlm@1 392 case OP_GETGLOBAL:
rlm@1 393 case OP_SETGLOBAL: {
rlm@1 394 check(ttisstring(&pt->k[b]));
rlm@1 395 break;
rlm@1 396 }
rlm@1 397 case OP_SELF: {
rlm@1 398 checkreg(pt, a+1);
rlm@1 399 if (reg == a+1) last = pc;
rlm@1 400 break;
rlm@1 401 }
rlm@1 402 case OP_CONCAT: {
rlm@1 403 check(b < c); /* at least two operands */
rlm@1 404 break;
rlm@1 405 }
rlm@1 406 case OP_TFORLOOP: {
rlm@1 407 check(c >= 1); /* at least one result (control variable) */
rlm@1 408 checkreg(pt, a+2+c); /* space for results */
rlm@1 409 if (reg >= a+2) last = pc; /* affect all regs above its base */
rlm@1 410 break;
rlm@1 411 }
rlm@1 412 case OP_FORLOOP:
rlm@1 413 case OP_FORPREP:
rlm@1 414 checkreg(pt, a+3);
rlm@1 415 /* go through */
rlm@1 416 case OP_JMP: {
rlm@1 417 int dest = pc+1+b;
rlm@1 418 /* not full check and jump is forward and do not skip `lastpc'? */
rlm@1 419 if (reg != NO_REG && pc < dest && dest <= lastpc)
rlm@1 420 pc += b; /* do the jump */
rlm@1 421 break;
rlm@1 422 }
rlm@1 423 case OP_CALL:
rlm@1 424 case OP_TAILCALL: {
rlm@1 425 if (b != 0) {
rlm@1 426 checkreg(pt, a+b-1);
rlm@1 427 }
rlm@1 428 c--; /* c = num. returns */
rlm@1 429 if (c == LUA_MULTRET) {
rlm@1 430 check(checkopenop(pt, pc));
rlm@1 431 }
rlm@1 432 else if (c != 0)
rlm@1 433 checkreg(pt, a+c-1);
rlm@1 434 if (reg >= a) last = pc; /* affect all registers above base */
rlm@1 435 break;
rlm@1 436 }
rlm@1 437 case OP_RETURN: {
rlm@1 438 b--; /* b = num. returns */
rlm@1 439 if (b > 0) checkreg(pt, a+b-1);
rlm@1 440 break;
rlm@1 441 }
rlm@1 442 case OP_SETLIST: {
rlm@1 443 if (b > 0) checkreg(pt, a + b);
rlm@1 444 if (c == 0) {
rlm@1 445 pc++;
rlm@1 446 check(pc < pt->sizecode - 1);
rlm@1 447 }
rlm@1 448 break;
rlm@1 449 }
rlm@1 450 case OP_CLOSURE: {
rlm@1 451 int nup, j;
rlm@1 452 check(b < pt->sizep);
rlm@1 453 nup = pt->p[b]->nups;
rlm@1 454 check(pc + nup < pt->sizecode);
rlm@1 455 for (j = 1; j <= nup; j++) {
rlm@1 456 OpCode op1 = GET_OPCODE(pt->code[pc + j]);
rlm@1 457 check(op1 == OP_GETUPVAL || op1 == OP_MOVE);
rlm@1 458 }
rlm@1 459 if (reg != NO_REG) /* tracing? */
rlm@1 460 pc += nup; /* do not 'execute' these pseudo-instructions */
rlm@1 461 break;
rlm@1 462 }
rlm@1 463 case OP_VARARG: {
rlm@1 464 check((pt->is_vararg & VARARG_ISVARARG) &&
rlm@1 465 !(pt->is_vararg & VARARG_NEEDSARG));
rlm@1 466 b--;
rlm@1 467 if (b == LUA_MULTRET) check(checkopenop(pt, pc));
rlm@1 468 checkreg(pt, a+b-1);
rlm@1 469 break;
rlm@1 470 }
rlm@1 471 default: break;
rlm@1 472 }
rlm@1 473 }
rlm@1 474 return pt->code[last];
rlm@1 475 }
rlm@1 476
rlm@1 477 #undef check
rlm@1 478 #undef checkjump
rlm@1 479 #undef checkreg
rlm@1 480
rlm@1 481 /* }====================================================== */
rlm@1 482
rlm@1 483
rlm@1 484 int luaG_checkcode (const Proto *pt) {
rlm@1 485 return (symbexec(pt, pt->sizecode, NO_REG) != 0);
rlm@1 486 }
rlm@1 487
rlm@1 488
rlm@1 489 static const char *kname (Proto *p, int c) {
rlm@1 490 if (ISK(c) && ttisstring(&p->k[INDEXK(c)]))
rlm@1 491 return svalue(&p->k[INDEXK(c)]);
rlm@1 492 else
rlm@1 493 return "?";
rlm@1 494 }
rlm@1 495
rlm@1 496
rlm@1 497 static const char *getobjname (lua_State *L, CallInfo *ci, int stackpos,
rlm@1 498 const char **name) {
rlm@1 499 if (isLua(ci)) { /* a Lua function? */
rlm@1 500 Proto *p = ci_func(ci)->l.p;
rlm@1 501 int pc = currentpc(L, ci);
rlm@1 502 Instruction i;
rlm@1 503 *name = luaF_getlocalname(p, stackpos+1, pc);
rlm@1 504 if (*name) /* is a local? */
rlm@1 505 return "local";
rlm@1 506 i = symbexec(p, pc, stackpos); /* try symbolic execution */
rlm@1 507 lua_assert(pc != -1);
rlm@1 508 switch (GET_OPCODE(i)) {
rlm@1 509 case OP_GETGLOBAL: {
rlm@1 510 int g = GETARG_Bx(i); /* global index */
rlm@1 511 lua_assert(ttisstring(&p->k[g]));
rlm@1 512 *name = svalue(&p->k[g]);
rlm@1 513 return "global";
rlm@1 514 }
rlm@1 515 case OP_MOVE: {
rlm@1 516 int a = GETARG_A(i);
rlm@1 517 int b = GETARG_B(i); /* move from `b' to `a' */
rlm@1 518 if (b < a)
rlm@1 519 return getobjname(L, ci, b, name); /* get name for `b' */
rlm@1 520 break;
rlm@1 521 }
rlm@1 522 case OP_GETTABLE: {
rlm@1 523 int k = GETARG_C(i); /* key index */
rlm@1 524 *name = kname(p, k);
rlm@1 525 return "field";
rlm@1 526 }
rlm@1 527 case OP_GETUPVAL: {
rlm@1 528 int u = GETARG_B(i); /* upvalue index */
rlm@1 529 *name = p->upvalues ? getstr(p->upvalues[u]) : "?";
rlm@1 530 return "upvalue";
rlm@1 531 }
rlm@1 532 case OP_SELF: {
rlm@1 533 int k = GETARG_C(i); /* key index */
rlm@1 534 *name = kname(p, k);
rlm@1 535 return "method";
rlm@1 536 }
rlm@1 537 default: break;
rlm@1 538 }
rlm@1 539 }
rlm@1 540 return NULL; /* no useful name found */
rlm@1 541 }
rlm@1 542
rlm@1 543
rlm@1 544 static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
rlm@1 545 Instruction i;
rlm@1 546 if ((isLua(ci) && ci->tailcalls > 0) || !isLua(ci - 1))
rlm@1 547 return NULL; /* calling function is not Lua (or is unknown) */
rlm@1 548 ci--; /* calling function */
rlm@1 549 i = ci_func(ci)->l.p->code[currentpc(L, ci)];
rlm@1 550 if (GET_OPCODE(i) == OP_CALL || GET_OPCODE(i) == OP_TAILCALL ||
rlm@1 551 GET_OPCODE(i) == OP_TFORLOOP)
rlm@1 552 return getobjname(L, ci, GETARG_A(i), name);
rlm@1 553 else
rlm@1 554 return NULL; /* no useful name can be found */
rlm@1 555 }
rlm@1 556
rlm@1 557
rlm@1 558 /* only ANSI way to check whether a pointer points to an array */
rlm@1 559 static int isinstack (CallInfo *ci, const TValue *o) {
rlm@1 560 StkId p;
rlm@1 561 for (p = ci->base; p < ci->top; p++)
rlm@1 562 if (o == p) return 1;
rlm@1 563 return 0;
rlm@1 564 }
rlm@1 565
rlm@1 566
rlm@1 567 void luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
rlm@1 568 const char *name = NULL;
rlm@1 569 const char *t = luaT_typenames[ttype(o)];
rlm@1 570 const char *kind = (isinstack(L->ci, o)) ?
rlm@1 571 getobjname(L, L->ci, cast_int(o - L->base), &name) :
rlm@1 572 NULL;
rlm@1 573 if (kind)
rlm@1 574 luaG_runerror(L, "attempt to %s %s " LUA_QS " (a %s value)",
rlm@1 575 op, kind, name, t);
rlm@1 576 else
rlm@1 577 luaG_runerror(L, "attempt to %s a %s value", op, t);
rlm@1 578 }
rlm@1 579
rlm@1 580
rlm@1 581 void luaG_concaterror (lua_State *L, StkId p1, StkId p2) {
rlm@1 582 if (ttisstring(p1) || ttisnumber(p1)) p1 = p2;
rlm@1 583 lua_assert(!ttisstring(p1) && !ttisnumber(p1));
rlm@1 584 luaG_typeerror(L, p1, "concatenate");
rlm@1 585 }
rlm@1 586
rlm@1 587
rlm@1 588 void luaG_aritherror (lua_State *L, const TValue *p1, const TValue *p2) {
rlm@1 589 TValue temp;
rlm@1 590 if (luaV_tonumber(p1, &temp) == NULL)
rlm@1 591 p2 = p1; /* first operand is wrong */
rlm@1 592 luaG_typeerror(L, p2, "perform arithmetic on");
rlm@1 593 }
rlm@1 594
rlm@1 595
rlm@1 596 int luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
rlm@1 597 const char *t1 = luaT_typenames[ttype(p1)];
rlm@1 598 const char *t2 = luaT_typenames[ttype(p2)];
rlm@1 599 if (t1[2] == t2[2])
rlm@1 600 luaG_runerror(L, "attempt to compare two %s values", t1);
rlm@1 601 else
rlm@1 602 luaG_runerror(L, "attempt to compare %s with %s", t1, t2);
rlm@1 603 return 0;
rlm@1 604 }
rlm@1 605
rlm@1 606
rlm@1 607 static void addinfo (lua_State *L, const char *msg) {
rlm@1 608 CallInfo *ci = L->ci;
rlm@1 609 if (isLua(ci)) { /* is Lua code? */
rlm@1 610 char buff[LUA_IDSIZE]; /* add file:line information */
rlm@1 611 int line = currentline(L, ci);
rlm@1 612 luaO_chunkid(buff, getstr(getluaproto(ci)->source), LUA_IDSIZE);
rlm@1 613 luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);
rlm@1 614 }
rlm@1 615 }
rlm@1 616
rlm@1 617
rlm@1 618 void luaG_errormsg (lua_State *L) {
rlm@1 619 if (L->errfunc != 0) { /* is there an error handling function? */
rlm@1 620 StkId errfunc = restorestack(L, L->errfunc);
rlm@1 621 if (!ttisfunction(errfunc)) luaD_throw(L, LUA_ERRERR);
rlm@1 622 setobjs2s(L, L->top, L->top - 1); /* move argument */
rlm@1 623 setobjs2s(L, L->top - 1, errfunc); /* push function */
rlm@1 624 incr_top(L);
rlm@1 625 luaD_call(L, L->top - 2, 1); /* call it */
rlm@1 626 }
rlm@1 627 luaD_throw(L, LUA_ERRRUN);
rlm@1 628 }
rlm@1 629
rlm@1 630
rlm@1 631 void luaG_runerror (lua_State *L, const char *fmt, ...) {
rlm@1 632 va_list argp;
rlm@1 633 va_start(argp, fmt);
rlm@1 634 addinfo(L, luaO_pushvfstring(L, fmt, argp));
rlm@1 635 va_end(argp);
rlm@1 636 luaG_errormsg(L);
rlm@1 637 }
rlm@1 638