Fix forced unwinding triggered by external unwinder.
[luajit-2.0.git] / src / lj_err.c
blob0c495d43710911ea111805688185f35e2136d26e
1 /*
2 ** Error handling.
3 ** Copyright (C) 2005-2011 Mike Pall. See Copyright Notice in luajit.h
4 */
6 #define lj_err_c
7 #define LUA_CORE
9 #include "lj_obj.h"
10 #include "lj_err.h"
11 #include "lj_debug.h"
12 #include "lj_str.h"
13 #include "lj_func.h"
14 #include "lj_state.h"
15 #include "lj_frame.h"
16 #include "lj_ff.h"
17 #include "lj_trace.h"
18 #include "lj_vm.h"
21 ** LuaJIT can either use internal or external frame unwinding:
23 ** - Internal frame unwinding (INT) is free-standing and doesn't require
24 ** any OS or library support.
26 ** - External frame unwinding (EXT) uses the system-provided unwind handler.
28 ** Pros and Cons:
30 ** - EXT requires unwind tables for *all* functions on the C stack between
31 ** the pcall/catch and the error/throw. This is the default on x64,
32 ** but needs to be manually enabled on x86/PPC for non-C++ code.
34 ** - INT is faster when actually throwing errors (but this happens rarely).
35 ** Setting up error handlers is zero-cost in any case.
37 ** - EXT provides full interoperability with C++ exceptions. You can throw
38 ** Lua errors or C++ exceptions through a mix of Lua frames and C++ frames.
39 ** C++ destructors are called as needed. C++ exceptions caught by pcall
40 ** are converted to the string "C++ exception". Lua errors can be caught
41 ** with catch (...) in C++.
43 ** - INT has only limited support for automatically catching C++ exceptions
44 ** on POSIX systems using DWARF2 stack unwinding. Other systems may use
45 ** the wrapper function feature. Lua errors thrown through C++ frames
46 ** cannot be caught by C++ code and C++ destructors are not run.
48 ** EXT is the default on x64 systems, INT is the default on all other systems.
50 ** EXT can be manually enabled on POSIX systems using GCC and DWARF2 stack
51 ** unwinding with -DLUAJIT_UNWIND_EXTERNAL. *All* C code must be compiled
52 ** with -funwind-tables (or -fexceptions). This includes LuaJIT itself (set
53 ** TARGET_CFLAGS), all of your C/Lua binding code, all loadable C modules
54 ** and all C libraries that have callbacks which may be used to call back
55 ** into Lua. C++ code must *not* be compiled with -fno-exceptions.
57 ** EXT cannot be enabled on WIN32 since system exceptions use code-driven SEH.
58 ** EXT is mandatory on WIN64 since the calling convention has an abundance
59 ** of callee-saved registers (rbx, rbp, rsi, rdi, r12-r15, xmm6-xmm15).
60 ** EXT is mandatory on POSIX/x64 since the interpreter doesn't save r12/r13.
63 #if defined(__GNUC__) && (LJ_TARGET_X64 || defined(LUAJIT_UNWIND_EXTERNAL))
64 #define LJ_UNWIND_EXT 1
65 #elif LJ_TARGET_X64 && LJ_TARGET_WINDOWS
66 #define LJ_UNWIND_EXT 1
67 #endif
69 /* -- Error messages ------------------------------------------------------ */
71 /* Error message strings. */
72 LJ_DATADEF const char *lj_err_allmsg =
73 #define ERRDEF(name, msg) msg "\0"
74 #include "lj_errmsg.h"
77 /* -- Internal frame unwinding -------------------------------------------- */
79 /* Unwind Lua stack and move error message to new top. */
80 LJ_NOINLINE static void unwindstack(lua_State *L, TValue *top)
82 lj_func_closeuv(L, top);
83 if (top < L->top-1) {
84 copyTV(L, top, L->top-1);
85 L->top = top+1;
87 lj_state_relimitstack(L);
90 /* Unwind until stop frame. Optionally cleanup frames. */
91 static void *err_unwind(lua_State *L, void *stopcf, int errcode)
93 TValue *frame = L->base-1;
94 void *cf = L->cframe;
95 while (cf) {
96 int32_t nres = cframe_nres(cframe_raw(cf));
97 if (nres < 0) { /* C frame without Lua frame? */
98 TValue *top = restorestack(L, -nres);
99 if (frame < top) { /* Frame reached? */
100 if (errcode) {
101 L->cframe = cframe_prev(cf);
102 L->base = frame+1;
103 unwindstack(L, top);
105 return cf;
108 if (frame <= tvref(L->stack))
109 break;
110 switch (frame_typep(frame)) {
111 case FRAME_LUA: /* Lua frame. */
112 case FRAME_LUAP:
113 frame = frame_prevl(frame);
114 break;
115 case FRAME_C: /* C frame. */
116 #if LJ_UNWIND_EXT
117 if (errcode) {
118 L->cframe = cframe_prev(cf);
119 L->base = frame_prevd(frame) + 1;
120 unwindstack(L, frame);
121 } else if (cf != stopcf) {
122 cf = cframe_prev(cf);
123 frame = frame_prevd(frame);
124 break;
126 return NULL; /* Continue unwinding. */
127 #else
128 UNUSED(stopcf);
129 cf = cframe_prev(cf);
130 frame = frame_prevd(frame);
131 break;
132 #endif
133 case FRAME_CP: /* Protected C frame. */
134 if (cframe_canyield(cf)) { /* Resume? */
135 if (errcode) {
136 L->cframe = NULL;
137 L->status = (uint8_t)errcode;
139 return cf;
141 if (errcode) {
142 L->cframe = cframe_prev(cf);
143 L->base = frame_prevd(frame) + 1;
144 unwindstack(L, frame);
146 return cf;
147 case FRAME_CONT: /* Continuation frame. */
148 case FRAME_VARG: /* Vararg frame. */
149 frame = frame_prevd(frame);
150 break;
151 case FRAME_PCALL: /* FF pcall() frame. */
152 case FRAME_PCALLH: /* FF pcall() frame inside hook. */
153 if (errcode) {
154 if (errcode == LUA_YIELD) {
155 frame = frame_prevd(frame);
156 break;
158 if (frame_typep(frame) == FRAME_PCALL)
159 hook_leave(G(L));
160 L->cframe = cf;
161 L->base = frame_prevd(frame) + 1;
162 unwindstack(L, L->base);
164 return (void *)((intptr_t)cf | CFRAME_UNWIND_FF);
167 /* No C frame. */
168 if (errcode) {
169 L->cframe = NULL;
170 L->base = tvref(L->stack)+1;
171 unwindstack(L, L->base);
172 if (G(L)->panic)
173 G(L)->panic(L);
174 exit(EXIT_FAILURE);
176 return L; /* Anything non-NULL will do. */
179 /* -- External frame unwinding -------------------------------------------- */
181 #if defined(__GNUC__) && !defined(__symbian__) && \
182 !(LJ_TARGET_ARM && LJ_TARGET_OSX)
184 #ifdef __clang__
185 /* http://llvm.org/bugs/show_bug.cgi?id=8703 */
186 #define __unwind_word__ word
187 #endif
189 #include <unwind.h>
191 #if !LJ_TARGET_ARM
193 #define LJ_UEXCLASS 0x4c55414a49543200ULL /* LUAJIT2\0 */
194 #define LJ_UEXCLASS_MAKE(c) (LJ_UEXCLASS | (_Unwind_Exception_Class)(c))
195 #define LJ_UEXCLASS_CHECK(cl) (((cl) ^ LJ_UEXCLASS) <= 0xff)
196 #define LJ_UEXCLASS_ERRCODE(cl) ((int)((cl) & 0xff))
198 /* DWARF2 personality handler referenced from interpreter .eh_frame. */
199 LJ_FUNCA int lj_err_unwind_dwarf(int version, _Unwind_Action actions,
200 _Unwind_Exception_Class uexclass, struct _Unwind_Exception *uex,
201 struct _Unwind_Context *ctx)
203 void *cf;
204 lua_State *L;
205 if (version != 1)
206 return _URC_FATAL_PHASE1_ERROR;
207 UNUSED(uexclass);
208 cf = (void *)_Unwind_GetCFA(ctx);
209 L = cframe_L(cf);
210 if ((actions & _UA_SEARCH_PHASE)) {
211 #if LJ_UNWIND_EXT
212 if (err_unwind(L, cf, 0) == NULL)
213 return _URC_CONTINUE_UNWIND;
214 #endif
215 if (!LJ_UEXCLASS_CHECK(uexclass)) {
216 setstrV(L, L->top++, lj_err_str(L, LJ_ERR_ERRCPP));
218 return _URC_HANDLER_FOUND;
220 if ((actions & _UA_CLEANUP_PHASE)) {
221 int errcode;
222 if (LJ_UEXCLASS_CHECK(uexclass)) {
223 errcode = LJ_UEXCLASS_ERRCODE(uexclass);
224 } else {
225 if ((actions & _UA_HANDLER_FRAME))
226 _Unwind_DeleteException(uex);
227 errcode = LUA_ERRRUN;
229 #if LJ_UNWIND_EXT
230 cf = err_unwind(L, cf, errcode);
231 if ((actions & _UA_FORCE_UNWIND)) {
232 return _URC_CONTINUE_UNWIND;
233 } else if (cf) {
234 _Unwind_SetGR(ctx, LJ_TARGET_EHRETREG, errcode);
235 _Unwind_SetIP(ctx, (_Unwind_Ptr)(cframe_unwind_ff(cf) ?
236 lj_vm_unwind_ff_eh :
237 lj_vm_unwind_c_eh));
238 return _URC_INSTALL_CONTEXT;
240 #if LJ_TARGET_X86ORX64
241 else if ((actions & _UA_HANDLER_FRAME)) {
242 /* Workaround for ancient libgcc bug. Still present in RHEL 5.5. :-/
243 ** Real fix: http://gcc.gnu.org/viewcvs/trunk/gcc/unwind-dw2.c?r1=121165&r2=124837&pathrev=153877&diff_format=h
245 _Unwind_SetGR(ctx, LJ_TARGET_EHRETREG, errcode);
246 _Unwind_SetIP(ctx, (_Unwind_Ptr)lj_vm_unwind_rethrow);
247 return _URC_INSTALL_CONTEXT;
249 #endif
250 #else
251 /* This is not the proper way to escape from the unwinder. We get away with
252 ** it on x86/PPC because the interpreter restores all callee-saved regs.
254 lj_err_throw(L, errcode);
255 #endif
257 return _URC_CONTINUE_UNWIND;
260 #if LJ_UNWIND_EXT
261 static __thread struct _Unwind_Exception static_uex;
263 /* Raise DWARF2 exception. */
264 static void err_raise_ext(int errcode)
266 static_uex.exception_class = LJ_UEXCLASS_MAKE(errcode);
267 static_uex.exception_cleanup = NULL;
268 _Unwind_RaiseException(&static_uex);
270 #endif
272 #else
274 /* ARM unwinder personality handler referenced from interpreter .ARM.extab. */
275 LJ_FUNCA _Unwind_Reason_Code lj_err_unwind_arm(_Unwind_State state,
276 _Unwind_Control_Block *ucb,
277 _Unwind_Context *ctx)
279 void *cf = (void *)_Unwind_GetGR(ctx, 13);
280 lua_State *L = cframe_L(cf);
281 if ((state & _US_ACTION_MASK) == _US_VIRTUAL_UNWIND_FRAME) {
282 setstrV(L, L->top++, lj_err_str(L, LJ_ERR_ERRCPP));
283 return _URC_HANDLER_FOUND;
285 if ((state&(_US_ACTION_MASK|_US_FORCE_UNWIND)) == _US_UNWIND_FRAME_STARTING) {
286 _Unwind_DeleteException(ucb);
287 _Unwind_SetGR(ctx, 15, (_Unwind_Word)(void *)lj_err_throw);
288 _Unwind_SetGR(ctx, 0, (_Unwind_Word)L);
289 _Unwind_SetGR(ctx, 1, (_Unwind_Word)LUA_ERRRUN);
290 return _URC_INSTALL_CONTEXT;
292 if (__gnu_unwind_frame(ucb, ctx) != _URC_OK)
293 return _URC_FAILURE;
294 return _URC_CONTINUE_UNWIND;
297 #endif
299 #elif LJ_TARGET_X64 && LJ_TARGET_WINDOWS
302 ** Someone in Redmond owes me several days of my life. A lot of this is
303 ** undocumented or just plain wrong on MSDN. Some of it can be gathered
304 ** from 3rd party docs or must be found by trial-and-error. They really
305 ** don't want you to write your own language-specific exception handler
306 ** or to interact gracefully with MSVC. :-(
308 ** Apparently MSVC doesn't call C++ destructors for foreign exceptions
309 ** unless you compile your C++ code with /EHa. Unfortunately this means
310 ** catch (...) also catches things like access violations. The use of
311 ** _set_se_translator doesn't really help, because it requires /EHa, too.
314 #define WIN32_LEAN_AND_MEAN
315 #include <windows.h>
317 /* Taken from: http://www.nynaeve.net/?p=99 */
318 typedef struct UndocumentedDispatcherContext {
319 ULONG64 ControlPc;
320 ULONG64 ImageBase;
321 PRUNTIME_FUNCTION FunctionEntry;
322 ULONG64 EstablisherFrame;
323 ULONG64 TargetIp;
324 PCONTEXT ContextRecord;
325 PEXCEPTION_ROUTINE LanguageHandler;
326 PVOID HandlerData;
327 PUNWIND_HISTORY_TABLE HistoryTable;
328 ULONG ScopeIndex;
329 ULONG Fill0;
330 } UndocumentedDispatcherContext;
332 #ifdef _MSC_VER
333 /* Another wild guess. */
334 extern __DestructExceptionObject(EXCEPTION_RECORD *rec, int nothrow);
335 #endif
337 #define LJ_MSVC_EXCODE ((DWORD)0xe06d7363)
339 #define LJ_EXCODE ((DWORD)0xe24c4a00)
340 #define LJ_EXCODE_MAKE(c) (LJ_EXCODE | (DWORD)(c))
341 #define LJ_EXCODE_CHECK(cl) (((cl) ^ LJ_EXCODE) <= 0xff)
342 #define LJ_EXCODE_ERRCODE(cl) ((int)((cl) & 0xff))
344 /* Win64 exception handler for interpreter frame. */
345 LJ_FUNCA EXCEPTION_DISPOSITION lj_err_unwind_win64(EXCEPTION_RECORD *rec,
346 void *cf, CONTEXT *ctx, UndocumentedDispatcherContext *dispatch)
348 lua_State *L = cframe_L(cf);
349 int errcode = LJ_EXCODE_CHECK(rec->ExceptionCode) ?
350 LJ_EXCODE_ERRCODE(rec->ExceptionCode) : LUA_ERRRUN;
351 if ((rec->ExceptionFlags & 6)) { /* EH_UNWINDING|EH_EXIT_UNWIND */
352 /* Unwind internal frames. */
353 err_unwind(L, cf, errcode);
354 } else {
355 void *cf2 = err_unwind(L, cf, 0);
356 if (cf2) { /* We catch it, so start unwinding the upper frames. */
357 if (rec->ExceptionCode == LJ_MSVC_EXCODE) {
358 #ifdef _MSC_VER
359 __DestructExceptionObject(rec, 1);
360 #endif
361 setstrV(L, L->top++, lj_err_str(L, LJ_ERR_ERRCPP));
362 } else if (!LJ_EXCODE_CHECK(rec->ExceptionCode)) {
363 /* Don't catch access violations etc. */
364 return ExceptionContinueSearch;
366 /* Unwind the stack and call all handlers for all lower C frames
367 ** (including ourselves) again with EH_UNWINDING set. Then set
368 ** rsp = cf, rax = errcode and jump to the specified target.
370 RtlUnwindEx(cf, (void *)((cframe_unwind_ff(cf2) && errcode != LUA_YIELD) ?
371 lj_vm_unwind_ff_eh :
372 lj_vm_unwind_c_eh),
373 rec, (void *)errcode, ctx, dispatch->HistoryTable);
374 /* RtlUnwindEx should never return. */
377 return ExceptionContinueSearch;
380 /* Raise Windows exception. */
381 static void err_raise_ext(int errcode)
383 RaiseException(LJ_EXCODE_MAKE(errcode), 1 /* EH_NONCONTINUABLE */, 0, NULL);
386 #endif
388 /* -- Error handling ------------------------------------------------------ */
390 /* Throw error. Find catch frame, unwind stack and continue. */
391 LJ_NOINLINE void LJ_FASTCALL lj_err_throw(lua_State *L, int errcode)
393 global_State *g = G(L);
394 lj_trace_abort(g);
395 setgcrefnull(g->jit_L);
396 L->status = 0;
397 #if LJ_UNWIND_EXT
398 err_raise_ext(errcode);
400 ** A return from this function signals a corrupt C stack that cannot be
401 ** unwound. We have no choice but to call the panic function and exit.
403 ** Usually this is caused by a C function without unwind information.
404 ** This should never happen on x64, but may happen if you've manually
405 ** enabled LUAJIT_UNWIND_EXTERNAL and forgot to recompile *every*
406 ** non-C++ file with -funwind-tables.
408 if (G(L)->panic)
409 G(L)->panic(L);
410 #else
412 void *cf = err_unwind(L, NULL, errcode);
413 if (cframe_unwind_ff(cf))
414 lj_vm_unwind_ff(cframe_raw(cf));
415 else
416 lj_vm_unwind_c(cframe_raw(cf), errcode);
418 #endif
419 exit(EXIT_FAILURE);
422 /* Return string object for error message. */
423 LJ_NOINLINE GCstr *lj_err_str(lua_State *L, ErrMsg em)
425 return lj_str_newz(L, err2msg(em));
428 /* Out-of-memory error. */
429 LJ_NOINLINE void lj_err_mem(lua_State *L)
431 if (L->status == LUA_ERRERR+1) /* Don't touch the stack during lua_open. */
432 lj_vm_unwind_c(L->cframe, LUA_ERRMEM);
433 L->top = L->base;
434 setstrV(L, L->top++, lj_err_str(L, LJ_ERR_ERRMEM));
435 lj_err_throw(L, LUA_ERRMEM);
438 /* Find error function for runtime errors. Requires an extra stack traversal. */
439 static ptrdiff_t finderrfunc(lua_State *L)
441 cTValue *frame = L->base-1, *bot = tvref(L->stack);
442 void *cf = L->cframe;
443 while (frame > bot) {
444 lua_assert(cf != NULL);
445 while (cframe_nres(cframe_raw(cf)) < 0) { /* cframe without frame? */
446 if (frame >= restorestack(L, -cframe_nres(cf)))
447 break;
448 if (cframe_errfunc(cf) >= 0) /* Error handler not inherited (-1)? */
449 return cframe_errfunc(cf);
450 cf = cframe_prev(cf); /* Else unwind cframe and continue searching. */
451 if (cf == NULL)
452 return 0;
454 switch (frame_typep(frame)) {
455 case FRAME_LUA:
456 case FRAME_LUAP:
457 frame = frame_prevl(frame);
458 break;
459 case FRAME_C:
460 cf = cframe_prev(cf);
461 /* fallthrough */
462 case FRAME_CONT:
463 case FRAME_VARG:
464 frame = frame_prevd(frame);
465 break;
466 case FRAME_CP:
467 if (cframe_canyield(cf)) return 0;
468 if (cframe_errfunc(cf) >= 0)
469 return cframe_errfunc(cf);
470 frame = frame_prevd(frame);
471 break;
472 case FRAME_PCALL:
473 case FRAME_PCALLH:
474 if (frame_ftsz(frame) >= (ptrdiff_t)(2*sizeof(TValue))) /* xpcall? */
475 return savestack(L, frame-1); /* Point to xpcall's errorfunc. */
476 return 0;
477 default:
478 lua_assert(0);
479 return 0;
482 return 0;
485 /* Runtime error. */
486 LJ_NOINLINE void lj_err_run(lua_State *L)
488 ptrdiff_t ef = finderrfunc(L);
489 if (ef) {
490 TValue *errfunc = restorestack(L, ef);
491 TValue *top = L->top;
492 lj_trace_abort(G(L));
493 if (!tvisfunc(errfunc) || L->status == LUA_ERRERR) {
494 setstrV(L, top-1, lj_err_str(L, LJ_ERR_ERRERR));
495 lj_err_throw(L, LUA_ERRERR);
497 L->status = LUA_ERRERR;
498 copyTV(L, top, top-1);
499 copyTV(L, top-1, errfunc);
500 L->top = top+1;
501 lj_vm_call(L, top, 1+1); /* Stack: |errfunc|msg| -> |msg| */
503 lj_err_throw(L, LUA_ERRRUN);
506 /* Formatted runtime error message. */
507 LJ_NORET LJ_NOINLINE static void err_msgv(lua_State *L, ErrMsg em, ...)
509 const char *msg;
510 va_list argp;
511 va_start(argp, em);
512 if (curr_funcisL(L)) L->top = curr_topL(L);
513 msg = lj_str_pushvf(L, err2msg(em), argp);
514 va_end(argp);
515 lj_debug_addloc(L, msg, L->base-1, NULL);
516 lj_err_run(L);
519 /* Non-vararg variant for better calling conventions. */
520 LJ_NOINLINE void lj_err_msg(lua_State *L, ErrMsg em)
522 err_msgv(L, em);
525 /* Lexer error. */
526 LJ_NOINLINE void lj_err_lex(lua_State *L, GCstr *src, const char *tok,
527 BCLine line, ErrMsg em, va_list argp)
529 char buff[LUA_IDSIZE];
530 const char *msg;
531 lj_debug_shortname(buff, src);
532 msg = lj_str_pushvf(L, err2msg(em), argp);
533 msg = lj_str_pushf(L, "%s:%d: %s", buff, line, msg);
534 if (tok)
535 lj_str_pushf(L, err2msg(LJ_ERR_XNEAR), msg, tok);
536 lj_err_throw(L, LUA_ERRSYNTAX);
539 /* Typecheck error for operands. */
540 LJ_NOINLINE void lj_err_optype(lua_State *L, cTValue *o, ErrMsg opm)
542 const char *tname = typename(o);
543 const char *opname = err2msg(opm);
544 if (curr_funcisL(L)) {
545 GCproto *pt = curr_proto(L);
546 const BCIns *pc = cframe_Lpc(L) - 1;
547 const char *oname = NULL;
548 const char *kind = lj_debug_slotname(pt, pc, (BCReg)(o-L->base), &oname);
549 if (kind)
550 err_msgv(L, LJ_ERR_BADOPRT, opname, kind, oname, tname);
552 err_msgv(L, LJ_ERR_BADOPRV, opname, tname);
555 /* Typecheck error for ordered comparisons. */
556 LJ_NOINLINE void lj_err_comp(lua_State *L, cTValue *o1, cTValue *o2)
558 const char *t1 = typename(o1);
559 const char *t2 = typename(o2);
560 err_msgv(L, t1 == t2 ? LJ_ERR_BADCMPV : LJ_ERR_BADCMPT, t1, t2);
561 /* This assumes the two "boolean" entries are commoned by the C compiler. */
564 /* Typecheck error for __call. */
565 LJ_NOINLINE void lj_err_optype_call(lua_State *L, TValue *o)
567 /* Gross hack if lua_[p]call or pcall/xpcall fail for a non-callable object:
568 ** L->base still points to the caller. So add a dummy frame with L instead
569 ** of a function. See lua_getstack().
571 const BCIns *pc = cframe_Lpc(L);
572 if (((ptrdiff_t)pc & FRAME_TYPE) != FRAME_LUA) {
573 const char *tname = typename(o);
574 setframe_pc(o, pc);
575 setframe_gc(o, obj2gco(L));
576 L->top = L->base = o+1;
577 err_msgv(L, LJ_ERR_BADCALL, tname);
579 lj_err_optype(L, o, LJ_ERR_OPCALL);
582 /* Error in context of caller. */
583 LJ_NOINLINE void lj_err_callermsg(lua_State *L, const char *msg)
585 TValue *frame = L->base-1;
586 TValue *pframe = NULL;
587 if (frame_islua(frame)) {
588 pframe = frame_prevl(frame);
589 } else if (frame_iscont(frame)) {
590 pframe = frame_prevd(frame);
591 #if LJ_HASFFI
592 /* Remove frame for FFI metamethods. */
593 if (frame_func(frame)->c.ffid >= FF_ffi_meta___index &&
594 frame_func(frame)->c.ffid <= FF_ffi_meta___tostring) {
595 L->base = pframe+1;
596 L->top = frame;
598 #endif
600 lj_debug_addloc(L, msg, pframe, frame);
601 lj_err_run(L);
604 /* Formatted error in context of caller. */
605 LJ_NOINLINE void lj_err_callerv(lua_State *L, ErrMsg em, ...)
607 const char *msg;
608 va_list argp;
609 va_start(argp, em);
610 msg = lj_str_pushvf(L, err2msg(em), argp);
611 va_end(argp);
612 lj_err_callermsg(L, msg);
615 /* Error in context of caller. */
616 LJ_NOINLINE void lj_err_caller(lua_State *L, ErrMsg em)
618 lj_err_callermsg(L, err2msg(em));
621 /* Argument error message. */
622 LJ_NORET LJ_NOINLINE static void err_argmsg(lua_State *L, int narg,
623 const char *msg)
625 const char *fname = "?";
626 const char *ftype = lj_debug_funcname(L, L->base - 1, &fname);
627 if (narg < 0 && narg > LUA_REGISTRYINDEX)
628 narg = (int)(L->top - L->base) + narg + 1;
629 if (ftype && ftype[3] == 'h' && --narg == 0) /* Check for "method". */
630 msg = lj_str_pushf(L, err2msg(LJ_ERR_BADSELF), fname, msg);
631 else
632 msg = lj_str_pushf(L, err2msg(LJ_ERR_BADARG), narg, fname, msg);
633 lj_err_callermsg(L, msg);
636 /* Formatted argument error. */
637 LJ_NOINLINE void lj_err_argv(lua_State *L, int narg, ErrMsg em, ...)
639 const char *msg;
640 va_list argp;
641 va_start(argp, em);
642 msg = lj_str_pushvf(L, err2msg(em), argp);
643 va_end(argp);
644 err_argmsg(L, narg, msg);
647 /* Argument error. */
648 LJ_NOINLINE void lj_err_arg(lua_State *L, int narg, ErrMsg em)
650 err_argmsg(L, narg, err2msg(em));
653 /* Typecheck error for arguments. */
654 LJ_NOINLINE void lj_err_argtype(lua_State *L, int narg, const char *xname)
656 TValue *o = L->base + narg-1;
657 const char *tname = o < L->top ? typename(o) : lj_obj_typename[0];
658 const char *msg = lj_str_pushf(L, err2msg(LJ_ERR_BADTYPE), xname, tname);
659 err_argmsg(L, narg, msg);
662 /* Typecheck error for arguments. */
663 LJ_NOINLINE void lj_err_argt(lua_State *L, int narg, int tt)
665 lj_err_argtype(L, narg, lj_obj_typename[tt+1]);
668 /* -- Public error handling API ------------------------------------------- */
670 LUA_API lua_CFunction lua_atpanic(lua_State *L, lua_CFunction panicf)
672 lua_CFunction old = G(L)->panic;
673 G(L)->panic = panicf;
674 return old;
677 /* Forwarders for the public API (C calling convention and no LJ_NORET). */
678 LUA_API int lua_error(lua_State *L)
680 lj_err_run(L);
681 return 0; /* unreachable */
684 LUALIB_API int luaL_argerror(lua_State *L, int narg, const char *msg)
686 err_argmsg(L, narg, msg);
687 return 0; /* unreachable */
690 LUALIB_API int luaL_typerror(lua_State *L, int narg, const char *xname)
692 lj_err_argtype(L, narg, xname);
693 return 0; /* unreachable */
696 LUALIB_API void luaL_where(lua_State *L, int level)
698 int size;
699 cTValue *frame = lj_debug_frame(L, level, &size);
700 lj_debug_addloc(L, "", frame, size ? frame+size : NULL);
703 LUALIB_API int luaL_error(lua_State *L, const char *fmt, ...)
705 const char *msg;
706 va_list argp;
707 va_start(argp, fmt);
708 msg = lj_str_pushvf(L, fmt, argp);
709 va_end(argp);
710 lj_err_callermsg(L, msg);
711 return 0; /* unreachable */