3 ** Copyright (C) 2005-2014 Mike Pall. See Copyright Notice in luajit.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.
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
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
);
84 copyTV(L
, top
, L
->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;
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? */
101 L
->cframe
= cframe_prev(cf
);
108 if (frame
<= tvref(L
->stack
))
110 switch (frame_typep(frame
)) {
111 case FRAME_LUA
: /* Lua frame. */
113 frame
= frame_prevl(frame
);
115 case FRAME_C
: /* C frame. */
121 L
->cframe
= cframe_prev(cf
);
122 L
->base
= frame_prevd(frame
) + 1;
123 unwindstack(L
, frame
);
124 } else if (cf
!= stopcf
) {
125 cf
= cframe_prev(cf
);
126 frame
= frame_prevd(frame
);
129 return NULL
; /* Continue unwinding. */
132 cf
= cframe_prev(cf
);
133 frame
= frame_prevd(frame
);
136 case FRAME_CP
: /* Protected C frame. */
137 if (cframe_canyield(cf
)) { /* Resume? */
139 hook_leave(G(L
)); /* Assumes nobody uses coroutines inside hooks. */
141 L
->status
= (uint8_t)errcode
;
146 L
->cframe
= cframe_prev(cf
);
147 L
->base
= frame_prevd(frame
) + 1;
148 unwindstack(L
, frame
);
151 case FRAME_CONT
: /* Continuation frame. */
153 if ((frame
-1)->u32
.lo
== LJ_CONT_FFI_CALLBACK
)
156 case FRAME_VARG
: /* Vararg frame. */
157 frame
= frame_prevd(frame
);
159 case FRAME_PCALL
: /* FF pcall() frame. */
160 case FRAME_PCALLH
: /* FF pcall() frame inside hook. */
162 if (errcode
== LUA_YIELD
) {
163 frame
= frame_prevd(frame
);
166 if (frame_typep(frame
) == FRAME_PCALL
)
169 L
->base
= frame_prevd(frame
) + 1;
170 unwindstack(L
, L
->base
);
172 return (void *)((intptr_t)cf
| CFRAME_UNWIND_FF
);
178 L
->base
= tvref(L
->stack
)+1;
179 unwindstack(L
, L
->base
);
184 return L
; /* Anything non-NULL will do. */
187 /* -- External frame unwinding -------------------------------------------- */
189 #if defined(__GNUC__) && !LJ_NO_UNWIND && !LJ_TARGET_WINDOWS
192 ** We have to use our own definitions instead of the mandatory (!) unwind.h,
193 ** since various OS, distros and compilers mess up the header installation.
196 typedef struct _Unwind_Exception
199 void (*excleanup
)(int, struct _Unwind_Exception
*);
201 } __attribute__((__aligned__
)) _Unwind_Exception
;
203 typedef struct _Unwind_Context _Unwind_Context
;
206 #define _URC_FATAL_PHASE1_ERROR 3
207 #define _URC_HANDLER_FOUND 6
208 #define _URC_INSTALL_CONTEXT 7
209 #define _URC_CONTINUE_UNWIND 8
210 #define _URC_FAILURE 9
214 extern uintptr_t _Unwind_GetCFA(_Unwind_Context
*);
215 extern void _Unwind_SetGR(_Unwind_Context
*, int, uintptr_t);
216 extern void _Unwind_SetIP(_Unwind_Context
*, uintptr_t);
217 extern void _Unwind_DeleteException(_Unwind_Exception
*);
218 extern int _Unwind_RaiseException(_Unwind_Exception
*);
220 #define _UA_SEARCH_PHASE 1
221 #define _UA_CLEANUP_PHASE 2
222 #define _UA_HANDLER_FRAME 4
223 #define _UA_FORCE_UNWIND 8
225 #define LJ_UEXCLASS 0x4c55414a49543200ULL /* LUAJIT2\0 */
226 #define LJ_UEXCLASS_MAKE(c) (LJ_UEXCLASS | (uint64_t)(c))
227 #define LJ_UEXCLASS_CHECK(cl) (((cl) ^ LJ_UEXCLASS) <= 0xff)
228 #define LJ_UEXCLASS_ERRCODE(cl) ((int)((cl) & 0xff))
230 /* DWARF2 personality handler referenced from interpreter .eh_frame. */
231 LJ_FUNCA
int lj_err_unwind_dwarf(int version
, int actions
,
232 uint64_t uexclass
, _Unwind_Exception
*uex
, _Unwind_Context
*ctx
)
237 return _URC_FATAL_PHASE1_ERROR
;
239 cf
= (void *)_Unwind_GetCFA(ctx
);
241 if ((actions
& _UA_SEARCH_PHASE
)) {
243 if (err_unwind(L
, cf
, 0) == NULL
)
244 return _URC_CONTINUE_UNWIND
;
246 if (!LJ_UEXCLASS_CHECK(uexclass
)) {
247 setstrV(L
, L
->top
++, lj_err_str(L
, LJ_ERR_ERRCPP
));
249 return _URC_HANDLER_FOUND
;
251 if ((actions
& _UA_CLEANUP_PHASE
)) {
253 if (LJ_UEXCLASS_CHECK(uexclass
)) {
254 errcode
= LJ_UEXCLASS_ERRCODE(uexclass
);
256 if ((actions
& _UA_HANDLER_FRAME
))
257 _Unwind_DeleteException(uex
);
258 errcode
= LUA_ERRRUN
;
261 cf
= err_unwind(L
, cf
, errcode
);
262 if ((actions
& _UA_FORCE_UNWIND
)) {
263 return _URC_CONTINUE_UNWIND
;
265 _Unwind_SetGR(ctx
, LJ_TARGET_EHRETREG
, errcode
);
266 _Unwind_SetIP(ctx
, (uintptr_t)(cframe_unwind_ff(cf
) ?
269 return _URC_INSTALL_CONTEXT
;
271 #if LJ_TARGET_X86ORX64
272 else if ((actions
& _UA_HANDLER_FRAME
)) {
273 /* Workaround for ancient libgcc bug. Still present in RHEL 5.5. :-/
274 ** Real fix: http://gcc.gnu.org/viewcvs/trunk/gcc/unwind-dw2.c?r1=121165&r2=124837&pathrev=153877&diff_format=h
276 _Unwind_SetGR(ctx
, LJ_TARGET_EHRETREG
, errcode
);
277 _Unwind_SetIP(ctx
, (uintptr_t)lj_vm_unwind_rethrow
);
278 return _URC_INSTALL_CONTEXT
;
282 /* This is not the proper way to escape from the unwinder. We get away with
283 ** it on non-x64 because the interpreter restores all callee-saved regs.
285 lj_err_throw(L
, errcode
);
288 return _URC_CONTINUE_UNWIND
;
292 #if LJ_TARGET_OSX || defined(__OpenBSD__)
293 /* Sorry, no thread safety for OSX. Complain to Apple, not me. */
294 static _Unwind_Exception static_uex
;
296 static __thread _Unwind_Exception static_uex
;
299 /* Raise DWARF2 exception. */
300 static void err_raise_ext(int errcode
)
302 static_uex
.exclass
= LJ_UEXCLASS_MAKE(errcode
);
303 static_uex
.excleanup
= NULL
;
304 _Unwind_RaiseException(&static_uex
);
310 extern void _Unwind_DeleteException(void *);
311 extern int __gnu_unwind_frame (void *, _Unwind_Context
*);
312 extern int _Unwind_VRS_Set(_Unwind_Context
*, int, uint32_t, int, void *);
313 extern int _Unwind_VRS_Get(_Unwind_Context
*, int, uint32_t, int, void *);
315 static inline uint32_t _Unwind_GetGR(_Unwind_Context
*ctx
, int r
)
318 _Unwind_VRS_Get(ctx
, 0, r
, 0, &v
);
322 static inline void _Unwind_SetGR(_Unwind_Context
*ctx
, int r
, uint32_t v
)
324 _Unwind_VRS_Set(ctx
, 0, r
, 0, &v
);
327 #define _US_VIRTUAL_UNWIND_FRAME 0
328 #define _US_UNWIND_FRAME_STARTING 1
329 #define _US_ACTION_MASK 3
330 #define _US_FORCE_UNWIND 8
332 /* ARM unwinder personality handler referenced from interpreter .ARM.extab. */
333 LJ_FUNCA
int lj_err_unwind_arm(int state
, void *ucb
, _Unwind_Context
*ctx
)
335 void *cf
= (void *)_Unwind_GetGR(ctx
, 13);
336 lua_State
*L
= cframe_L(cf
);
337 if ((state
& _US_ACTION_MASK
) == _US_VIRTUAL_UNWIND_FRAME
) {
338 setstrV(L
, L
->top
++, lj_err_str(L
, LJ_ERR_ERRCPP
));
339 return _URC_HANDLER_FOUND
;
341 if ((state
&(_US_ACTION_MASK
|_US_FORCE_UNWIND
)) == _US_UNWIND_FRAME_STARTING
) {
342 _Unwind_DeleteException(ucb
);
343 _Unwind_SetGR(ctx
, 15, (uint32_t)(void *)lj_err_throw
);
344 _Unwind_SetGR(ctx
, 0, (uint32_t)L
);
345 _Unwind_SetGR(ctx
, 1, (uint32_t)LUA_ERRRUN
);
346 return _URC_INSTALL_CONTEXT
;
348 if (__gnu_unwind_frame(ucb
, ctx
) != _URC_OK
)
350 return _URC_CONTINUE_UNWIND
;
355 #elif LJ_TARGET_X64 && LJ_TARGET_WINDOWS
358 ** Someone in Redmond owes me several days of my life. A lot of this is
359 ** undocumented or just plain wrong on MSDN. Some of it can be gathered
360 ** from 3rd party docs or must be found by trial-and-error. They really
361 ** don't want you to write your own language-specific exception handler
362 ** or to interact gracefully with MSVC. :-(
364 ** Apparently MSVC doesn't call C++ destructors for foreign exceptions
365 ** unless you compile your C++ code with /EHa. Unfortunately this means
366 ** catch (...) also catches things like access violations. The use of
367 ** _set_se_translator doesn't really help, because it requires /EHa, too.
370 #define WIN32_LEAN_AND_MEAN
373 /* Taken from: http://www.nynaeve.net/?p=99 */
374 typedef struct UndocumentedDispatcherContext
{
377 PRUNTIME_FUNCTION FunctionEntry
;
378 ULONG64 EstablisherFrame
;
380 PCONTEXT ContextRecord
;
381 void (*LanguageHandler
)(void);
383 PUNWIND_HISTORY_TABLE HistoryTable
;
386 } UndocumentedDispatcherContext
;
388 /* Another wild guess. */
389 extern void __DestructExceptionObject(EXCEPTION_RECORD
*rec
, int nothrow
);
391 #ifdef MINGW_SDK_INIT
392 /* Workaround for broken MinGW64 declaration. */
393 VOID
RtlUnwindEx_FIXED(PVOID
,PVOID
,PVOID
,PVOID
,PVOID
,PVOID
) asm("RtlUnwindEx");
394 #define RtlUnwindEx RtlUnwindEx_FIXED
397 #define LJ_MSVC_EXCODE ((DWORD)0xe06d7363)
398 #define LJ_GCC_EXCODE ((DWORD)0x20474343)
400 #define LJ_EXCODE ((DWORD)0xe24c4a00)
401 #define LJ_EXCODE_MAKE(c) (LJ_EXCODE | (DWORD)(c))
402 #define LJ_EXCODE_CHECK(cl) (((cl) ^ LJ_EXCODE) <= 0xff)
403 #define LJ_EXCODE_ERRCODE(cl) ((int)((cl) & 0xff))
405 /* Win64 exception handler for interpreter frame. */
406 LJ_FUNCA EXCEPTION_DISPOSITION
lj_err_unwind_win64(EXCEPTION_RECORD
*rec
,
407 void *cf
, CONTEXT
*ctx
, UndocumentedDispatcherContext
*dispatch
)
409 lua_State
*L
= cframe_L(cf
);
410 int errcode
= LJ_EXCODE_CHECK(rec
->ExceptionCode
) ?
411 LJ_EXCODE_ERRCODE(rec
->ExceptionCode
) : LUA_ERRRUN
;
412 if ((rec
->ExceptionFlags
& 6)) { /* EH_UNWINDING|EH_EXIT_UNWIND */
413 /* Unwind internal frames. */
414 err_unwind(L
, cf
, errcode
);
416 void *cf2
= err_unwind(L
, cf
, 0);
417 if (cf2
) { /* We catch it, so start unwinding the upper frames. */
418 if (rec
->ExceptionCode
== LJ_MSVC_EXCODE
||
419 rec
->ExceptionCode
== LJ_GCC_EXCODE
) {
420 __DestructExceptionObject(rec
, 1);
421 setstrV(L
, L
->top
++, lj_err_str(L
, LJ_ERR_ERRCPP
));
422 } else if (!LJ_EXCODE_CHECK(rec
->ExceptionCode
)) {
423 /* Don't catch access violations etc. */
424 return ExceptionContinueSearch
;
426 /* Unwind the stack and call all handlers for all lower C frames
427 ** (including ourselves) again with EH_UNWINDING set. Then set
428 ** rsp = cf, rax = errcode and jump to the specified target.
430 RtlUnwindEx(cf
, (void *)((cframe_unwind_ff(cf2
) && errcode
!= LUA_YIELD
) ?
433 rec
, (void *)(uintptr_t)errcode
, ctx
, dispatch
->HistoryTable
);
434 /* RtlUnwindEx should never return. */
437 return ExceptionContinueSearch
;
440 /* Raise Windows exception. */
441 static void err_raise_ext(int errcode
)
443 RaiseException(LJ_EXCODE_MAKE(errcode
), 1 /* EH_NONCONTINUABLE */, 0, NULL
);
448 /* -- Error handling ------------------------------------------------------ */
450 /* Throw error. Find catch frame, unwind stack and continue. */
451 LJ_NOINLINE
void LJ_FASTCALL
lj_err_throw(lua_State
*L
, int errcode
)
453 global_State
*g
= G(L
);
455 setgcrefnull(g
->jit_L
);
458 err_raise_ext(errcode
);
460 ** A return from this function signals a corrupt C stack that cannot be
461 ** unwound. We have no choice but to call the panic function and exit.
463 ** Usually this is caused by a C function without unwind information.
464 ** This should never happen on x64, but may happen if you've manually
465 ** enabled LUAJIT_UNWIND_EXTERNAL and forgot to recompile *every*
466 ** non-C++ file with -funwind-tables.
472 void *cf
= err_unwind(L
, NULL
, errcode
);
473 if (cframe_unwind_ff(cf
))
474 lj_vm_unwind_ff(cframe_raw(cf
));
476 lj_vm_unwind_c(cframe_raw(cf
), errcode
);
482 /* Return string object for error message. */
483 LJ_NOINLINE GCstr
*lj_err_str(lua_State
*L
, ErrMsg em
)
485 return lj_str_newz(L
, err2msg(em
));
488 /* Out-of-memory error. */
489 LJ_NOINLINE
void lj_err_mem(lua_State
*L
)
491 if (L
->status
== LUA_ERRERR
+1) /* Don't touch the stack during lua_open. */
492 lj_vm_unwind_c(L
->cframe
, LUA_ERRMEM
);
493 setstrV(L
, L
->top
++, lj_err_str(L
, LJ_ERR_ERRMEM
));
494 lj_err_throw(L
, LUA_ERRMEM
);
497 /* Find error function for runtime errors. Requires an extra stack traversal. */
498 static ptrdiff_t finderrfunc(lua_State
*L
)
500 cTValue
*frame
= L
->base
-1, *bot
= tvref(L
->stack
);
501 void *cf
= L
->cframe
;
502 while (frame
> bot
) {
503 lua_assert(cf
!= NULL
);
504 while (cframe_nres(cframe_raw(cf
)) < 0) { /* cframe without frame? */
505 if (frame
>= restorestack(L
, -cframe_nres(cf
)))
507 if (cframe_errfunc(cf
) >= 0) /* Error handler not inherited (-1)? */
508 return cframe_errfunc(cf
);
509 cf
= cframe_prev(cf
); /* Else unwind cframe and continue searching. */
513 switch (frame_typep(frame
)) {
516 frame
= frame_prevl(frame
);
519 cf
= cframe_prev(cf
);
522 frame
= frame_prevd(frame
);
526 if ((frame
-1)->u32
.lo
== LJ_CONT_FFI_CALLBACK
)
527 cf
= cframe_prev(cf
);
529 frame
= frame_prevd(frame
);
532 if (cframe_canyield(cf
)) return 0;
533 if (cframe_errfunc(cf
) >= 0)
534 return cframe_errfunc(cf
);
535 frame
= frame_prevd(frame
);
539 if (frame_ftsz(frame
) >= (ptrdiff_t)(2*sizeof(TValue
))) /* xpcall? */
540 return savestack(L
, frame
-1); /* Point to xpcall's errorfunc. */
551 LJ_NOINLINE
void lj_err_run(lua_State
*L
)
553 ptrdiff_t ef
= finderrfunc(L
);
555 TValue
*errfunc
= restorestack(L
, ef
);
556 TValue
*top
= L
->top
;
557 lj_trace_abort(G(L
));
558 if (!tvisfunc(errfunc
) || L
->status
== LUA_ERRERR
) {
559 setstrV(L
, top
-1, lj_err_str(L
, LJ_ERR_ERRERR
));
560 lj_err_throw(L
, LUA_ERRERR
);
562 L
->status
= LUA_ERRERR
;
563 copyTV(L
, top
, top
-1);
564 copyTV(L
, top
-1, errfunc
);
566 lj_vm_call(L
, top
, 1+1); /* Stack: |errfunc|msg| -> |msg| */
568 lj_err_throw(L
, LUA_ERRRUN
);
571 /* Formatted runtime error message. */
572 LJ_NORET LJ_NOINLINE
static void err_msgv(lua_State
*L
, ErrMsg em
, ...)
577 if (curr_funcisL(L
)) L
->top
= curr_topL(L
);
578 msg
= lj_str_pushvf(L
, err2msg(em
), argp
);
580 lj_debug_addloc(L
, msg
, L
->base
-1, NULL
);
584 /* Non-vararg variant for better calling conventions. */
585 LJ_NOINLINE
void lj_err_msg(lua_State
*L
, ErrMsg em
)
591 LJ_NOINLINE
void lj_err_lex(lua_State
*L
, GCstr
*src
, const char *tok
,
592 BCLine line
, ErrMsg em
, va_list argp
)
594 char buff
[LUA_IDSIZE
];
596 lj_debug_shortname(buff
, src
);
597 msg
= lj_str_pushvf(L
, err2msg(em
), argp
);
598 msg
= lj_str_pushf(L
, "%s:%d: %s", buff
, line
, msg
);
600 lj_str_pushf(L
, err2msg(LJ_ERR_XNEAR
), msg
, tok
);
601 lj_err_throw(L
, LUA_ERRSYNTAX
);
604 /* Typecheck error for operands. */
605 LJ_NOINLINE
void lj_err_optype(lua_State
*L
, cTValue
*o
, ErrMsg opm
)
607 const char *tname
= lj_typename(o
);
608 const char *opname
= err2msg(opm
);
609 if (curr_funcisL(L
)) {
610 GCproto
*pt
= curr_proto(L
);
611 const BCIns
*pc
= cframe_Lpc(L
) - 1;
612 const char *oname
= NULL
;
613 const char *kind
= lj_debug_slotname(pt
, pc
, (BCReg
)(o
-L
->base
), &oname
);
615 err_msgv(L
, LJ_ERR_BADOPRT
, opname
, kind
, oname
, tname
);
617 err_msgv(L
, LJ_ERR_BADOPRV
, opname
, tname
);
620 /* Typecheck error for ordered comparisons. */
621 LJ_NOINLINE
void lj_err_comp(lua_State
*L
, cTValue
*o1
, cTValue
*o2
)
623 const char *t1
= lj_typename(o1
);
624 const char *t2
= lj_typename(o2
);
625 err_msgv(L
, t1
== t2
? LJ_ERR_BADCMPV
: LJ_ERR_BADCMPT
, t1
, t2
);
626 /* This assumes the two "boolean" entries are commoned by the C compiler. */
629 /* Typecheck error for __call. */
630 LJ_NOINLINE
void lj_err_optype_call(lua_State
*L
, TValue
*o
)
632 /* Gross hack if lua_[p]call or pcall/xpcall fail for a non-callable object:
633 ** L->base still points to the caller. So add a dummy frame with L instead
634 ** of a function. See lua_getstack().
636 const BCIns
*pc
= cframe_Lpc(L
);
637 if (((ptrdiff_t)pc
& FRAME_TYPE
) != FRAME_LUA
) {
638 const char *tname
= lj_typename(o
);
640 setframe_gc(o
, obj2gco(L
));
641 L
->top
= L
->base
= o
+1;
642 err_msgv(L
, LJ_ERR_BADCALL
, tname
);
644 lj_err_optype(L
, o
, LJ_ERR_OPCALL
);
647 /* Error in context of caller. */
648 LJ_NOINLINE
void lj_err_callermsg(lua_State
*L
, const char *msg
)
650 TValue
*frame
= L
->base
-1;
651 TValue
*pframe
= NULL
;
652 if (frame_islua(frame
)) {
653 pframe
= frame_prevl(frame
);
654 } else if (frame_iscont(frame
)) {
656 if ((frame
-1)->u32
.lo
== LJ_CONT_FFI_CALLBACK
) {
662 pframe
= frame_prevd(frame
);
664 /* Remove frame for FFI metamethods. */
665 if (frame_func(frame
)->c
.ffid
>= FF_ffi_meta___index
&&
666 frame_func(frame
)->c
.ffid
<= FF_ffi_meta___tostring
) {
669 setcframe_pc(cframe_raw(L
->cframe
), frame_contpc(frame
));
674 lj_debug_addloc(L
, msg
, pframe
, frame
);
678 /* Formatted error in context of caller. */
679 LJ_NOINLINE
void lj_err_callerv(lua_State
*L
, ErrMsg em
, ...)
684 msg
= lj_str_pushvf(L
, err2msg(em
), argp
);
686 lj_err_callermsg(L
, msg
);
689 /* Error in context of caller. */
690 LJ_NOINLINE
void lj_err_caller(lua_State
*L
, ErrMsg em
)
692 lj_err_callermsg(L
, err2msg(em
));
695 /* Argument error message. */
696 LJ_NORET LJ_NOINLINE
static void err_argmsg(lua_State
*L
, int narg
,
699 const char *fname
= "?";
700 const char *ftype
= lj_debug_funcname(L
, L
->base
- 1, &fname
);
701 if (narg
< 0 && narg
> LUA_REGISTRYINDEX
)
702 narg
= (int)(L
->top
- L
->base
) + narg
+ 1;
703 if (ftype
&& ftype
[3] == 'h' && --narg
== 0) /* Check for "method". */
704 msg
= lj_str_pushf(L
, err2msg(LJ_ERR_BADSELF
), fname
, msg
);
706 msg
= lj_str_pushf(L
, err2msg(LJ_ERR_BADARG
), narg
, fname
, msg
);
707 lj_err_callermsg(L
, msg
);
710 /* Formatted argument error. */
711 LJ_NOINLINE
void lj_err_argv(lua_State
*L
, int narg
, ErrMsg em
, ...)
716 msg
= lj_str_pushvf(L
, err2msg(em
), argp
);
718 err_argmsg(L
, narg
, msg
);
721 /* Argument error. */
722 LJ_NOINLINE
void lj_err_arg(lua_State
*L
, int narg
, ErrMsg em
)
724 err_argmsg(L
, narg
, err2msg(em
));
727 /* Typecheck error for arguments. */
728 LJ_NOINLINE
void lj_err_argtype(lua_State
*L
, int narg
, const char *xname
)
730 TValue
*o
= narg
< 0 ? L
->top
+ narg
: L
->base
+ narg
-1;
731 const char *tname
= o
< L
->top
? lj_typename(o
) : lj_obj_typename
[0];
732 const char *msg
= lj_str_pushf(L
, err2msg(LJ_ERR_BADTYPE
), xname
, tname
);
733 err_argmsg(L
, narg
, msg
);
736 /* Typecheck error for arguments. */
737 LJ_NOINLINE
void lj_err_argt(lua_State
*L
, int narg
, int tt
)
739 lj_err_argtype(L
, narg
, lj_obj_typename
[tt
+1]);
742 /* -- Public error handling API ------------------------------------------- */
744 LUA_API lua_CFunction
lua_atpanic(lua_State
*L
, lua_CFunction panicf
)
746 lua_CFunction old
= G(L
)->panic
;
747 G(L
)->panic
= panicf
;
751 /* Forwarders for the public API (C calling convention and no LJ_NORET). */
752 LUA_API
int lua_error(lua_State
*L
)
755 return 0; /* unreachable */
758 LUALIB_API
int luaL_argerror(lua_State
*L
, int narg
, const char *msg
)
760 err_argmsg(L
, narg
, msg
);
761 return 0; /* unreachable */
764 LUALIB_API
int luaL_typerror(lua_State
*L
, int narg
, const char *xname
)
766 lj_err_argtype(L
, narg
, xname
);
767 return 0; /* unreachable */
770 LUALIB_API
void luaL_where(lua_State
*L
, int level
)
773 cTValue
*frame
= lj_debug_frame(L
, level
, &size
);
774 lj_debug_addloc(L
, "", frame
, size
? frame
+size
: NULL
);
777 LUALIB_API
int luaL_error(lua_State
*L
, const char *fmt
, ...)
782 msg
= lj_str_pushvf(L
, fmt
, argp
);
784 lj_err_callermsg(L
, msg
);
785 return 0; /* unreachable */