34#define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_VCCL)
42 lua_assert(isLua(ci));
43 return pcRel(ci->u.l.savedpc, ci_func(ci)->p);
60static int getbaseline (
const Proto *f,
int pc,
int *basepc) {
61 if (f->sizeabslineinfo == 0 || pc < f->abslineinfo[0].pc) {
63 return f->linedefined;
66 int i = cast_uint(pc) / MAXIWTHABS - 1;
69 (i < f->sizeabslineinfo && f->abslineinfo[i].pc <= pc));
70 while (i + 1 < f->sizeabslineinfo && pc >= f->abslineinfo[i + 1].pc)
72 *basepc = f->abslineinfo[i].pc;
73 return f->abslineinfo[i].line;
83int luaG_getfuncline (
const Proto *f,
int pc) {
84 if (f->lineinfo == NULL)
88 int baseline = getbaseline(f, pc, &basepc);
89 while (basepc++ < pc) {
90 lua_assert(f->lineinfo[basepc] != ABSLINEINFO);
91 baseline += f->lineinfo[basepc];
98static int getcurrentline (
CallInfo *ci) {
99 return luaG_getfuncline(ci_func(ci)->p, currentpc(ci));
114static void settraps (
CallInfo *ci) {
115 for (; ci != NULL; ci = ci->previous)
131LUA_API
void lua_sethook (
lua_State *L, lua_Hook func,
int mask,
int count) {
132 if (func == NULL || mask == 0) {
137 L->basehookcount = count;
139 L->hookmask = cast_byte(mask);
145LUA_API lua_Hook lua_gethook (
lua_State *L) {
150LUA_API
int lua_gethookmask (
lua_State *L) {
155LUA_API
int lua_gethookcount (
lua_State *L) {
156 return L->basehookcount;
163 if (level < 0)
return 0;
165 for (ci = L->ci; level > 0 && ci != &L->base_ci; ci = ci->previous)
167 if (level == 0 && ci != &L->base_ci) {
177static const char *upvalname (
const Proto *p,
int uv) {
178 TString *s = check_exp(uv < p->sizeupvalues, p->upvalues[uv].name);
179 if (s == NULL)
return "?";
180 else return getstr(s);
184static const char *findvararg (
CallInfo *ci,
int n, StkId *pos) {
185 if (clLvalue(s2v(ci->func.p))->p->is_vararg) {
186 int nextra = ci->u.l.nextraargs;
188 *pos = ci->func.p - nextra - (n + 1);
197 StkId base = ci->func.p + 1;
198 const char *name = NULL;
201 return findvararg(ci, n, pos);
203 name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci));
206 StkId limit = (ci == L->ci) ? L->top.p : ci->next->func.p;
207 if (limit - base >= n && n > 0) {
209 name = isLua(ci) ?
"(temporary)" :
"(C temporary)";
215 *pos = base + (n - 1);
224 if (!isLfunction(s2v(L->top.p - 1)))
227 name = luaF_getlocalname(clLvalue(s2v(L->top.p - 1))->p, n, 0);
231 name = luaG_findlocal(L, ar->i_ci, n, &pos);
233 setobjs2s(L, L->top.p, pos);
246 name = luaG_findlocal(L, ar->i_ci, n, &pos);
248 setobjs2s(L, pos, L->top.p - 1);
257 if (noLuaClosure(cl)) {
259 ar->srclen = LL(
"=[C]");
260 ar->linedefined = -1;
261 ar->lastlinedefined = -1;
265 const Proto *p = cl->l.p;
267 ar->source = getstr(p->source);
268 ar->srclen = tsslen(p->source);
272 ar->srclen = LL(
"=?");
274 ar->linedefined = p->linedefined;
275 ar->lastlinedefined = p->lastlinedefined;
276 ar->what = (ar->linedefined == 0) ?
"main" :
"Lua";
278 luaO_chunkid(ar->short_src, ar->source, ar->srclen);
282static int nextline (
const Proto *p,
int currentline,
int pc) {
283 if (p->lineinfo[pc] != ABSLINEINFO)
284 return currentline + p->lineinfo[pc];
286 return luaG_getfuncline(p, pc);
291 if (noLuaClosure(f)) {
292 setnilvalue(s2v(L->top.p));
298 const Proto *p = f->l.p;
299 int currentline = p->linedefined;
300 Table *t = luaH_new(L);
301 sethvalue2s(L, L->top.p, t);
307 lua_assert(GET_OPCODE(p->code[0]) == OP_VARARGPREP);
308 currentline = nextline(p, currentline, 0);
311 for (; i < p->sizelineinfo; i++) {
312 currentline = nextline(p, currentline, i);
313 luaH_setint(L, t, currentline, &v);
321 if (ci != NULL && !(ci->callstatus & CIST_TAIL))
322 return funcnamefromcall(L, ci->previous, name);
330 for (; *what; what++) {
337 ar->currentline = (ci && isLua(ci)) ? getcurrentline(ci) : -1;
341 ar->nups = (f == NULL) ? 0 : f->c.nupvalues;
342 if (noLuaClosure(f)) {
347 ar->isvararg = f->l.p->is_vararg;
348 ar->nparams = f->l.p->numparams;
353 ar->istailcall = (ci) ? ci->callstatus & CIST_TAIL : 0;
357 ar->namewhat = getfuncname(L, ci, &ar->name);
358 if (ar->namewhat == NULL) {
365 if (ci == NULL || !(ci->callstatus & CIST_TRAN))
366 ar->ftransfer = ar->ntransfer = 0;
368 ar->ftransfer = ci->u2.transferinfo.ftransfer;
369 ar->ntransfer = ci->u2.transferinfo.ntransfer;
391 func = s2v(L->top.p - 1);
392 api_check(L, ttisfunction(func),
"function expected");
398 func = s2v(ci->func.p);
399 lua_assert(ttisfunction(func));
401 cl = ttisclosure(func) ? clvalue(func) : NULL;
402 status = auxgetinfo(L, what, ar, cl, ci);
403 if (strchr(what,
'f')) {
404 setobj2s(L, L->top.p, func);
407 if (strchr(what,
'L'))
408 collectvalidlines(L, cl);
420static const char *getobjname (
const Proto *p,
int lastpc,
int reg,
427static void kname (
const Proto *p,
int c,
const char **name) {
428 TValue *kvalue = &p->k[c];
429 *name = (ttisstring(kvalue)) ? svalue(kvalue) :
"?";
436static void rname (
const Proto *p,
int pc,
int c,
const char **name) {
437 const char *what = getobjname(p, pc, c, name);
438 if (!(what && *what ==
'c'))
446static void rkname (
const Proto *p,
int pc, Instruction i,
const char **name) {
451 rname(p, pc, c, name);
455static int filterpc (
int pc,
int jmptarget) {
465static int findsetreg (
const Proto *p,
int lastpc,
int reg) {
469 if (testMMMode(GET_OPCODE(p->code[lastpc])))
471 for (pc = 0; pc < lastpc; pc++) {
472 Instruction i = p->code[pc];
473 OpCode op = GET_OPCODE(i);
479 change = (a <= reg && reg <= a + b);
483 change = (reg >= a + 2);
492 int b = GETARG_sJ(i);
493 int dest = pc + 1 + b;
495 if (dest <= lastpc && dest > jmptarget)
501 change = (testAMode(op) && reg == a);
505 setreg = filterpc(pc, jmptarget);
515static const char *gxf (
const Proto *p,
int pc, Instruction i,
int isup) {
519 name = upvalname(p, t);
521 getobjname(p, pc, t, &name);
522 return (name && strcmp(name, LUA_ENV) == 0) ?
"global" :
"field";
526static const char *getobjname (
const Proto *p,
int lastpc,
int reg,
529 *name = luaF_getlocalname(p, reg + 1, lastpc);
533 pc = findsetreg(p, lastpc, reg);
535 Instruction i = p->code[pc];
536 OpCode op = GET_OPCODE(i);
541 return getobjname(p, pc, b, name);
547 return gxf(p, pc, i, 1);
551 rname(p, pc, k, name);
552 return gxf(p, pc, i, 0);
555 *name =
"integer index";
561 return gxf(p, pc, i, 0);
564 *name = upvalname(p, GETARG_B(i));
569 int b = (op == OP_LOADK) ? GETARG_Bx(i)
570 : GETARG_Ax(p->code[pc + 1]);
571 if (ttisstring(&p->k[b])) {
572 *name = svalue(&p->k[b]);
578 rkname(p, pc, i, name);
595 int pc,
const char **name) {
597 Instruction i = p->code[pc];
598 switch (GET_OPCODE(i)) {
601 return getobjname(p, pc, GETARG_A(i), name);
603 *name =
"for iterator";
604 return "for iterator";
607 case OP_SELF:
case OP_GETTABUP:
case OP_GETTABLE:
608 case OP_GETI:
case OP_GETFIELD:
611 case OP_SETTABUP:
case OP_SETTABLE:
case OP_SETI:
case OP_SETFIELD:
614 case OP_MMBIN:
case OP_MMBINI:
case OP_MMBINK: {
615 tm = cast(TMS, GETARG_C(i));
618 case OP_UNM: tm = TM_UNM;
break;
619 case OP_BNOT: tm = TM_BNOT;
break;
620 case OP_LEN: tm = TM_LEN;
break;
621 case OP_CONCAT: tm = TM_CONCAT;
break;
622 case OP_EQ: tm = TM_EQ;
break;
624 case OP_LT:
case OP_LTI:
case OP_GTI: tm = TM_LT;
break;
625 case OP_LE:
case OP_LEI:
case OP_GEI: tm = TM_LE;
break;
626 case OP_CLOSE:
case OP_RETURN: tm = TM_CLOSE;
break;
630 *name = getstr(G(L)->tmname[tm]) + 2;
640 if (ci->callstatus & CIST_HOOKED) {
644 else if (ci->callstatus & CIST_FIN) {
649 return funcnamefromcode(L, ci_func(ci)->p, currentpc(ci), name);
666 StkId base = ci->func.p + 1;
667 for (pos = 0; base + pos < ci->top.p; pos++) {
668 if (o == s2v(base + pos))
684 for (i = 0; i < c->nupvalues; i++) {
685 if (c->upvals[i]->v.p == o) {
686 *name = upvalname(c->p, i);
694static const char *formatvarinfo (
lua_State *L,
const char *kind,
699 return luaO_pushfstring(L,
" (%s '%s')", kind, name);
708 const char *name = NULL;
709 const char *kind = NULL;
711 kind = getupvalname(ci, o, &name);
713 int reg = instack(ci, o);
715 kind = getobjname(ci_func(ci)->p, currentpc(ci), reg, &name);
718 return formatvarinfo(L, kind, name);
725static l_noret typeerror (
lua_State *L,
const TValue *o,
const char *op,
727 const char *t = luaT_objtypename(L, o);
728 luaG_runerror(L,
"attempt to %s a %s value%s", op, t, extra);
736l_noret luaG_typeerror (
lua_State *L,
const TValue *o,
const char *op) {
737 typeerror(L, o, op, varinfo(L, o));
748 const char *name = NULL;
749 const char *kind = funcnamefromcall(L, ci, &name);
750 const char *extra = kind ? formatvarinfo(L, kind, name) : varinfo(L, o);
751 typeerror(L, o,
"call", extra);
755l_noret luaG_forerror (
lua_State *L,
const TValue *o,
const char *what) {
756 luaG_runerror(L,
"bad 'for' %s (number expected, got %s)",
757 what, luaT_objtypename(L, o));
762 if (ttisstring(p1) || cvt2str(p1)) p1 = p2;
763 luaG_typeerror(L, p1,
"concatenate");
768 const TValue *p2,
const char *msg) {
771 luaG_typeerror(L, p2, msg);
780 if (!luaV_tointegerns(p1, &temp, LUA_FLOORN2I))
782 luaG_runerror(L,
"number%s has no integer representation", varinfo(L, p2));
787 const char *t1 = luaT_objtypename(L, p1);
788 const char *t2 = luaT_objtypename(L, p2);
789 if (strcmp(t1, t2) == 0)
790 luaG_runerror(L,
"attempt to compare two %s values", t1);
792 luaG_runerror(L,
"attempt to compare %s with %s", t1, t2);
799 char buff[LUA_IDSIZE];
801 luaO_chunkid(buff, getstr(src), tsslen(src));
803 buff[0] =
'?'; buff[1] =
'\0';
805 return luaO_pushfstring(L,
"%s:%d: %s", buff, line, msg);
810 if (L->errfunc != 0) {
811 StkId errfunc = restorestack(L, L->errfunc);
812 lua_assert(ttisfunction(s2v(errfunc)));
813 setobjs2s(L, L->top.p, L->top.p - 1);
814 setobjs2s(L, L->top.p - 1, errfunc);
816 luaD_callnoyield(L, L->top.p - 2, 1);
818 luaD_throw(L, LUA_ERRRUN);
822l_noret luaG_runerror (
lua_State *L,
const char *fmt, ...) {
828 msg = luaO_pushvfstring(L, fmt, argp);
831 luaG_addinfo(L, msg, ci_func(ci)->p->source, getcurrentline(ci));
832 setobjs2s(L, L->top.p - 2, L->top.p - 1);
847static int changedline (
const Proto *p,
int oldpc,
int newpc) {
848 if (p->lineinfo == NULL)
850 if (newpc - oldpc < MAXIWTHABS / 2) {
854 int lineinfo = p->lineinfo[++pc];
855 if (lineinfo == ABSLINEINFO)
864 return (luaG_getfuncline(p, oldpc) != luaG_getfuncline(p, newpc));
880int luaG_traceexec (
lua_State *L,
const Instruction *pc) {
882 lu_byte mask = L->hookmask;
883 const Proto *p = ci_func(ci)->p;
885 if (!(mask & (LUA_MASKLINE | LUA_MASKCOUNT))) {
890 ci->u.l.savedpc = pc;
891 counthook = (--L->hookcount == 0 && (mask & LUA_MASKCOUNT));
894 else if (!(mask & LUA_MASKLINE))
896 if (ci->callstatus & CIST_HOOKYIELD) {
897 ci->callstatus &= ~CIST_HOOKYIELD;
900 if (!isIT(*(ci->u.l.savedpc - 1)))
901 L->top.p = ci->top.p;
903 luaD_hook(L, LUA_HOOKCOUNT, -1, 0, 0);
904 if (mask & LUA_MASKLINE) {
906 int oldpc = (L->oldpc < p->sizecode) ? L->oldpc : 0;
907 int npci = pcRel(pc, p);
909 changedline(p, oldpc, npci)) {
910 int newline = luaG_getfuncline(p, npci);
911 luaD_hook(L, LUA_HOOKLINE, newline, 0, 0);
915 if (L->status == LUA_YIELD) {
919 ci->callstatus |= CIST_HOOKYIELD;
920 luaD_throw(L, LUA_YIELD);