ldebug.c 19 KB

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